This commit is contained in:
@ -2,6 +2,7 @@ import {
|
||||
get_signature_info,
|
||||
verify_signature
|
||||
} from '@sapphirecode/crypto-helper';
|
||||
import { run_regex } from '@sapphirecode/utilities';
|
||||
import keystore from './KeyStore';
|
||||
import blacklist from './Blacklist';
|
||||
|
||||
@ -10,6 +11,7 @@ type Gateway = (req: Request, res: Response, next: AnyFunc) => Promise<void>;
|
||||
|
||||
interface GatewayOptions {
|
||||
redirect_url: string;
|
||||
cookie_name?: string;
|
||||
}
|
||||
|
||||
class GatewayClass {
|
||||
@ -25,19 +27,40 @@ class GatewayClass {
|
||||
res.end ();
|
||||
}
|
||||
|
||||
private get_header_auth (req: Request): string | null {
|
||||
const auth_header = req.headers.get ('Authorization');
|
||||
const auth = (/(?<type>\w)+ (?<data>.*)/u).exec (auth_header);
|
||||
if (auth === null)
|
||||
return null;
|
||||
if (auth.groups.type !== 'Bearer')
|
||||
return null;
|
||||
return auth.groups.data;
|
||||
}
|
||||
|
||||
private get_cookie_auth (req: Request): string | null {
|
||||
let auth = null;
|
||||
run_regex (
|
||||
/[\^;](?<name>[^;=]+)=(?<value>[^;]+)/gu,
|
||||
req.headers.get ('cookie'),
|
||||
(res) => {
|
||||
if (res.groups.name === this._options.cookie_name)
|
||||
auth = res.groups.value;
|
||||
}
|
||||
);
|
||||
return auth;
|
||||
}
|
||||
|
||||
private authenticate (req: Request): Promise<boolean> {
|
||||
const auth = req.headers.get ('Authentication');
|
||||
const auth_type_regex = /(?<type>\w)+ (?<data>.*)/u;
|
||||
const auth_type = auth_type_regex.exec (auth);
|
||||
if (auth_type === null)
|
||||
return false;
|
||||
if (auth_type.groups.type !== 'Bearer')
|
||||
let auth = this.get_header_auth (req);
|
||||
if (auth === null)
|
||||
auth = this.get_cookie_auth (req);
|
||||
if (auth === null)
|
||||
return false;
|
||||
|
||||
const data = get_signature_info (auth_type.groups.data);
|
||||
const data = get_signature_info (auth);
|
||||
const key = keystore.get_key (data.iat / 1000);
|
||||
const valid = verify_signature (
|
||||
auth_type.groups.data,
|
||||
auth,
|
||||
key,
|
||||
data.obj.valid_for * 1000
|
||||
) === null;
|
||||
|
Reference in New Issue
Block a user