mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-23 05:12:39 +00:00
feat: auth openapi key
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { connectToDatabase, Model } from '@/service/mongo';
|
||||
import { getOpenAIApi } from '@/service/utils/chat';
|
||||
import { authToken } from '@/service/utils/tools';
|
||||
import { authOpenApiKey } from '@/service/utils/tools';
|
||||
import { httpsAgent, openaiChatFilter, systemPromptFilter } from '@/service/utils/tools';
|
||||
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||
import { ChatItemType } from '@/types/chat';
|
||||
@@ -36,7 +36,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
modelId: string;
|
||||
};
|
||||
|
||||
const { authorization } = req.headers;
|
||||
if (!prompt) {
|
||||
throw new Error('缺少参数');
|
||||
}
|
||||
@@ -46,7 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
let startTime = Date.now();
|
||||
|
||||
/* 凭证校验 */
|
||||
const userId = await authToken(authorization);
|
||||
const userId = await authOpenApiKey(req);
|
||||
|
||||
const { userApiKey, systemKey } = await getOpenApiKey(userId);
|
||||
|
||||
/* 查找数据库里的模型信息 */
|
@@ -74,7 +74,11 @@ const OpenApi = () => {
|
||||
<Tr key={id}>
|
||||
<Td>{apiKey}</Td>
|
||||
<Td>{dayjs(createTime).format('YYYY/MM/DD HH:mm:ss')}</Td>
|
||||
<Td>{dayjs(lastUsedTime).format('YYYY/MM/DD HH:mm:ss')}</Td>
|
||||
<Td>
|
||||
{lastUsedTime
|
||||
? dayjs(lastUsedTime).format('YYYY/MM/DD HH:mm:ss')
|
||||
: '没有使用过'}
|
||||
</Td>
|
||||
<Td>
|
||||
<IconButton
|
||||
icon={<DeleteIcon />}
|
||||
|
@@ -16,8 +16,7 @@ const OpenApiSchema = new Schema({
|
||||
default: () => new Date()
|
||||
},
|
||||
lastUsedTime: {
|
||||
type: Date,
|
||||
default: () => new Date()
|
||||
type: Date
|
||||
}
|
||||
});
|
||||
|
||||
|
@@ -1,8 +1,10 @@
|
||||
import type { NextApiRequest } from 'next';
|
||||
import crypto from 'crypto';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import tunnel from 'tunnel';
|
||||
import { ChatItemType } from '@/types/chat';
|
||||
import { encode } from 'gpt-token-utils';
|
||||
import { OpenApi } from '../mongo';
|
||||
|
||||
/* 密码加密 */
|
||||
export const hashPassword = (psw: string) => {
|
||||
@@ -41,6 +43,30 @@ export const authToken = (token?: string): Promise<string> => {
|
||||
});
|
||||
};
|
||||
|
||||
/* 校验 open api key */
|
||||
export const authOpenApiKey = (req: NextApiRequest) => {
|
||||
return new Promise<string>(async (resolve, reject) => {
|
||||
const { apikey: apiKey } = req.headers;
|
||||
|
||||
if (!apiKey) {
|
||||
reject('api key is empty');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const openApi = await OpenApi.findOne({ apiKey });
|
||||
if (!openApi) {
|
||||
return reject('api key is error');
|
||||
}
|
||||
await OpenApi.findByIdAndUpdate(openApi._id, {
|
||||
lastUsedTime: new Date()
|
||||
});
|
||||
resolve(String(openApi.userId));
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/* 代理 */
|
||||
export const httpsAgent =
|
||||
process.env.AXIOS_PROXY_HOST && process.env.AXIOS_PROXY_PORT
|
||||
|
2
src/types/mongoSchema.d.ts
vendored
2
src/types/mongoSchema.d.ts
vendored
@@ -153,6 +153,6 @@ export interface OpenApiSchema {
|
||||
_id: string;
|
||||
userId: string;
|
||||
createTime: Date;
|
||||
lastUsedTime: Date;
|
||||
lastUsedTime?: Date;
|
||||
apiKey: String;
|
||||
}
|
||||
|
2
src/types/openapi.d.ts
vendored
2
src/types/openapi.d.ts
vendored
@@ -2,5 +2,5 @@ export interface UserOpenApiKey {
|
||||
id: string;
|
||||
apiKey: string;
|
||||
createTime: Date;
|
||||
lastUsedTime: Date;
|
||||
lastUsedTime?: Date;
|
||||
}
|
||||
|
Reference in New Issue
Block a user