allow gateway without redirection, manual request handling
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-01-03 14:44:27 +01:00
parent c55ed33e53
commit 85a5f3c2fb
4 changed files with 39 additions and 9 deletions

View File

@ -16,24 +16,32 @@ type Gateway = (
) => unknown;
interface GatewayOptions {
redirect_url: string;
redirect_url?: string;
cookie_name?: string;
}
class GatewayClass {
private _options: GatewayOptions;
public constructor (options: GatewayOptions) {
public constructor (options: GatewayOptions = {}) {
this._options = options;
}
private redirect (res: ServerResponse): void {
public deny (res: ServerResponse): void {
res.statusCode = 403;
res.end();
}
public redirect (res: ServerResponse): void {
if (typeof this._options.redirect_url !== 'string')
return this.deny(res);
res.statusCode = 302;
res.setHeader ('Location', this._options.redirect_url);
res.end ();
}
private get_header_auth (req: IncomingMessage): string | null {
public get_header_auth (req: IncomingMessage): string | null {
const auth_header = req.headers.authorization;
const auth = (/(?<type>\w+) (?<data>.*)/u).exec (auth_header || '');
if (auth === null)
@ -43,7 +51,7 @@ class GatewayClass {
return auth.groups?.data;
}
private get_cookie_auth (req: IncomingMessage): string | null {
public get_cookie_auth (req: IncomingMessage): string | null {
if (typeof this._options.cookie_name === 'undefined')
return null;
let auth = null;
@ -58,7 +66,7 @@ class GatewayClass {
return auth;
}
private authenticate (req: IncomingMessage): boolean {
public authenticate (req: IncomingMessage): boolean {
let auth = this.get_header_auth (req);
if (auth === null)
auth = this.get_cookie_auth (req);