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 # Changelog
## 3.1.0
- Option to enable body parsing
## 3.0.0 ## 3.0.0
- Allows Cookies Parameters to be set - Allows Cookies Parameters to be set

View File

@ -1,6 +1,6 @@
# auth-server-helper # auth-server-helper
version: 3.0.x version: 3.1.x
customizable and simple authentication 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, 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>; modules?: Record<string, AuthRequestHandler>;
cookie?: CookieSettings; cookie?: CookieSettings;
refresh_cookie?: CookieSettings; refresh_cookie?: CookieSettings;
parse_body?: boolean;
} }
type ProcessRequestOptions = Omit<CreateHandlerOptions, 'parse_body'>
// eslint-disable-next-line max-lines-per-function // eslint-disable-next-line max-lines-per-function
function process_request ( function process_request (
request: AuthRequest, request: AuthRequest,
token: RegExpExecArray | null, token: RegExpExecArray | null,
default_handler: AuthRequestHandler, default_handler: AuthRequestHandler,
options?: CreateHandlerOptions options?: ProcessRequestOptions
): Promise<void> | void { ): Promise<void> | void {
if (token === null) if (token === null)
return default_handler (request); return default_handler (request);
@ -313,15 +316,17 @@ export default function create_auth_handler (
req: IncomingMessage, req: IncomingMessage,
res: ServerResponse res: ServerResponse
): Promise<boolean> => { ): Promise<boolean> => {
const body: string = await new Promise ((resolve) => { const body: string = options?.parse_body
let data = ''; ? await new Promise ((resolve) => {
req.on ('data', (c) => { let data = '';
data += c; req.on ('data', (c) => {
}); data += c;
req.on ('end', () => { });
resolve (data); req.on ('end', () => {
}); resolve (data);
}); });
})
: '';
const request = new AuthRequest ( const request = new AuthRequest (
req, req,

View File

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

View File

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