feat: login handler

This commit is contained in:
手瓜一十雪 2024-05-06 22:52:57 +08:00
parent 54ab1dc091
commit bd8a1d715f
4 changed files with 35 additions and 7 deletions

View File

@ -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) => {

View File

@ -2,7 +2,7 @@ let LoginRuntime = {
LoginCurrentTime: Date.now(),
LoginCurrentRate: 0
}
export const Data = {
export const DataRuntime = {
checkLoginRate: async function (RateLimit: number): Promise<boolean> {
if (Date.now() - LoginRuntime.LoginCurrentTime > 1000 * 60) {
LoginRuntime.LoginCurrentRate = 0;//超出时间重置限速

View File

@ -27,15 +27,17 @@ async function tryUsePort(port: number, tryCount: number = 0): Promise<number> {
export interface WebUiConfig {
port: number;
token: string;
loginRate: number
}
// 读取当前目录下名为 webui.json 的配置文件,如果不存在则创建初始化配置文件
export async function config(): Promise<WebUiConfig> {
export async function WebUIConfig(): Promise<WebUiConfig> {
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)) {

View File

@ -1 +0,0 @@
//静态资源