Skip to content

Commit 5a2ea80

Browse files
committed
fix: encode client_secret_basic - _ . ! ~ * ' ( ) characters
Because encodeURIComponent() encodes everything except alphanumericals and `- _ . ! ~ * ' ( )` these need to be encoded explicitly similar to how the resulting `%20' is replaced with '+' This is as per RFC6749 Section 2.3.1 and Appendix B
1 parent 9d3cfb8 commit 5a2ea80

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/helpers/client.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,26 @@ const request = require('./request');
99
const { keystores } = require('./weak_cache');
1010
const merge = require('./merge');
1111

12-
const formUrlEncode = (value) => encodeURIComponent(value).replace(/%20/g, '+');
12+
function formUrlEncode(token) {
13+
return encodeURIComponent(token).replace(/(?:[-_.!~*'()]|%20)/g, (substring) => {
14+
switch (substring) {
15+
case '-':
16+
case '_':
17+
case '.':
18+
case '!':
19+
case '~':
20+
case '*':
21+
case "'":
22+
case '(':
23+
case ')':
24+
return `%${substring.charCodeAt(0).toString(16).toUpperCase()}`;
25+
case '%20':
26+
return '+';
27+
default:
28+
throw new Error();
29+
}
30+
});
31+
}
1332

1433
async function clientAssertion(endpoint, payload) {
1534
let alg = this[`${endpoint}_endpoint_auth_signing_alg`];

test/client/client_instance.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,7 @@ describe('Client', () => {
22742274
expect(await clientInternal.authFor.call(client, 'token')).to.eql({
22752275
headers: {
22762276
Authorization:
2277-
'Basic YW4lM0FpZGVudGlmaWVyOnNvbWUrc2VjdXJlKyUyNitub24tc3RhbmRhcmQrc2VjcmV0',
2277+
'Basic YW4lM0FpZGVudGlmaWVyOnNvbWUrc2VjdXJlKyUyNitub24lMkRzdGFuZGFyZCtzZWNyZXQ=',
22782278
},
22792279
});
22802280
});

0 commit comments

Comments
 (0)