diff --git a/README.md b/README.md index 94263b0..840e73c 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ app.use(handler); // node http // ... 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 diff --git a/lib/AuthHandler.ts b/lib/AuthHandler.ts index 5affbd9..6d90041 100644 --- a/lib/AuthHandler.ts +++ b/lib/AuthHandler.ts @@ -46,6 +46,11 @@ class AuthRequest { public body: string; private _cookie_name?: string; + private _is_successful: boolean; + + public get is_successful (): boolean { + return this._is_successful; + } public constructor ( req: IncomingMessage, @@ -61,6 +66,7 @@ class AuthRequest { this.user = ''; this.password = ''; this._cookie_name = cookie; + this._is_successful = false; } private default_header (set_content = true) { @@ -123,6 +129,7 @@ class AuthRequest { this.response.writeHead (200); this.response.end (JSON.stringify (res)); + this._is_successful = true; return result; } @@ -148,6 +155,7 @@ class AuthRequest { this.response.writeHead (200); this.response.end (JSON.stringify (res)); + this._is_successful = true; return pt.id; } @@ -175,16 +183,76 @@ interface CreateHandlerOptions { 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 { + 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 export default function create_auth_handler ( default_handler: AuthRequestHandler, options?: CreateHandlerOptions ) { - // eslint-disable-next-line max-lines-per-function - return async function process_request ( + return async ( req: IncomingMessage, res: ServerResponse - ): Promise { + ): Promise => { const body: string = await new Promise ((resolve) => { let data = ''; req.on ('data', (c) => { @@ -199,58 +267,9 @@ export default function create_auth_handler ( const token = (/(?\S+) (?.+)/ui) .exec (req.headers.authorization as string); - if (token === null) - return default_handler (request); + process_request (request, token, default_handler, options); - 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); + return request.is_successful; }; } diff --git a/package.json b/package.json index 141d808..6cf5f1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sapphirecode/auth-server-helper", - "version": "2.1.0", + "version": "2.1.1", "main": "dist/index.js", "author": { "name": "Timo Hocker",