Update img_verify

This commit is contained in:
2023-08-07 19:28:25 +08:00
parent cd86f44cee
commit af5667f337
14 changed files with 310 additions and 90 deletions

29
server/check_auth.ts Normal file
View File

@@ -0,0 +1,29 @@
import { get_task_manager } from "../server.ts";
import { parse_cookies } from "./cookies.ts";
export function check_auth(req: Request) {
if (req.method === "OPTIONS") return true;
const m = get_task_manager();
if (m.db.get_user_count() === 0) return true;
const u = new URL(req.url);
let token: string | null | undefined = req.headers.get("X-TOKEN");
const cookies = parse_cookies(req.headers.get("Cookie"));
if (!token) {
token = cookies.get("token");
}
const check = () => {
if (u.pathname === "/api/token" && req.method === "PUT") return true;
if (u.pathname === "/api/status" && req.method === "GET") return true;
return false;
};
if (!token) return check();
const t = m.db.get_token(token);
const now = (new Date()).getTime();
if (!t || t.expired.getTime() < now) return check();
const user = m.db.get_user(t.uid);
if (!user) {
m.db.delete_token(token);
return check();
}
return true;
}