From a19ea4e7bf367f5d44035b0751b53337c2723cb5 Mon Sep 17 00:00:00 2001 From: andrei-cdl Date: Mon, 9 Aug 2021 17:12:22 -0400 Subject: [PATCH] feat: add support for URL safe encoding due to a bug in node.js some clients will end up encoding the value before base64 which results in the incorrect value being parsed. ref: https://github.com/nodejs/node/issues/31439 --- index.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 9106e64..bef766d 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ var USER_PASS_REGEXP = /^([^:]*):(.*)$/ * @public */ -function auth (req) { +function auth (req, isURI = false) { if (!req) { throw new TypeError('argument req is required') } @@ -66,7 +66,7 @@ function auth (req) { var header = getAuthorization(req) // parse header - return parse(header) + return parse(header, isURI) } /** @@ -99,12 +99,10 @@ function getAuthorization (req) { * @public */ -function parse (string) { +function parse (string, isURI = false) { if (typeof string !== 'string') { return undefined - } - - // parse header + } // parse header var match = CREDENTIALS_REGEXP.exec(string) if (!match) { @@ -112,7 +110,13 @@ function parse (string) { } // decode user pass - var userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1])) + let userPass = null; + + if (isURI) { + var userPass = USER_PASS_REGEXP.exec(decodeURIComponent(decodeBase64(match[1]))) + } else { + var userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1])) + } if (!userPass) { return undefined