import AuthProof from "./AuthProof"; import MysqlConnectionManager from "../db/MysqlConnectionManager"; import User from "./models/User"; import {Connection} from "mysql"; import {Request} from "express"; import {PENDING_ACCOUNT_REVIEW_MAIL_TEMPLATE} from "../Mails"; import Mail from "../Mail"; import Controller from "../Controller"; import config from "config"; export default abstract class AuthGuard
> { protected abstract async getProofForSession(session: Express.Session): Promise
; protected async getProofForRequest(_req: Request): Promise
{ return null; } public async getProof(req: Request): Promise
{ let proof = await this.isAuthenticatedViaRequest(req); if (!proof && req.session) { proof = await this.isAuthenticated(req.session); } return proof; } public async isAuthenticated(session: Express.Session): Promise
{ if (!session.is_authenticated) return null; const proof = await this.getProofForSession(session); if (!proof || !await proof.isValid() || !await proof.isAuthorized()) { await proof?.revoke(); session.is_authenticated = false; return null; } return proof; } public async isAuthenticatedViaRequest(req: Request): Promise
{
const proof = await this.getProofForRequest(req);
if (!proof || !await proof.isValid() || !await proof.isAuthorized()) {
await proof?.revoke();
return null;
}
return proof;
}
public async authenticateOrRegister(
session: Express.Session,
proof: P,
onLogin?: (user: User) => Promise