27 lines
889 B
TypeScript
27 lines
889 B
TypeScript
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;
|