@@ -80,31 +80,7 @@ function encryptOld(userPublicKey, payload) {
80
80
} ;
81
81
}
82
82
83
- // Intermediate standard, Firefox 46.
84
- function encryptIntermediate ( userPublicKey , userAuth , payload ) {
85
- var localCurve = crypto . createECDH ( 'prime256v1' ) ;
86
- var localPublicKey = localCurve . generateKeys ( ) ;
87
-
88
- var salt = urlBase64 . encode ( crypto . randomBytes ( 16 ) ) ;
89
-
90
- ece . saveKey ( 'webpushKey' , localCurve , 'P-256' ) ;
91
-
92
- var cipherText = ece . encrypt ( payload , {
93
- keyid : 'webpushKey' ,
94
- dh : userPublicKey ,
95
- salt : salt ,
96
- authSecret : userAuth ,
97
- padSize : 1 ,
98
- } ) ;
99
-
100
- return {
101
- localPublicKey : localPublicKey ,
102
- salt : salt ,
103
- cipherText : cipherText ,
104
- } ;
105
- }
106
-
107
- // New standard, Firefox 47+ and Chrome 50+.
83
+ // New standard, Firefox 46+ and Chrome 50+.
108
84
function encrypt ( userPublicKey , userAuth , payload ) {
109
85
var localCurve = crypto . createECDH ( 'prime256v1' ) ;
110
86
var localPublicKey = localCurve . generateKeys ( ) ;
@@ -159,8 +135,8 @@ function sendNotification(endpoint, params) {
159
135
if ( userAuth ) {
160
136
if ( typeof userAuth !== 'string' ) {
161
137
throw new Error ( 'userAuth should be a base64-encoded string.' ) ;
162
- } else if ( urlBase64 . decode ( userAuth ) . length < 12 ) {
163
- throw new Error ( 'userAuth should be at least 12 bytes long' ) ;
138
+ } else if ( urlBase64 . decode ( userAuth ) . length < 16 ) {
139
+ throw new Error ( 'userAuth should be at least 16 bytes long' ) ;
164
140
}
165
141
}
166
142
@@ -177,47 +153,34 @@ function sendNotification(endpoint, params) {
177
153
}
178
154
} ;
179
155
180
- var encrypted ;
181
- var useCryptoKey = false ;
156
+ var requestPayload ;
182
157
if ( typeof payload !== 'undefined' ) {
158
+ var encrypted ;
183
159
var encodingHeader ;
184
-
160
+ var cryptoHeaderName ;
185
161
if ( userAuth ) {
186
- useCryptoKey = true ;
187
-
188
- var userAuthBuf = urlBase64 . decode ( userAuth ) ;
189
- if ( userAuthBuf . length === 16 ) {
190
- // Use the new standard if userAuth is defined and is 16 bytes long (Firefox 47+ and Chrome 50+).
191
- encrypted = encrypt ( userPublicKey , userAuth , new Buffer ( payload ) ) ;
192
- encodingHeader = 'aesgcm' ;
193
- } else {
194
- // Use the intermediate standard if userAuth is defined and is 12 bytes long (Firefox 46).
195
- encrypted = encryptIntermediate ( userPublicKey , userAuth , new Buffer ( payload ) ) ;
196
- encodingHeader = 'aesgcm128' ;
197
- }
162
+ // Use the new standard if userAuth is defined (Firefox 46+ and Chrome 50+).
163
+ encrypted = encrypt ( userPublicKey , userAuth , new Buffer ( payload ) ) ;
164
+ encodingHeader = 'aesgcm' ;
165
+ cryptoHeaderName = 'Crypto-Key' ;
198
166
} else {
199
- // Use the old standard if userAuth isn't defined (Firefox 45).
167
+ // Use the old standard if userAuth isn't defined (up to Firefox 45).
200
168
encrypted = encryptOld ( userPublicKey , new Buffer ( payload ) ) ;
201
169
encodingHeader = 'aesgcm128' ;
170
+ cryptoHeaderName = 'Encryption-Key' ;
202
171
}
203
172
204
173
options . headers = {
205
- 'Content-Length' : encrypted . cipherText . length ,
206
174
'Content-Type' : 'application/octet-stream' ,
175
+ 'Content-Encoding' : encodingHeader ,
207
176
'Encryption' : 'keyid=p256dh;salt=' + encrypted . salt ,
208
177
} ;
209
178
210
- var cryptoHeader = 'keyid=p256dh;dh=' + urlBase64 . encode ( encrypted . localPublicKey ) ;
179
+ options . headers [ cryptoHeaderName ] = 'keyid=p256dh;dh=' + urlBase64 . encode ( encrypted . localPublicKey ) ;
211
180
212
- if ( useCryptoKey ) {
213
- options . headers [ 'Crypto-Key' ] = cryptoHeader ;
214
- } else {
215
- options . headers [ 'Encryption-Key' ] = cryptoHeader ;
216
- }
217
- options . headers [ 'Content-Encoding' ] = encodingHeader ;
181
+ requestPayload = encrypted . cipherText ;
218
182
}
219
183
220
- var gcmPayload ;
221
184
if ( isGCM ) {
222
185
if ( ! gcmAPIKey ) {
223
186
console . warn ( 'Attempt to send push notification to GCM endpoint, but no GCM key is defined' . bold . red ) ;
@@ -229,19 +192,18 @@ function sendNotification(endpoint, params) {
229
192
var gcmObj = {
230
193
registration_ids : [ subscriptionId ] ,
231
194
} ;
232
- if ( encrypted ) {
233
- gcmObj [ 'raw_data' ] = encrypted . cipherText . toString ( 'base64' ) ;
195
+ if ( requestPayload ) {
196
+ gcmObj [ 'raw_data' ] = requestPayload . toString ( 'base64' ) ;
234
197
}
235
- gcmPayload = JSON . stringify ( gcmObj ) ;
198
+ requestPayload = JSON . stringify ( gcmObj ) ;
236
199
237
200
options . path = options . path . substring ( 0 , options . path . length - subscriptionId . length - 1 ) ;
238
201
239
202
options . headers [ 'Authorization' ] = 'key=' + gcmAPIKey ;
240
203
options . headers [ 'Content-Type' ] = 'application/json' ;
241
- options . headers [ 'Content-Length' ] = gcmPayload . length ;
242
204
}
243
205
244
- if ( vapid && ! isGCM && ( ! encrypted || useCryptoKey ) ) {
206
+ if ( vapid && ! isGCM && ( typeof payload === 'undefined' || 'Crypto-Key' in options . headers ) ) {
245
207
// VAPID isn't supported by GCM.
246
208
// We also can't use it when there's a payload on Firefox 45, because
247
209
// Firefox 45 uses the old standard with Encryption-Key.
@@ -278,6 +240,10 @@ function sendNotification(endpoint, params) {
278
240
options . headers [ 'TTL' ] = 2419200 ; // Default TTL is four weeks.
279
241
}
280
242
243
+ if ( requestPayload ) {
244
+ options . headers [ 'Content-Length' ] = requestPayload . length ;
245
+ }
246
+
281
247
var expectedStatusCode = isGCM ? 200 : 201 ;
282
248
var pushRequest = https . request ( options , function ( pushResponse ) {
283
249
var body = "" ;
@@ -295,10 +261,8 @@ function sendNotification(endpoint, params) {
295
261
} ) ;
296
262
} ) ;
297
263
298
- if ( isGCM ) {
299
- pushRequest . write ( gcmPayload ) ;
300
- } else if ( typeof payload !== 'undefined' ) {
301
- pushRequest . write ( encrypted . cipherText ) ;
264
+ if ( requestPayload ) {
265
+ pushRequest . write ( requestPayload ) ;
302
266
}
303
267
304
268
pushRequest . end ( ) ;
0 commit comments