Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

Commit

Permalink
Allow sending with no payload, plus some small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wibblymat committed Mar 31, 2016
1 parent bd407f5 commit 698ddc7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
2 changes: 0 additions & 2 deletions src/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,4 @@ function encryptPayload(plaintext, contentEncryptionKey, nonce) {
return Buffer.concat([result, cipher.getAuthTag()]);
}

// All functions are exported here to make them testable, but only `encrypt` is
// re-exported by `index.js` as part of the public API.
module.exports = encrypt;
33 changes: 19 additions & 14 deletions src/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'use strict';

const request = require('request');
const encrypt = require('./encrypt').encrypt;
const encrypt = require('./encrypt');

const GCM_URL = 'https://android.googleapis.com/gcm/send';
const TEMP_GCM_URL = 'https://gcm-http.googleapis.com/gcm';
Expand All @@ -35,34 +35,39 @@ function ub64(buffer) {

/**
* Sends a message using the Web Push protocol
* @param {String} message The message to send
* @param {Object} subscription The subscription details for the client we
* are sending to
* @param {String} authToken Optional token to be used in the
* `Authentication` header if the endpoint
* requires it.
* @param {String} message The message to send
* @param {Object} subscription The subscription details for the client we
* are sending to
* @param {String} authToken Optional token to be used in the
* `Authentication` header if the endpoint
* requires it.
* @param {Number} paddingLength The number of bytes of padding to add to the
* message before encrypting it.
* @return {Promise} A promise that resolves if the push was sent successfully
* with status and body.
*/
function sendWebPush(message, subscription, authToken) {
function sendWebPush(message, subscription, authToken, paddingLength) {
// If the endpoint is GCM then we temporarily need to rewrite it, as not all
// GCM servers support the Web Push protocol. This should go away in the
// future.
const endpoint = subscription.endpoint.replace(GCM_URL, TEMP_GCM_URL);
const headers = {};
let body;

const payload = encrypt(message, subscription);
const headers = {
'Encryption': `salt=${ub64(payload.salt)}`,
'Crypto-Key': `dh=${ub64(payload.serverPublicKey)}`
};
if (message && typeof message === 'string' && message.length > 0) {
const payload = encrypt(message, subscription, paddingLength);
headers.Encryption = `salt=${ub64(payload.salt)}`;
headers['Crypto-Key'] = `dh=${ub64(payload.serverPublicKey)}`;
body = payload.ciphertext;
}

if (authToken) {
headers.Authorization = `key=${authToken}`;
}

return new Promise(function(resolve, reject) {
request.post(endpoint, {
body: payload.ciphertext,
body: body,
headers: headers
}, function(error, response, body) {
if (error) {
Expand Down

0 comments on commit 698ddc7

Please sign in to comment.