-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use builtin encoding
base64url
(#819)
Node 16+ supports base64url
- Loading branch information
Showing
10 changed files
with
58 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,13 @@ | ||
// Largely ported from https://github.com/RGBboy/urlsafe-base64 | ||
|
||
'use strict'; | ||
|
||
function encode(buffer) { | ||
return buffer.toString('base64') | ||
.replace(/\+/g, '-') // Convert '+' to '-' | ||
.replace(/\//g, '_') // Convert '/' to '_' | ||
.replace(/=+$/, ''); // Remove ending '=' | ||
} | ||
|
||
function decode(base64) { | ||
// Add removed at end '=' | ||
base64 += Array(5 - (base64.length % 4)).join('='); | ||
|
||
base64 = base64 | ||
.replace(/-/g, '+') // Convert '-' to '+' | ||
.replace(/_/g, '/'); // Convert '_' to '/' | ||
|
||
// change from urlsafe-base64 since new Buffer() is deprecated | ||
return Buffer.from(base64, 'base64'); | ||
} | ||
|
||
/** | ||
* @param {string} base64 | ||
* @returns {boolean} | ||
*/ | ||
function validate(base64) { | ||
return /^[A-Za-z0-9\-_]+$/.test(base64); | ||
} | ||
return /^[A-Za-z0-9\-_]+$/.test(base64); | ||
} | ||
|
||
module.exports = { | ||
encode: encode, | ||
decode: decode, | ||
validate: validate | ||
validate: validate | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,12 +2,11 @@ | |
|
||
const assert = require('assert'); | ||
const webPush = require('../src/index'); | ||
const urlBase64Helper = require('../src/urlsafe-base64-helper'); | ||
|
||
const VALID_SUBJECT_MAILTO = 'mailto: [email protected]'; | ||
const VALID_SUBJECT_URL = 'https://exampe.com/contact'; | ||
const VALID_PUBLIC_KEY = urlBase64Helper.encode(Buffer.alloc(65)); | ||
const VALID_PRIVATE_KEY = urlBase64Helper.encode(Buffer.alloc(32)); | ||
const VALID_PUBLIC_KEY = Buffer.alloc(65).toString('base64url'); | ||
const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); | ||
|
||
suite('setVapidDetails()', function() { | ||
test('is defined', function() { | ||
|
@@ -55,7 +54,7 @@ suite('setVapidDetails()', function() { | |
}, | ||
{ | ||
subject: VALID_SUBJECT_URL, | ||
publicKey: urlBase64Helper.encode(Buffer.alloc(60)), | ||
publicKey: Buffer.alloc(60).toString('base64url'), | ||
privateKey: VALID_PRIVATE_KEY | ||
}, | ||
{ | ||
|
@@ -81,7 +80,7 @@ suite('setVapidDetails()', function() { | |
{ | ||
subject: VALID_SUBJECT_URL, | ||
publicKey: VALID_PUBLIC_KEY, | ||
privateKey: urlBase64Helper.encode(Buffer.alloc(60)) | ||
privateKey: Buffer.alloc(60).toString('base64url') | ||
}, | ||
{ | ||
subject: VALID_SUBJECT_URL, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ const crypto = require('crypto'); | |
const mocha = require('mocha'); | ||
const webPush = require('../src/index'); | ||
const vapidHelper = require('../src/vapid-helper'); | ||
const urlBase64Helper = require('../src/urlsafe-base64-helper'); | ||
|
||
const VALID_AUDIENCE = 'https://example.com'; | ||
const VALID_SUBJECT_MAILTO = 'mailto:[email protected]'; | ||
|
@@ -15,9 +14,9 @@ const VALID_SUBJECT_URL = 'https://example.com/contact'; | |
const WARN_SUBJECT_LOCALHOST_URL = 'https://localhost'; | ||
const INVALID_SUBJECT_URL_1 = 'http://example.gov'; | ||
const INVALID_SUBJECT_URL_2 = 'ftp://example.net'; | ||
const VALID_PUBLIC_KEY = urlBase64Helper.encode(Buffer.alloc(65)); | ||
const VALID_PUBLIC_KEY = Buffer.alloc(65).toString('base64url'); | ||
const VALID_UNSAFE_BASE64_PUBLIC_KEY = Buffer.alloc(65).toString('base64'); | ||
const VALID_PRIVATE_KEY = urlBase64Helper.encode(Buffer.alloc(32)); | ||
const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); | ||
const VALID_UNSAFE_BASE64_PRIVATE_KEY = Buffer.alloc(32).toString('base64'); | ||
const VALID_CONTENT_ENCODING = webPush.supportedContentEncodings.AES_GCM; | ||
const VALID_EXPIRATION = Math.floor(Date.now() / 1000) + (60 * 60 * 12); | ||
|
@@ -46,8 +45,8 @@ suite('Test Vapid Helpers', function() { | |
assert.equal(typeof keys.privateKey, 'string'); | ||
assert.equal(typeof keys.publicKey, 'string'); | ||
|
||
assert.equal(urlBase64Helper.decode(keys.privateKey).length, 32); | ||
assert.equal(urlBase64Helper.decode(keys.publicKey).length, 65); | ||
assert.equal(Buffer.from(keys.privateKey, 'base64url').length, 32); | ||
assert.equal(Buffer.from(keys.publicKey, 'base64url').length, 65); | ||
}); | ||
|
||
test('generate vapid keys with padding', function() { | ||
|
@@ -66,8 +65,8 @@ suite('Test Vapid Helpers', function() { | |
assert.equal(typeof keys.privateKey, 'string'); | ||
assert.equal(typeof keys.publicKey, 'string'); | ||
|
||
assert.equal(urlBase64Helper.decode(keys.privateKey).length, 32); | ||
assert.equal(urlBase64Helper.decode(keys.publicKey).length, 65); | ||
assert.equal(Buffer.from(keys.privateKey, 'base64url').length, 32); | ||
assert.equal(Buffer.from(keys.publicKey, 'base64url').length, 65); | ||
}); | ||
|
||
test('generate new vapid keys between calls', function() { | ||
|
Oops, something went wrong.