auth-server-helper/lib/Gateway.ts

197 lines
5.2 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Auth-Server-Helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, December 2020
*/
2020-12-28 16:53:08 +01:00
import { IncomingMessage, ServerResponse } from 'http';
2020-12-19 15:40:49 +01:00
import authority from './Authority';
2022-01-04 21:32:04 +01:00
import { AuthRequest, AccessSettings } from './AuthHandler';
2022-01-05 12:32:04 +01:00
import { debug } from './debug';
2022-01-08 22:10:02 +01:00
import { extract_cookie } from './cookie';
2022-01-05 12:32:04 +01:00
const logger = debug ('gateway');
2020-12-12 15:53:47 +01:00
2020-12-28 16:53:08 +01:00
type AnyFunc = (...args: unknown[]) => unknown;
type Gateway = (
req: IncomingMessage,
res: ServerResponse, next: AnyFunc
) => unknown;
2020-12-06 15:51:59 +01:00
2022-01-04 21:32:04 +01:00
interface RefreshSettings extends AccessSettings {
leave_open?: never;
redirect_to?: never;
2022-01-05 08:11:18 +01:00
data?: never;
2022-01-04 21:32:04 +01:00
}
2020-12-06 15:51:59 +01:00
interface GatewayOptions {
redirect_url?: string;
2020-12-13 12:26:40 +01:00
cookie_name?: string;
2022-01-04 21:32:04 +01:00
refresh_cookie_name?: string;
refresh_settings?: RefreshSettings;
}
2020-12-06 15:51:59 +01:00
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 = {}) {
2022-01-05 12:32:04 +01:00
logger ('creating new gateway');
2022-01-04 21:32:04 +01:00
if (
typeof options.cookie_name === 'string'
&& options.cookie_name === options.refresh_cookie_name
)
throw new Error ('access and refresh cookies cannot have the same name');
2020-12-06 21:06:40 +01:00
this._options = options;
2020-12-06 15:51:59 +01:00
}
public deny (res: ServerResponse): void {
2022-01-05 12:32:04 +01:00
logger ('denied http request');
res.statusCode = 403;
2022-01-03 14:46:12 +01:00
res.end ();
}
public redirect (res: ServerResponse): void {
2022-01-05 12:32:04 +01:00
logger ('redirecting http request to %s', this._options.redirect_url);
2022-01-03 14:46:12 +01:00
if (typeof this._options.redirect_url !== 'string') {
2022-01-05 12:32:04 +01:00
logger ('no redirect url defined');
2022-01-03 14:46:12 +01:00
this.deny (res);
return;
}
2020-12-06 15:51:59 +01:00
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 ();
}
public get_header_auth (req: IncomingMessage): string | null {
2022-01-05 12:32:04 +01:00
logger ('extracting authorization header');
2020-12-28 16:53:08 +01:00
const auth_header = req.headers.authorization;
const auth = (/(?<type>\w+) (?<data>.*)/u).exec (auth_header || '');
2020-12-13 12:26:40 +01:00
if (auth === null)
return null;
2020-12-28 16:53:08 +01:00
if (auth.groups?.type !== 'Bearer')
2020-12-13 12:26:40 +01:00
return null;
2022-01-05 12:32:04 +01:00
logger ('found bearer token');
2020-12-28 16:53:08 +01:00
return auth.groups?.data;
2020-12-13 12:26:40 +01:00
}
2022-01-05 12:58:00 +01:00
public try_access (req: IncomingMessage): boolean {
2022-01-05 12:32:04 +01:00
logger ('authenticating incoming request');
2020-12-13 12:26:40 +01:00
let auth = this.get_header_auth (req);
if (auth === null)
2022-01-08 22:10:02 +01:00
auth = extract_cookie (this._options.cookie_name, req.headers.cookie);
2022-01-05 12:32:04 +01:00
if (auth === null) {
logger ('found no auth token');
2020-12-12 15:53:47 +01:00
return false;
2022-01-05 12:32:04 +01:00
}
2020-12-12 15:53:47 +01:00
2021-01-03 15:32:29 +01:00
const ver = authority.verify (auth);
2022-01-05 12:32:04 +01:00
logger ('setting connection info');
2021-01-05 15:59:06 +01:00
const con = req.connection as unknown as Record<string, unknown>;
con.auth = { token_id: ver.id, token_data: ver.data };
2021-01-03 15:32:29 +01:00
2022-01-05 12:32:04 +01:00
logger ('token valid: %s', ver.authorized);
2021-01-03 15:32:29 +01:00
return ver.authorized;
2020-12-06 15:51:59 +01:00
}
2022-01-04 21:32:04 +01:00
public async try_refresh (
req: IncomingMessage,
res: ServerResponse
): Promise<boolean> {
if (
typeof this._options.refresh_cookie_name === 'undefined'
|| typeof this._options.refresh_settings === 'undefined'
)
return false;
2022-01-05 12:32:04 +01:00
logger ('trying to apply refresh token');
2022-01-08 22:10:02 +01:00
const refresh = extract_cookie (
this._options.refresh_cookie_name,
req.headers.cookie
);
2022-01-05 12:32:04 +01:00
if (refresh === null) {
logger ('could not find refresh token');
2022-01-04 21:32:04 +01:00
return false;
2022-01-05 12:32:04 +01:00
}
2022-01-04 21:32:04 +01:00
const ver = authority.verify (refresh);
if (ver.type === 'refresh_token' && ver.valid) {
2022-01-05 12:32:04 +01:00
logger ('refresh token valid, generating new tokens');
2022-01-04 21:32:04 +01:00
const auth_request = new AuthRequest (
req,
res,
''
, this._options.cookie_name,
this._options.refresh_cookie_name
);
const refresh_result = await auth_request.allow_access ({
...this._options.refresh_settings,
2022-01-05 08:11:18 +01:00
data: ver.data,
leave_open: true
2022-01-04 21:32:04 +01:00
});
2022-01-05 12:32:04 +01:00
logger ('setting connection info');
2022-01-04 21:32:04 +01:00
const con = req.connection as unknown as Record<string, unknown>;
con.auth = {
token_id: refresh_result.access_token_id,
token_data: this._options.refresh_settings.data
};
2022-01-05 12:32:04 +01:00
logger ('tokens refreshed');
2022-01-04 21:32:04 +01:00
return true;
}
2022-01-05 12:32:04 +01:00
logger ('refresh token invalid');
2022-01-04 21:32:04 +01:00
return false;
}
2022-01-05 12:58:00 +01:00
public async authenticate (
req: IncomingMessage,
res: ServerResponse
2022-01-05 13:01:32 +01:00
): Promise<boolean> {
2022-01-05 12:58:00 +01:00
logger ('trying to authenticate http request');
if (this.try_access (req)) {
logger ('authenticated via access_token');
return true;
}
if (await this.try_refresh (req, res)) {
logger ('authenticated via refresh_token');
return true;
}
logger ('could not verify session');
return false;
}
2022-01-04 21:32:04 +01:00
public async process_request (
2020-12-28 16:53:08 +01:00
req: IncomingMessage,
res: ServerResponse,
2020-12-06 21:06:40 +01:00
next: AnyFunc
2022-01-04 21:32:04 +01:00
): Promise<unknown> {
2022-01-05 12:32:04 +01:00
logger ('processing incoming http request');
2022-01-05 12:58:00 +01:00
if (await this.authenticate (req, res)) {
2022-01-05 12:32:04 +01:00
logger ('authentification successful, calling next handler');
2020-12-06 21:06:40 +01:00
return next ();
2022-01-05 12:32:04 +01:00
}
2022-01-05 12:58:00 +01:00
logger ('failed to authenticate, redirecting client');
2020-12-06 21:06:40 +01:00
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);
2020-12-28 16:53:08 +01:00
return g.process_request.bind (g);
2020-12-06 15:51:59 +01:00
}
2020-12-28 16:53:08 +01:00
2021-01-05 22:10:41 +01:00
export {
AnyFunc,
Gateway,
GatewayOptions,
2022-01-05 08:11:18 +01:00
GatewayClass,
RefreshSettings
2021-01-05 22:10:41 +01:00
};