2020-04-24 12:12:27 +02:00
|
|
|
import AuthProof from "./AuthProof";
|
|
|
|
import MysqlConnectionManager from "../db/MysqlConnectionManager";
|
|
|
|
import User from "./models/User";
|
2020-04-25 09:35:49 +02:00
|
|
|
import {Connection} from "mysql";
|
2020-06-14 11:59:02 +02:00
|
|
|
import {Request} from "express";
|
2020-07-20 17:32:32 +02:00
|
|
|
import {PENDING_ACCOUNT_REVIEW_MAIL_TEMPLATE} from "../Mails";
|
|
|
|
import Mail from "../Mail";
|
|
|
|
import Controller from "../Controller";
|
|
|
|
import config from "config";
|
2020-04-24 12:12:27 +02:00
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
export default abstract class AuthGuard<P extends AuthProof<User>> {
|
2020-07-28 11:47:20 +02:00
|
|
|
protected abstract async getProofForSession(session: Express.Session): Promise<P | null>;
|
2020-04-24 12:12:27 +02:00
|
|
|
|
2020-07-28 11:47:20 +02:00
|
|
|
protected async getProofForRequest(req: Request): Promise<P | null> {
|
2020-06-14 11:59:02 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:47:20 +02:00
|
|
|
public async getProof(req: Request): Promise<P | null> {
|
|
|
|
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<P | null> {
|
|
|
|
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<P | null> {
|
|
|
|
const proof = await this.getProofForRequest(req);
|
|
|
|
|
|
|
|
if (!proof || !await proof.isValid() || !await proof.isAuthorized()) {
|
|
|
|
await proof?.revoke();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return proof;
|
|
|
|
}
|
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
public async authenticateOrRegister(
|
|
|
|
session: Express.Session,
|
|
|
|
proof: P,
|
|
|
|
onLogin?: (user: User) => Promise<void>,
|
|
|
|
onRegister?: (connection: Connection, user: User) => Promise<RegisterCallback[]>
|
|
|
|
): Promise<void> {
|
|
|
|
if (!await proof.isValid()) throw new InvalidAuthProofError();
|
|
|
|
if (!await proof.isAuthorized()) throw new UnauthorizedAuthProofError();
|
2020-04-24 12:12:27 +02:00
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
let user = await proof.getResource();
|
2020-04-24 12:12:27 +02:00
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
// Register if user doesn't exist
|
|
|
|
if (!user) {
|
|
|
|
const callbacks: RegisterCallback[] = [];
|
2020-04-24 12:12:27 +02:00
|
|
|
|
|
|
|
await MysqlConnectionManager.wrapTransaction(async connection => {
|
2020-07-27 10:52:39 +02:00
|
|
|
user = User.create({});
|
2020-07-25 10:28:50 +02:00
|
|
|
if (onRegister) {
|
|
|
|
(await onRegister(connection, user)).forEach(c => callbacks.push(c));
|
2020-04-25 09:36:20 +02:00
|
|
|
}
|
2020-07-26 11:37:01 +02:00
|
|
|
await user.save(connection, c => callbacks.push(c));
|
2020-04-24 12:12:27 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
for (const callback of callbacks) {
|
|
|
|
await callback();
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:32:32 +02:00
|
|
|
if (user) {
|
2020-07-24 13:00:20 +02:00
|
|
|
if (!user!.isApproved()) {
|
2020-07-20 17:32:32 +02:00
|
|
|
await new Mail(PENDING_ACCOUNT_REVIEW_MAIL_TEMPLATE, {
|
|
|
|
username: user!.name,
|
2020-07-24 12:59:44 +02:00
|
|
|
link: config.get<string>('base_url') + Controller.route('accounts-approval'),
|
2020-07-20 17:32:32 +02:00
|
|
|
}).send(config.get<string>('app.contact_email'));
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-24 12:12:27 +02:00
|
|
|
throw new Error('Unable to register user.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
// Don't login if user isn't approved
|
2020-07-24 13:00:20 +02:00
|
|
|
if (!user.isApproved()) {
|
|
|
|
throw new PendingApprovalAuthError();
|
|
|
|
}
|
2020-06-16 11:12:58 +02:00
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
// Login
|
2020-07-28 11:47:20 +02:00
|
|
|
session.is_authenticated = true;
|
2020-07-25 10:28:50 +02:00
|
|
|
if (onLogin) await onLogin(user);
|
2020-04-24 12:12:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AuthError extends Error {
|
|
|
|
constructor(message: string) {
|
|
|
|
super(message);
|
|
|
|
}
|
2020-04-25 16:08:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
export class AuthProofError extends AuthError {
|
|
|
|
}
|
|
|
|
|
|
|
|
export class InvalidAuthProofError extends AuthProofError {
|
|
|
|
constructor() {
|
|
|
|
super('Invalid auth proof.');
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 16:08:20 +02:00
|
|
|
|
2020-07-25 10:28:50 +02:00
|
|
|
export class UnauthorizedAuthProofError extends AuthProofError {
|
|
|
|
constructor() {
|
|
|
|
super('Unauthorized auth proof.');
|
2020-04-25 16:08:20 +02:00
|
|
|
}
|
2020-06-16 11:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export class PendingApprovalAuthError extends AuthError {
|
|
|
|
constructor() {
|
|
|
|
super(`User is not approved.`);
|
|
|
|
}
|
2020-07-24 13:00:20 +02:00
|
|
|
}
|
2020-07-25 10:28:50 +02:00
|
|
|
|
|
|
|
export type RegisterCallback = () => Promise<void>;
|