temp
This commit is contained in:
@@ -1,43 +1,53 @@
|
||||
import { Query } from '@kevisual/query/query';
|
||||
import { app, assistantConfig } from '../app.ts';
|
||||
import './config/index.ts';
|
||||
import './shop-install/index.ts';
|
||||
import './ai/index.ts';
|
||||
import './light-code/index.ts';
|
||||
import './user/index.ts';
|
||||
|
||||
import os from 'node:os';
|
||||
|
||||
import { authCache } from '@/module/cache/auth.ts';
|
||||
export const getTokenUser = async (ctx: any) => {
|
||||
const query = assistantConfig.query
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
token: ctx.state.token,
|
||||
});
|
||||
if (res.code !== 200) {
|
||||
return ctx.throw(401, 'not login');
|
||||
}
|
||||
const tokenUser = res.data || {};
|
||||
return tokenUser;
|
||||
}
|
||||
const checkAuth = async (ctx: any, isAdmin = false) => {
|
||||
const config = assistantConfig.getConfig();
|
||||
const { auth } = config;
|
||||
const host = config.pageApi || config.registry || 'https://kevisual.cn';
|
||||
const url = new URL('/api/router', host);
|
||||
const { auth = {} } = config;
|
||||
const token = ctx.query.token;
|
||||
if (auth && auth.type !== 'public') {
|
||||
if (!token) {
|
||||
return ctx.throw(401, 'not login');
|
||||
}
|
||||
url.searchParams.set('token', token);
|
||||
// 鉴权代理
|
||||
// TODO:
|
||||
const query = new Query({ url: url.toString() });
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
});
|
||||
if (res.code !== 200) {
|
||||
return ctx.throw(401, 'not login');
|
||||
}
|
||||
const tokenUser = res.data || {};
|
||||
ctx.state = {
|
||||
...ctx.state,
|
||||
tokenUser,
|
||||
};
|
||||
const { username } = tokenUser;
|
||||
if (isAdmin) {
|
||||
if (auth.username && auth.username !== username) {
|
||||
return ctx.throw(403, 'not admin user');
|
||||
}
|
||||
|
||||
if (!token) {
|
||||
return ctx.throw(401, 'not login');
|
||||
}
|
||||
// 鉴权代理
|
||||
let tokenUser = await authCache.get(token);
|
||||
if (!tokenUser) {
|
||||
tokenUser = await getTokenUser(ctx);
|
||||
authCache.set(token, tokenUser);
|
||||
}
|
||||
ctx.state = {
|
||||
...ctx.state,
|
||||
token,
|
||||
tokenUser,
|
||||
};
|
||||
const { username } = tokenUser;
|
||||
if (!auth.username) {
|
||||
// 初始管理员账号
|
||||
auth.username = username;
|
||||
assistantConfig.setConfig({ auth });
|
||||
}
|
||||
if (isAdmin) {
|
||||
if (auth.username && auth.username !== username) {
|
||||
return ctx.throw(403, 'not admin user');
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -45,8 +55,7 @@ app
|
||||
.route({
|
||||
path: 'auth',
|
||||
id: 'auth',
|
||||
isDebug: true,
|
||||
description: '获取当前登录用户信息',
|
||||
description: '获取当前登录用户信息, 第一个登录的用户为管理员用户',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
await checkAuth(ctx);
|
||||
@@ -56,6 +65,7 @@ app
|
||||
.route({
|
||||
path: 'admin-auth',
|
||||
id: 'admin-auth',
|
||||
description: '管理员鉴权, 获取用户信息,并验证是否为管理员。',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
await checkAuth(ctx, true);
|
||||
@@ -66,6 +76,7 @@ app
|
||||
.route({
|
||||
path: 'client',
|
||||
key: 'version',
|
||||
description: '获取客户端版本号',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'v1.0.0';
|
||||
@@ -76,6 +87,7 @@ app
|
||||
.route({
|
||||
path: 'client',
|
||||
key: 'time',
|
||||
description: '获取当前时间',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = {
|
||||
|
||||
Reference in New Issue
Block a user