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;
} }
@ -175,16 +183,76 @@ interface CreateHandlerOptions {
cookie_name?: string; cookie_name?: string;
} }
// eslint-disable-next-line max-lines-per-function
function process_request (
request: AuthRequest,
token: RegExpExecArray | null,
default_handler: AuthRequestHandler,
options?: CreateHandlerOptions
): Promise<void> | void {
if (token === null)
return default_handler (request);
if ((/Basic/ui).test (token?.groups?.type as string)) {
request.is_basic = true;
let login = token?.groups?.token as string;
if (!login.includes (':'))
login = to_utf8 (login, 'base64');
const login_data = login.split (':');
request.user = login_data[0];
request.password = login_data[1];
return default_handler (request);
}
if ((/Bearer/ui).test (token?.groups?.type as string)) {
request.is_bearer = true;
request.token = token?.groups?.token;
const token_data = auth.verify (request.token as string);
if (!token_data.valid)
return default_handler (request);
request.token_data = token_data.data;
request.token_id = token_data.id;
if (
typeof options !== 'undefined'
&& typeof options.refresh !== 'undefined'
&& token_data.type === 'refresh_token'
) {
request.allow_access (options.refresh);
return Promise.resolve ();
}
if (
typeof options !== 'undefined'
&& typeof options.modules !== 'undefined'
&& token_data.type === 'part_token'
&& typeof token_data.next_module !== 'undefined'
&& Object.keys (options.modules)
.includes (token_data.next_module)
)
return options.modules[token_data.next_module] (request);
request.invalid ('invalid bearer type');
return Promise.resolve ();
}
return default_handler (request);
}
// eslint-disable-next-line max-lines-per-function // eslint-disable-next-line max-lines-per-function
export default function create_auth_handler ( export default function create_auth_handler (
default_handler: AuthRequestHandler, default_handler: AuthRequestHandler,
options?: CreateHandlerOptions options?: CreateHandlerOptions
) { ) {
// eslint-disable-next-line max-lines-per-function return async (
return async function process_request (
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse res: ServerResponse
): Promise<void> { ): Promise<boolean> => {
const body: string = await new Promise ((resolve) => { const body: string = await new Promise ((resolve) => {
let data = ''; let data = '';
req.on ('data', (c) => { req.on ('data', (c) => {
@ -199,58 +267,9 @@ export default function create_auth_handler (
const token = (/(?<type>\S+) (?<token>.+)/ui) const token = (/(?<type>\S+) (?<token>.+)/ui)
.exec (req.headers.authorization as string); .exec (req.headers.authorization as string);
if (token === null) process_request (request, token, default_handler, options);
return default_handler (request);
if ((/Basic/ui).test (token?.groups?.type as string)) { return request.is_successful;
request.is_basic = true;
let login = token?.groups?.token as string;
if (!login.includes (':'))
login = to_utf8 (login, 'base64');
const login_data = login.split (':');
request.user = login_data[0];
request.password = login_data[1];
return default_handler (request);
}
if ((/Bearer/ui).test (token?.groups?.type as string)) {
request.is_bearer = true;
request.token = token?.groups?.token;
const token_data = auth.verify (request.token as string);
if (!token_data.valid)
return default_handler (request);
request.token_data = token_data.data;
request.token_id = token_data.id;
if (
typeof options !== 'undefined'
&& typeof options.refresh !== 'undefined'
&& token_data.type === 'refresh_token'
) {
request.allow_access (options.refresh);
return Promise.resolve ();
}
if (
typeof options !== 'undefined'
&& typeof options.modules !== 'undefined'
&& token_data.type === 'part_token'
&& typeof token_data.next_module !== 'undefined'
&& Object.keys (options.modules)
.includes (token_data.next_module)
)
return options.modules[token_data.next_module] (request);
request.invalid ('invalid bearer type');
return Promise.resolve ();
}
return default_handler (request);
}; };
} }

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",