From bd8a1d715fb7dbe393138450338c63be8b2bcfaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 6 May 2024 22:52:57 +0800 Subject: [PATCH] feat: login handler --- src/webui/src/api/Auth.ts | 33 ++++++++++++++++++++++++++++++--- src/webui/src/helper/Data.ts | 2 +- src/webui/src/helper/config.ts | 6 ++++-- src/webui/src/router/static.ts | 1 - 4 files changed, 35 insertions(+), 7 deletions(-) delete mode 100644 src/webui/src/router/static.ts diff --git a/src/webui/src/api/Auth.ts b/src/webui/src/api/Auth.ts index bd65e365..8fe96769 100644 --- a/src/webui/src/api/Auth.ts +++ b/src/webui/src/api/Auth.ts @@ -1,7 +1,34 @@ import { RequestHandler } from "express"; - -export const LoginHandler: RequestHandler = (req, res) => { - +import { AuthHelper } from "../helper/SignToken"; +import { WebUIConfig } from "../helper/config"; +import { DataRuntime } from "../helper/Data"; +const isEmpty = (data: any) => data === undefined || data === null || data === ''; +export const LoginHandler: RequestHandler = async (req, res) => { + const { token } = req.body; + if (isEmpty(token)) { + res.status(400).send({ + code: 400, + message: 'token is empty' + }); + return; + } + let config = await WebUIConfig(); + if (!DataRuntime.checkLoginRate(config.loginRate)) { + res.status(400).send({ + code: 400, + message: 'login rate limit' + }); + return; + } + let signCredential = Buffer.from(JSON.stringify(AuthHelper.signCredential(config.token))).toString('base64'); + res.json({ + code: 200, + message: 'success', + data: { + "Credential": signCredential + } + }); + return; }; export const LogoutHandler: RequestHandler = (req, res) => { diff --git a/src/webui/src/helper/Data.ts b/src/webui/src/helper/Data.ts index 8d775556..a5f010ba 100644 --- a/src/webui/src/helper/Data.ts +++ b/src/webui/src/helper/Data.ts @@ -2,7 +2,7 @@ let LoginRuntime = { LoginCurrentTime: Date.now(), LoginCurrentRate: 0 } -export const Data = { +export const DataRuntime = { checkLoginRate: async function (RateLimit: number): Promise { if (Date.now() - LoginRuntime.LoginCurrentTime > 1000 * 60) { LoginRuntime.LoginCurrentRate = 0;//超出时间重置限速 diff --git a/src/webui/src/helper/config.ts b/src/webui/src/helper/config.ts index 7a0f2240..7a519e78 100644 --- a/src/webui/src/helper/config.ts +++ b/src/webui/src/helper/config.ts @@ -27,15 +27,17 @@ async function tryUsePort(port: number, tryCount: number = 0): Promise { export interface WebUiConfig { port: number; token: string; + loginRate: number } // 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件 -export async function config(): Promise { +export async function WebUIConfig(): Promise { try { let configPath = resolve(__dirname, "./webui.json"); let config: WebUiConfig = { port: 6099, - token: Math.random().toString(36).slice(2)//生成随机密码 + token: Math.random().toString(36).slice(2),//生成随机密码 + loginRate: 3 }; if (!existsSync(configPath)) { diff --git a/src/webui/src/router/static.ts b/src/webui/src/router/static.ts deleted file mode 100644 index ba129364..00000000 --- a/src/webui/src/router/static.ts +++ /dev/null @@ -1 +0,0 @@ -//静态资源 \ No newline at end of file