Skip to content

Commit 4ec6ea2

Browse files
author
Matt Gaunt
committed
Changing vapid input / output to be base64url encoded string
1 parent 251c28d commit 4ec6ea2

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/vapid-helper.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function generateVAPIDKeys() {
3232
curve.generateKeys();
3333

3434
return {
35-
publicKey: curve.getPublicKey(),
36-
privateKey: curve.getPrivateKey()
35+
publicKey: urlBase64.encode(curve.getPublicKey()),
36+
privateKey: urlBase64.encode(curve.getPrivateKey())
3737
};
3838
}
3939

@@ -84,24 +84,30 @@ function getVapidHeaders(audience, subject, publicKey, privateKey, expiration) {
8484
throw new Error('No key set vapid.publicKey');
8585
}
8686

87-
if (!(publicKey instanceof Buffer)) {
88-
throw new Error('Vapid public key is not a buffer.');
87+
if (typeof publicKey !== 'string') {
88+
throw new Error('Vapid public key is must be a URL safe Base 64 ' +
89+
'encoded string.');
8990
}
9091

92+
publicKey = urlBase64.decode(publicKey);
93+
9194
if (publicKey.length !== 65) {
92-
throw new Error('Vapid public key should be 65 bytes long');
95+
throw new Error('Vapid public key should be 65 bytes long when decoded.');
9396
}
9497

9598
if (!privateKey) {
9699
throw new Error('No key set in vapid.privateKey');
97100
}
98101

99-
if (!(privateKey instanceof Buffer)) {
100-
throw new Error('Vapid private key is not a buffer');
102+
if (typeof privateKey !== 'string') {
103+
throw new Error('Vapid private key must be a URL safe Base 64 ' +
104+
'encoded string.');
101105
}
102106

107+
privateKey = urlBase64.decode(privateKey);
108+
103109
if (privateKey.length !== 32) {
104-
throw new Error('Vapid private key should be 32 bytes long');
110+
throw new Error('Vapid private key should be 32 bytes long when decoded.');
105111
}
106112

107113
if (expiration) {

test/test-vapid-helper.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
'use strict';
22

33
const assert = require('assert');
4+
const urlBase64 = require('urlsafe-base64');
45
const webPush = require('../src/index');
56
const vapidHelper = require('../src/vapid-helper');
67

78
const VALID_AUDIENCE = 'https://example.com';
89
const VALID_SUBJECT_MAILTO = 'mailto: [email protected]';
910
const VALID_SUBJECT_URL = 'https://exampe.com/contact';
10-
const VALID_PUBLIC_KEY = new Buffer(65);
11-
const VALID_PRIVATE_KEY = new Buffer(32);
11+
const VALID_PUBLIC_KEY = urlBase64.encode(new Buffer(65));
12+
const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
1213
const VALID_EXPIRATION = Math.floor(Date.now() / 1000) + (60 * 60 * 12);
1314

1415
suite('Test Vapid Helpers', function() {
@@ -21,11 +22,11 @@ suite('Test Vapid Helpers', function() {
2122
assert(keys.privateKey);
2223
assert(keys.publicKey);
2324

24-
assert.equal(keys.privateKey instanceof Buffer, true);
25-
assert.equal(keys.publicKey instanceof Buffer, true);
25+
assert.equal(typeof keys.privateKey, 'string');
26+
assert.equal(typeof keys.publicKey, 'string');
2627

27-
assert.equal(keys.privateKey.length, 32);
28-
assert.equal(keys.publicKey.length, 65);
28+
assert.equal(urlBase64.decode(keys.privateKey).length, 32);
29+
assert.equal(urlBase64.decode(keys.publicKey).length, 65);
2930
});
3031

3132
test('generate new vapid keys between calls', function() {

test/testSelenium.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
}
99

1010
/* eslint-disable global-require */
11-
const urlBase64 = require('urlsafe-base64');
1211
const seleniumAssistant = require('selenium-assistant');
1312
const webdriver = require('selenium-webdriver');
1413
const seleniumFirefox = require('selenium-webdriver/firefox');
@@ -26,11 +25,13 @@
2625
require('chromedriver');
2726
/* eslint-enable global-require */
2827

28+
const vapidKeys = webPush.generateVAPIDKeys();
29+
2930
const PUSH_TEST_TIMEOUT = 120 * 1000;
3031
const VAPID_PARAM = {
3132
subject: 'mailto:[email protected]',
32-
privateKey: new Buffer('H6tqEMswzHOFlPHFi2JPfDQRiKN32ZJIwvSPWZl1VTA=', 'base64'),
33-
publicKey: new Buffer('BIx6khu9Z/5lBwNEXYNEOQiL70IKYDpDxsTyoiCb82puQ/V4c/NFdyrBFpWdsz3mikmV6sWARNuhRbbbLTMOmB0=', 'base64')
33+
privateKey: vapidKeys.privateKey,
34+
publicKey: vapidKeys.publicKey
3435
};
3536
const testDirectory = './test/output/';
3637

@@ -119,7 +120,7 @@
119120
globalDriver = driver;
120121

121122
if (options.vapid) {
122-
testServerURL += '?vapid=' + urlBase64.encode(options.vapid.publicKey);
123+
testServerURL += '?vapid=' + options.vapid.publicKey;
123124
}
124125

125126
return globalDriver.get(testServerURL)

0 commit comments

Comments
 (0)