key store
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-12-06 21:06:40 +01:00
parent 008fd3f545
commit 0be180f632
7 changed files with 162 additions and 9 deletions

View File

View File

@ -7,27 +7,31 @@ interface GatewayOptions {
}
class GatewayClass {
private options: GatewayOptions;
private _options: GatewayOptions;
public constructor (options: GatewayOptions) {
this.options = options;
this._options = options;
}
private redirect (res): void {
res.statusCode = 302;
res.setHeader ('Location', this.options.redirect_url);
res.setHeader ('Location', this._options.redirect_url);
res.end ();
}
private async authenticate (req): Promise<boolean> {
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> {
public async process_request (
req: Request,
res: Response,
next: AnyFunc
): Promise<void> {
if (await this.authenticate (req))
next ();
else
this.redirect (res);
return next ();
return this.redirect (res);
}
}

26
lib/KeyStore.ts Normal file
View File

@ -0,0 +1,26 @@
import { create_salt } from '@sapphirecode/crypto-helper';
class KeyStore {
private _keys: Record<string, string> = {};
public get_key (iat: number, valid_for = 0): string {
const key = Math.floor (iat / 60)
.toFixed (0);
if (typeof this._keys[key] === 'string')
return this._keys[key];
if (valid_for !== 0) {
this._keys[key] = create_salt ();
setTimeout (() => {
delete this._keys[key];
}, (valid_for + 5) * 1000);
return this._keys[key];
}
throw new Error ('key could not be found');
}
}
const ks: KeyStore = (new KeyStore);
export default ks;