Skip to content

Commit

Permalink
增加refresh_token存活检测
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Apr 10, 2024
1 parent d08a4b2 commit e157e40
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ ZhipuAI (智谱清言) 接口转API [glm-free-api](https://github.com/LLM-Red-Te
* [对话补全](#对话补全)
* [文档解读](#文档解读)
* [图像解析](#图像解析)
* [refresh_token存活检测](#refresh_token存活检测)
* [注意事项](#注意事项)
* [Nginx反代优化](#Nginx反代优化)

Expand Down Expand Up @@ -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反代优化
Expand Down
26 changes: 26 additions & 0 deletions src/api/controllers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
4 changes: 3 additions & 1 deletion src/api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand All @@ -19,5 +20,6 @@ export default [
}
},
chat,
ping
ping,
token
];
25 changes: 25 additions & 0 deletions src/api/routes/token.ts
Original file line number Diff line number Diff line change
@@ -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
}
}

}

}

0 comments on commit e157e40

Please sign in to comment.