option to enable body parsing
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Timo Hocker 2022-01-26 09:08:45 +01:00
parent cc8762e4ec
commit ec08f8f04e
Signed by: Timo
GPG Key ID: DFAC2CF4E1D1BEC9
5 changed files with 25 additions and 14 deletions

View File

@ -1,5 +1,9 @@
# Changelog
## 3.1.0
- Option to enable body parsing
## 3.0.0
- Allows Cookies Parameters to be set

View File

@ -1,6 +1,6 @@
# auth-server-helper
version: 3.0.x
version: 3.1.x
customizable and simple authentication
@ -104,7 +104,8 @@ const handler = create_auth_handler(
},
},
cookie: { name: 'auth_cookie' }, // if defined, access tokens will be stored in this cookie,
refresh_cookie: { name: 'refresh_cookie' } // if defined, refresh tokens will be stored in this cookie
refresh_cookie: { name: 'refresh_cookie' }, // if defined, refresh tokens will be stored in this cookie
parse_body: true // read the request body into a string (default false)
}
);

View File

@ -225,14 +225,17 @@ interface CreateHandlerOptions {
modules?: Record<string, AuthRequestHandler>;
cookie?: CookieSettings;
refresh_cookie?: CookieSettings;
parse_body?: boolean;
}
type ProcessRequestOptions = Omit<CreateHandlerOptions, 'parse_body'>
// eslint-disable-next-line max-lines-per-function
function process_request (
request: AuthRequest,
token: RegExpExecArray | null,
default_handler: AuthRequestHandler,
options?: CreateHandlerOptions
options?: ProcessRequestOptions
): Promise<void> | void {
if (token === null)
return default_handler (request);
@ -313,7 +316,8 @@ export default function create_auth_handler (
req: IncomingMessage,
res: ServerResponse
): Promise<boolean> => {
const body: string = await new Promise ((resolve) => {
const body: string = options?.parse_body
? await new Promise ((resolve) => {
let data = '';
req.on ('data', (c) => {
data += c;
@ -321,7 +325,8 @@ export default function create_auth_handler (
req.on ('end', () => {
resolve (data);
});
});
})
: '';
const request = new AuthRequest (
req,

View File

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

View File

@ -129,7 +129,8 @@ describe ('auth handler', () => {
}
else { request.deny (); }
}
}
},
parse_body: true
});
server = http.createServer (async (