get boolean return from auth handler
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2022-01-03 15:40:13 +01:00
parent 86b07af63d
commit 4820bda8ca
Signed by: Timo
GPG Key ID: DFAC2CF4E1D1BEC9
3 changed files with 75 additions and 56 deletions

View File

@ -107,7 +107,7 @@ app.use(handler);
// node http // node http
// ... create server, on path /auth run the handler // ... create server, on path /auth run the handler
handler(req, res); handler(req, res); // the handler will also return true if allow_access or allow_part was called
``` ```
after the auth handler, the request will be completed, no additional content after the auth handler, the request will be completed, no additional content

View File

@ -46,6 +46,11 @@ class AuthRequest {
public body: string; public body: string;
private _cookie_name?: string; private _cookie_name?: string;
private _is_successful: boolean;
public get is_successful (): boolean {
return this._is_successful;
}
public constructor ( public constructor (
req: IncomingMessage, req: IncomingMessage,
@ -61,6 +66,7 @@ class AuthRequest {
this.user = ''; this.user = '';
this.password = ''; this.password = '';
this._cookie_name = cookie; this._cookie_name = cookie;
this._is_successful = false;
} }
private default_header (set_content = true) { private default_header (set_content = true) {
@ -123,6 +129,7 @@ class AuthRequest {
this.response.writeHead (200); this.response.writeHead (200);
this.response.end (JSON.stringify (res)); this.response.end (JSON.stringify (res));
this._is_successful = true;
return result; return result;
} }
@ -148,6 +155,7 @@ class AuthRequest {
this.response.writeHead (200); this.response.writeHead (200);
this.response.end (JSON.stringify (res)); this.response.end (JSON.stringify (res));
this._is_successful = true;
return pt.id; return pt.id;
} }
@ -176,29 +184,12 @@ interface CreateHandlerOptions {
} }
// eslint-disable-next-line max-lines-per-function // eslint-disable-next-line max-lines-per-function
export default function create_auth_handler ( function process_request (
request: AuthRequest,
token: RegExpExecArray | null,
default_handler: AuthRequestHandler, default_handler: AuthRequestHandler,
options?: CreateHandlerOptions options?: CreateHandlerOptions
) { ): Promise<void> | void {
// eslint-disable-next-line max-lines-per-function
return async function process_request (
req: IncomingMessage,
res: ServerResponse
): 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);
if (token === null) if (token === null)
return default_handler (request); return default_handler (request);
@ -251,6 +242,34 @@ export default function create_auth_handler (
} }
return default_handler (request); return default_handler (request);
}
// eslint-disable-next-line max-lines-per-function
export default function create_auth_handler (
default_handler: AuthRequestHandler,
options?: CreateHandlerOptions
) {
return async (
req: IncomingMessage,
res: ServerResponse
): Promise<boolean> => {
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);
process_request (request, token, default_handler, options);
return request.is_successful;
}; };
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "@sapphirecode/auth-server-helper", "name": "@sapphirecode/auth-server-helper",
"version": "2.1.0", "version": "2.1.1",
"main": "dist/index.js", "main": "dist/index.js",
"author": { "author": {
"name": "Timo Hocker", "name": "Timo Hocker",