feat: 更新助手配置,添加应用ID和URL,优化身份验证和代理逻辑
This commit is contained in:
@@ -21,7 +21,16 @@ app
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const { data } = ctx.query;
|
||||
ctx.body = assistantConfig.setConfig(data);
|
||||
ctx.body = assistantConfig.setConfig(data, true);
|
||||
reload();
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app.route({
|
||||
path: 'config',
|
||||
key: 'getId'
|
||||
}).define(async (ctx) => {
|
||||
const config = assistantConfig.getCacheAssistantConfig();
|
||||
ctx.body = config?.app?.id || null;
|
||||
|
||||
}).addTo(app);
|
||||
@@ -9,18 +9,28 @@ import './hot-api/key-sender/index.ts';
|
||||
|
||||
import os from 'node:os';
|
||||
import { authCache } from '@/module/cache/auth.ts';
|
||||
export const getTokenUser = async (ctx: any) => {
|
||||
const getTokenUser = async (token: string) => {
|
||||
const query = assistantConfig.query
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
token: ctx.state.token || ctx.query.token,
|
||||
token: token,
|
||||
});
|
||||
if (res.code !== 200) {
|
||||
return ctx.throw(401, 'not login');
|
||||
return res;
|
||||
}
|
||||
export const getTokenUserCache = async (token: string) => {
|
||||
const tokenUser = await authCache.get(token);
|
||||
if (tokenUser) {
|
||||
return {
|
||||
code: 200,
|
||||
data: tokenUser,
|
||||
};
|
||||
}
|
||||
const tokenUser = res.data || {};
|
||||
return tokenUser;
|
||||
const res = await getTokenUser(token);
|
||||
if (res.code === 200) {
|
||||
authCache.set(token, res.data);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
const checkAuth = async (ctx: any, isAdmin = false) => {
|
||||
const config = assistantConfig.getConfig();
|
||||
@@ -33,7 +43,12 @@ const checkAuth = async (ctx: any, isAdmin = false) => {
|
||||
// 鉴权代理
|
||||
let tokenUser = await authCache.get(token);
|
||||
if (!tokenUser) {
|
||||
tokenUser = await getTokenUser(ctx);
|
||||
const tokenUserRes = await getTokenUser(token);
|
||||
if (tokenUserRes.code !== 200) {
|
||||
return ctx.throw(tokenUserRes.code, 'not login');
|
||||
} else {
|
||||
tokenUser = tokenUserRes.data;
|
||||
}
|
||||
authCache.set(token, tokenUser);
|
||||
}
|
||||
ctx.state = {
|
||||
|
||||
@@ -56,8 +56,8 @@ app.route({
|
||||
if (!auth.username) {
|
||||
// 初始管理员账号
|
||||
auth.username = loginUser;
|
||||
if (!auth.type) {
|
||||
auth.type = 'public';
|
||||
if (!auth.share) {
|
||||
auth.share = 'protected';
|
||||
}
|
||||
assistantConfig.setConfig({ auth });
|
||||
console.log('set first admin user', { username: loginUser });
|
||||
|
||||
Reference in New Issue
Block a user