get boolean return from auth handler
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
86b07af63d
commit
4820bda8ca
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
@ -176,29 +184,12 @@ interface CreateHandlerOptions {
|
||||
}
|
||||
|
||||
// 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,
|
||||
options?: CreateHandlerOptions
|
||||
) {
|
||||
// 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);
|
||||
|
||||
): Promise<void> | void {
|
||||
if (token === null)
|
||||
return default_handler (request);
|
||||
|
||||
@ -251,6 +242,34 @@ export default function create_auth_handler (
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -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",
|
||||
|
Loading…
x
Reference in New Issue
Block a user