auth-server-helper/lib/Gateway.ts
Timo Hocker 0be180f632
Some checks failed
continuous-integration/drone/push Build is failing
key store
2020-12-06 21:08:42 +01:00

42 lines
973 B
TypeScript

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 {
private _options: GatewayOptions;
public constructor (options: GatewayOptions) {
this._options = options;
}
private redirect (res): void {
res.statusCode = 302;
res.setHeader ('Location', this._options.redirect_url);
res.end ();
}
private async authenticate (req: Request): Promise<boolean> {
await Promise.resolve (req.body);
return false;
}
public async process_request (
req: Request,
res: Response,
next: AnyFunc
): Promise<void> {
if (await this.authenticate (req))
return next ();
return this.redirect (res);
}
}
export default function create_gateway (options: GatewayOptions): Gateway {
const g = new GatewayClass (options);
return g.process_request;
}