Skip to content

Commit

Permalink
perf: use base64url encoding in node when available
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jan 18, 2021
1 parent 62488e8 commit 24ab5b4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const url = require('url');

const { ParseError } = require('got');
const jose = require('jose');
const base64url = require('base64url');
const tokenHash = require('oidc-token-hash');

const base64url = require('./helpers/base64url');
const defaults = require('./helpers/defaults');
const { assertSigningAlgValuesSupport, assertIssuerConfiguration } = require('./helpers/assert');
const pick = require('./helpers/pick');
Expand Down
12 changes: 12 additions & 0 deletions lib/helpers/base64url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let encode;
if (Buffer.isEncoding('base64url')) {
encode = (input, encoding = 'utf8') => Buffer.from(input, encoding).toString('base64url');
} else {
const fromBase64 = (base64) => base64.replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
encode = (input, encoding = 'utf8') => fromBase64(Buffer.from(input, encoding).toString('base64'));
}

const decode = (input) => Buffer.from(input, 'base64');

module.exports.decode = decode;
module.exports.encode = encode;
6 changes: 3 additions & 3 deletions lib/helpers/generators.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const { createHash, randomBytes } = require('crypto');

const { encode: base64url } = require('base64url');
const base64url = require('./base64url');

const random = (bytes = 32) => base64url(randomBytes(bytes));
const random = (bytes = 32) => base64url.encode(randomBytes(bytes));

module.exports = {
random,
state: random,
nonce: random,
codeVerifier: random,
codeChallenge: (codeVerifier) => base64url(createHash('sha256').update(codeVerifier).digest()),
codeChallenge: (codeVerifier) => base64url.encode(createHash('sha256').update(codeVerifier).digest()),
};
3 changes: 1 addition & 2 deletions lib/token_set.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const base64url = require('base64url');

const base64url = require('./helpers/base64url');
const now = require('./helpers/unix_timestamp');

class TokenSet {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@
]
},
"dependencies": {
"base64url": "^3.0.1",
"got": "^11.8.0",
"jose": "^2.0.2",
"jose": "^2.0.4",
"lru-cache": "^6.0.0",
"make-error": "^1.3.6",
"object-hash": "^2.0.1",
"oidc-token-hash": "^5.0.0",
"oidc-token-hash": "^5.0.1",
"p-any": "^3.0.0"
},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/passport": "^1.0.4",
"base64url": "^3.0.1",
"chai": "^4.2.0",
"eslint": "^7.12.1",
"eslint-config-airbnb-base": "^14.2.0",
Expand Down

0 comments on commit 24ab5b4

Please sign in to comment.