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 type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { connectToDatabase, Model } from '@/service/mongo';
|
import { connectToDatabase, Model } from '@/service/mongo';
|
||||||
import { getOpenAIApi } from '@/service/utils/chat';
|
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 { httpsAgent, openaiChatFilter, systemPromptFilter } from '@/service/utils/tools';
|
||||||
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
import { ChatCompletionRequestMessage, ChatCompletionRequestMessageRoleEnum } from 'openai';
|
||||||
import { ChatItemType } from '@/types/chat';
|
import { ChatItemType } from '@/types/chat';
|
||||||
@@ -36,7 +36,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
modelId: string;
|
modelId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const { authorization } = req.headers;
|
|
||||||
if (!prompt) {
|
if (!prompt) {
|
||||||
throw new Error('缺少参数');
|
throw new Error('缺少参数');
|
||||||
}
|
}
|
||||||
@@ -46,7 +45,8 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
|||||||
let startTime = Date.now();
|
let startTime = Date.now();
|
||||||
|
|
||||||
/* 凭证校验 */
|
/* 凭证校验 */
|
||||||
const userId = await authToken(authorization);
|
const userId = await authOpenApiKey(req);
|
||||||
|
|
||||||
const { userApiKey, systemKey } = await getOpenApiKey(userId);
|
const { userApiKey, systemKey } = await getOpenApiKey(userId);
|
||||||
|
|
||||||
/* 查找数据库里的模型信息 */
|
/* 查找数据库里的模型信息 */
|
@@ -74,7 +74,11 @@ const OpenApi = () => {
|
|||||||
<Tr key={id}>
|
<Tr key={id}>
|
||||||
<Td>{apiKey}</Td>
|
<Td>{apiKey}</Td>
|
||||||
<Td>{dayjs(createTime).format('YYYY/MM/DD HH:mm:ss')}</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>
|
<Td>
|
||||||
<IconButton
|
<IconButton
|
||||||
icon={<DeleteIcon />}
|
icon={<DeleteIcon />}
|
||||||
|
@@ -16,8 +16,7 @@ const OpenApiSchema = new Schema({
|
|||||||
default: () => new Date()
|
default: () => new Date()
|
||||||
},
|
},
|
||||||
lastUsedTime: {
|
lastUsedTime: {
|
||||||
type: Date,
|
type: Date
|
||||||
default: () => new Date()
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
|
import type { NextApiRequest } from 'next';
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import tunnel from 'tunnel';
|
import tunnel from 'tunnel';
|
||||||
import { ChatItemType } from '@/types/chat';
|
import { ChatItemType } from '@/types/chat';
|
||||||
import { encode } from 'gpt-token-utils';
|
import { encode } from 'gpt-token-utils';
|
||||||
|
import { OpenApi } from '../mongo';
|
||||||
|
|
||||||
/* 密码加密 */
|
/* 密码加密 */
|
||||||
export const hashPassword = (psw: string) => {
|
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 =
|
export const httpsAgent =
|
||||||
process.env.AXIOS_PROXY_HOST && process.env.AXIOS_PROXY_PORT
|
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;
|
_id: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
createTime: Date;
|
createTime: Date;
|
||||||
lastUsedTime: Date;
|
lastUsedTime?: Date;
|
||||||
apiKey: String;
|
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;
|
id: string;
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
createTime: Date;
|
createTime: Date;
|
||||||
lastUsedTime: Date;
|
lastUsedTime?: Date;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user