Skip to content

Commit b702b9e

Browse files
committed
Simplify handling of the different standards
1 parent cd39611 commit b702b9e

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

index.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,36 +154,30 @@ function sendNotification(endpoint, params) {
154154
};
155155

156156
var encrypted;
157-
var useCryptoKey = false;
158157
if (typeof payload !== 'undefined') {
159158
var encodingHeader;
159+
var cryptoHeaderName;
160160

161161
if (userAuth) {
162-
useCryptoKey = true;
163-
164162
// Use the new standard if userAuth is defined (Firefox 46+ and Chrome 50+).
165163
encrypted = encrypt(userPublicKey, userAuth, new Buffer(payload));
166164
encodingHeader = 'aesgcm';
165+
cryptoHeaderName = 'Crypto-Key';
167166
} else {
168167
// Use the old standard if userAuth isn't defined (up to Firefox 45).
169168
encrypted = encryptOld(userPublicKey, new Buffer(payload));
170169
encodingHeader = 'aesgcm128';
170+
cryptoHeaderName = 'Encryption-Key';
171171
}
172172

173173
options.headers = {
174174
'Content-Length': encrypted.cipherText.length,
175175
'Content-Type': 'application/octet-stream',
176+
'Content-Encoding': encodingHeader,
176177
'Encryption': 'keyid=p256dh;salt=' + encrypted.salt,
177178
};
178179

179-
var cryptoHeader = 'keyid=p256dh;dh=' + urlBase64.encode(encrypted.localPublicKey);
180-
181-
if (useCryptoKey) {
182-
options.headers['Crypto-Key'] = cryptoHeader;
183-
} else {
184-
options.headers['Encryption-Key'] = cryptoHeader;
185-
}
186-
options.headers['Content-Encoding'] = encodingHeader;
180+
options.headers[cryptoHeaderName] = 'keyid=p256dh;dh=' + urlBase64.encode(encrypted.localPublicKey);
187181
}
188182

189183
var gcmPayload;
@@ -210,7 +204,7 @@ function sendNotification(endpoint, params) {
210204
options.headers['Content-Length'] = gcmPayload.length;
211205
}
212206

213-
if (vapid && !isGCM && (!encrypted || useCryptoKey)) {
207+
if (vapid && !isGCM && (typeof payload === 'undefined' || 'Crypto-Key' in options.headers)) {
214208
// VAPID isn't supported by GCM.
215209
// We also can't use it when there's a payload on Firefox 45, because
216210
// Firefox 45 uses the old standard with Encryption-Key.

0 commit comments

Comments
 (0)