complete redesign

This commit is contained in:
2021-01-03 14:51:07 +01:00
parent 4c27d0eace
commit f39759bad9
6 changed files with 99 additions and 18 deletions

@ -1,3 +1,10 @@
/*
* 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>, January 2021
*/
import { IncomingMessage, ServerResponse } from 'http';
import { to_utf8 } from '@sapphirecode/encoding-helper';
import auth from './Authority';
@ -29,16 +36,19 @@ class AuthRequest {
public is_basic: boolean;
public user: string;
public password: string;
public body: string;
private _cookie_name?: string;
public constructor (
req: IncomingMessage,
res: ServerResponse,
body: string,
cookie?: string
) {
this.request = req;
this.response = res;
this.body = body;
this.is_basic = false;
this.user = '';
this.password = '';
@ -88,6 +98,23 @@ class AuthRequest {
return result;
}
public allow_part (part_token_expires_in: number, module: string): string {
this.default_header ();
const pt = auth.sign ('part_token', part_token_expires_in, module);
const res = {
token_type: 'bearer',
part_token: pt.signature,
expires_in: part_token_expires_in
};
this.response.writeHead (200);
this.response.end (JSON.stringify (res));
return pt.id;
}
public invalid (error_description?: string) {
this.default_header ();
this.response.writeHead (400);
@ -117,11 +144,22 @@ export default function create_auth_handler (
default_handler: AuthRequestHandler,
options?: CreateHandlerOptions
) {
return function process_request (
// eslint-disable-next-line max-lines-per-function
return async function process_request (
req: IncomingMessage,
res: ServerResponse
): Promise<void>|void {
const request = new AuthRequest (req, res, options?.cookie_name);
): Promise<void> {
const body: string = await new Promise ((resolve) => {
let data = '';
req.on ('data', (c) => {
data += c;
});
req.on ('end', () => {
resolve (data);
});
});
const request = new AuthRequest (req, res, body, options?.cookie_name);
const token = (/(?<type>\S+) (?<token>.+)/ui)
.exec (req.headers.authorization as string);