File size: 889 Bytes
bf48b89 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import type { MiddlewareHandler } from 'hono';
import { config } from '@/config';
import RejectError from '@/errors/types/reject';
import md5 from '@/utils/md5';
const reject = (requestPath) => {
throw new RejectError(`Authentication failed. Access denied.\n${requestPath}`);
};
const middleware: MiddlewareHandler = async (ctx, next) => {
const requestPath = new URL(ctx.req.url).pathname;
const accessKey = ctx.req.query('key');
const accessCode = ctx.req.query('code');
if (requestPath === '/' || requestPath === '/robots.txt' || requestPath === '/favicon.ico' || requestPath === '/logo.png') {
await next();
} else {
if (config.accessKey && !(config.accessKey === accessKey || accessCode === md5(requestPath + config.accessKey))) {
return reject(requestPath);
}
await next();
}
};
export default middleware;
|