2022-05-17 01:25:05 +08:00

36 lines
724 B
TypeScript

import log from '../util/LogUtil';
import config from "../config";
let f = async (ctx, next) => {
try {
//检查是否有密码
if (checkToken(ctx)) {
await next();
} else {
ctx.status = 401;
ctx.body = "密钥验证错误";
}
} catch (error: any) {
if (error.status != undefined) {
ctx.status = error.status;
} else {
ctx.status = 500;
}
ctx.body = error.message;
log.error(error);
}
}
function checkToken(ctx) {
if (!config.token) {
return true;
}
let requestPath = ctx.method + ctx.path.replace(config.urlPrefix, "");
if (config.publicPath.has(requestPath)) {
return true;
}
return config.token == ctx.headers.token;
}
export default f;