From e157e40525cad9f5bd70f7b119cd960534656ee1 Mon Sep 17 00:00:00 2001 From: Vinlic Date: Wed, 10 Apr 2024 18:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0refresh=5Ftoken=E5=AD=98?= =?UTF-8?q?=E6=B4=BB=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 21 +++++++++++++++++++++ src/api/controllers/chat.ts | 26 ++++++++++++++++++++++++++ src/api/routes/index.ts | 4 +++- src/api/routes/token.ts | 25 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/api/routes/token.ts diff --git a/README.md b/README.md index 203587e..831e47e 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te * [对话补全](#对话补全) * [文档解读](#文档解读) * [图像解析](#图像解析) + * [refresh_token存活检测](#refresh_token存活检测) * [注意事项](#注意事项) * [Nginx反代优化](#Nginx反代优化) @@ -379,6 +380,26 @@ Authorization: Bearer [refresh_token] } ``` +### refresh_token存活检测 + +检测refresh_token是否存活,如果存活live未true,否则为false,请不要频繁(小于10分钟)调用此接口。 + +**POST /token/check** + +请求数据: +```json +{ + "token": "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9..." +} +``` + +响应数据: +```json +{ + "live": true +} +``` + ## 注意事项 ### Nginx反代优化 diff --git a/src/api/controllers/chat.ts b/src/api/controllers/chat.ts index 6b282a1..8050da0 100644 --- a/src/api/controllers/chat.ts +++ b/src/api/controllers/chat.ts @@ -739,9 +739,35 @@ function tokenSplit(authorization: string) { return authorization.replace('Bearer ', '').split(','); } +/** + * 获取Token存活状态 + */ +async function getTokenLiveStatus(refreshToken: string) { + const result = await axios.get('https://kimi.moonshot.cn/api/auth/token/refresh', { + headers: { + Authorization: `Bearer ${refreshToken}`, + Referer: 'https://kimi.moonshot.cn/', + ...FAKE_HEADERS + }, + timeout: 15000, + validateStatus: () => true + }); + try { + const { + access_token, + refresh_token + } = checkResult(result, refreshToken); + return !!(access_token && refresh_token) + } + catch(err) { + return false; + } +} + export default { createConversation, createCompletion, createCompletionStream, + getTokenLiveStatus, tokenSplit }; diff --git a/src/api/routes/index.ts b/src/api/routes/index.ts index 4cc91bc..72dc88f 100644 --- a/src/api/routes/index.ts +++ b/src/api/routes/index.ts @@ -3,6 +3,7 @@ import fs from 'fs-extra'; import Response from '@/lib/response/Response.ts'; import chat from "./chat.ts"; import ping from "./ping.ts"; +import token from './token.ts'; export default [ { @@ -19,5 +20,6 @@ export default [ } }, chat, - ping + ping, + token ]; \ No newline at end of file diff --git a/src/api/routes/token.ts b/src/api/routes/token.ts new file mode 100644 index 0000000..c69c512 --- /dev/null +++ b/src/api/routes/token.ts @@ -0,0 +1,25 @@ +import _ from 'lodash'; + +import Request from '@/lib/request/Request.ts'; +import Response from '@/lib/response/Response.ts'; +import chat from '@/api/controllers/chat.ts'; +import logger from '@/lib/logger.ts'; + +export default { + + prefix: '/token', + + post: { + + '/check': async (request: Request) => { + request + .validate('body.token', _.isString) + const live = await chat.getTokenLiveStatus(request.body.token); + return { + live + } + } + + } + +} \ No newline at end of file