From ea1681e1eba637c5f47a2a7805d7b84ab8792cc3 Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Fri, 7 Apr 2023 23:33:59 +0800 Subject: [PATCH] feat: auth openapi key --- src/pages/api/openapi/{ => chat}/lafGpt.ts | 6 ++--- src/pages/openapi/index.tsx | 6 ++++- src/service/models/openapi.ts | 3 +-- src/service/utils/tools.ts | 26 ++++++++++++++++++++++ src/types/mongoSchema.d.ts | 2 +- src/types/openapi.d.ts | 2 +- 6 files changed, 37 insertions(+), 8 deletions(-) rename src/pages/api/openapi/{ => chat}/lafGpt.ts (98%) diff --git a/src/pages/api/openapi/lafGpt.ts b/src/pages/api/openapi/chat/lafGpt.ts similarity index 98% rename from src/pages/api/openapi/lafGpt.ts rename to src/pages/api/openapi/chat/lafGpt.ts index 14963cc00..3b480d17e 100644 --- a/src/pages/api/openapi/lafGpt.ts +++ b/src/pages/api/openapi/chat/lafGpt.ts @@ -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); /* 查找数据库里的模型信息 */ diff --git a/src/pages/openapi/index.tsx b/src/pages/openapi/index.tsx index 3319d01e5..a88fc580e 100644 --- a/src/pages/openapi/index.tsx +++ b/src/pages/openapi/index.tsx @@ -74,7 +74,11 @@ const OpenApi = () => { {apiKey} {dayjs(createTime).format('YYYY/MM/DD HH:mm:ss')} - {dayjs(lastUsedTime).format('YYYY/MM/DD HH:mm:ss')} + + {lastUsedTime + ? dayjs(lastUsedTime).format('YYYY/MM/DD HH:mm:ss') + : '没有使用过'} + } diff --git a/src/service/models/openapi.ts b/src/service/models/openapi.ts index 1e2718f95..203290305 100644 --- a/src/service/models/openapi.ts +++ b/src/service/models/openapi.ts @@ -16,8 +16,7 @@ const OpenApiSchema = new Schema({ default: () => new Date() }, lastUsedTime: { - type: Date, - default: () => new Date() + type: Date } }); diff --git a/src/service/utils/tools.ts b/src/service/utils/tools.ts index fba658500..5c0420138 100644 --- a/src/service/utils/tools.ts +++ b/src/service/utils/tools.ts @@ -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 => { }); }; +/* 校验 open api key */ +export const authOpenApiKey = (req: NextApiRequest) => { + return new Promise(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 diff --git a/src/types/mongoSchema.d.ts b/src/types/mongoSchema.d.ts index f8ff8ee0e..227c28025 100644 --- a/src/types/mongoSchema.d.ts +++ b/src/types/mongoSchema.d.ts @@ -153,6 +153,6 @@ export interface OpenApiSchema { _id: string; userId: string; createTime: Date; - lastUsedTime: Date; + lastUsedTime?: Date; apiKey: String; } diff --git a/src/types/openapi.d.ts b/src/types/openapi.d.ts index 484790a56..21b7e9a29 100644 --- a/src/types/openapi.d.ts +++ b/src/types/openapi.d.ts @@ -2,5 +2,5 @@ export interface UserOpenApiKey { id: string; apiKey: string; createTime: Date; - lastUsedTime: Date; + lastUsedTime?: Date; }