Skip to content

Commit

Permalink
feat: add support for URL safe encoding
Browse files Browse the repository at this point in the history
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: nodejs/node#31439
  • Loading branch information
andrei-cdl committed Aug 9, 2021
1 parent e8a29f9 commit a19ea4e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -66,7 +66,7 @@ function auth (req) {
var header = getAuthorization(req)

// parse header
return parse(header)
return parse(header, isURI)
}

/**
Expand Down Expand Up @@ -99,20 +99,24 @@ 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) {
return undefined
}

// 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
Expand Down

0 comments on commit a19ea4e

Please # to comment.