auth-server-helper/lib/Gateway.ts

42 lines
973 B
TypeScript
Raw Normal View History

2020-12-06 15:51:59 +01:00
type AnyFunc = (...args: unknown) => unknown;
type Gateway = (req: Request, res: Response, next: AnyFunc) => Promise<void>;
interface GatewayOptions {
redirect_url: string;
use_stored_sessions?: boolean;
}
class GatewayClass {
2020-12-06 21:06:40 +01:00
private _options: GatewayOptions;
2020-12-06 15:51:59 +01:00
public constructor (options: GatewayOptions) {
2020-12-06 21:06:40 +01:00
this._options = options;
2020-12-06 15:51:59 +01:00
}
private redirect (res): void {
res.statusCode = 302;
2020-12-06 21:06:40 +01:00
res.setHeader ('Location', this._options.redirect_url);
2020-12-06 15:51:59 +01:00
res.end ();
}
2020-12-06 21:06:40 +01:00
private async authenticate (req: Request): Promise<boolean> {
await Promise.resolve (req.body);
2020-12-06 15:51:59 +01:00
return false;
}
2020-12-06 21:06:40 +01:00
public async process_request (
req: Request,
res: Response,
next: AnyFunc
): Promise<void> {
2020-12-06 15:51:59 +01:00
if (await this.authenticate (req))
2020-12-06 21:06:40 +01:00
return next ();
return this.redirect (res);
2020-12-06 15:51:59 +01:00
}
}
export default function create_gateway (options: GatewayOptions): Gateway {
const g = new GatewayClass (options);
return g.process_request;
}