@@ -7,6 +7,7 @@ const https = require('https');
77const WebPushError = require ( './web-push-error.js' ) ;
88const vapidHelper = require ( './vapid-helper.js' ) ;
99const encryptionHelper = require ( './encryption-helper.js' ) ;
10+ const webPushConstants = require ( './web-push-constants.js' ) ;
1011
1112// Default TTL is four weeks.
1213const DEFAULT_TTL = 2419200 ;
@@ -107,13 +108,15 @@ WebPushLib.prototype.generateRequestDetails =
107108 let currentVapidDetails = vapidDetails ;
108109 let timeToLive = DEFAULT_TTL ;
109110 let extraHeaders = { } ;
111+ let contentEncoding = webPushConstants . supportedContentEncodings . AES_GCM ;
110112
111113 if ( options ) {
112114 const validOptionKeys = [
113115 'headers' ,
114116 'gcmAPIKey' ,
115117 'vapidDetails' ,
116- 'TTL'
118+ 'TTL' ,
119+ 'contentEncoding'
117120 ] ;
118121 const optionKeys = Object . keys ( options ) ;
119122 for ( let i = 0 ; i < optionKeys . length ; i += 1 ) {
@@ -150,6 +153,15 @@ WebPushLib.prototype.generateRequestDetails =
150153 if ( options . TTL ) {
151154 timeToLive = options . TTL ;
152155 }
156+
157+ if ( options . contentEncoding ) {
158+ if ( ( options . contentEncoding === webPushConstants . supportedContentEncodings . AES_128_GCM
159+ || options . contentEncoding === webPushConstants . supportedContentEncodings . AES_GCM ) ) {
160+ contentEncoding = options . contentEncoding ;
161+ } else {
162+ throw new Error ( 'Unsupported content encoding specified.' ) ;
163+ }
164+ }
153165 }
154166
155167 if ( typeof timeToLive === 'undefined' ) {
@@ -177,13 +189,19 @@ WebPushLib.prototype.generateRequestDetails =
177189 'required encryption keys' ) ) ;
178190 }
179191
180- const encrypted = encryptionHelper . encrypt ( subscription . keys . p256dh , subscription . keys . auth , payload ) ;
192+ const encrypted = encryptionHelper
193+ . encrypt ( subscription . keys . p256dh , subscription . keys . auth , payload , contentEncoding ) ;
181194
182195 requestDetails . headers [ 'Content-Length' ] = encrypted . cipherText . length ;
183196 requestDetails . headers [ 'Content-Type' ] = 'application/octet-stream' ;
184- requestDetails . headers [ 'Content-Encoding' ] = 'aesgcm' ;
185- requestDetails . headers . Encryption = 'salt=' + encrypted . salt ;
186- requestDetails . headers [ 'Crypto-Key' ] = 'dh=' + urlBase64 . encode ( encrypted . localPublicKey ) ;
197+
198+ if ( contentEncoding === webPushConstants . supportedContentEncodings . AES_128_GCM ) {
199+ requestDetails . headers [ 'Content-Encoding' ] = webPushConstants . supportedContentEncodings . AES_128_GCM ;
200+ } else if ( contentEncoding === webPushConstants . supportedContentEncodings . AES_GCM ) {
201+ requestDetails . headers [ 'Content-Encoding' ] = webPushConstants . supportedContentEncodings . AES_GCM ;
202+ requestDetails . headers . Encryption = 'salt=' + encrypted . salt ;
203+ requestDetails . headers [ 'Crypto-Key' ] = 'dh=' + urlBase64 . encode ( encrypted . localPublicKey ) ;
204+ }
187205
188206 requestPayload = encrypted . cipherText ;
189207 } else {
@@ -201,6 +219,10 @@ WebPushLib.prototype.generateRequestDetails =
201219 requestDetails . headers . Authorization = 'key=' + currentGCMAPIKey ;
202220 }
203221 } else if ( currentVapidDetails ) {
222+ if ( contentEncoding === webPushConstants . supportedContentEncodings . AES_128_GCM
223+ && subscription . endpoint . indexOf ( 'https://fcm.googleapis.com' ) === 0 ) {
224+ subscription . endpoint = subscription . endpoint . replace ( 'fcm/send' , 'wp' ) ;
225+ }
204226 const parsedUrl = url . parse ( subscription . endpoint ) ;
205227 const audience = parsedUrl . protocol + '//' +
206228 parsedUrl . host ;
@@ -209,15 +231,19 @@ WebPushLib.prototype.generateRequestDetails =
209231 audience ,
210232 currentVapidDetails . subject ,
211233 currentVapidDetails . publicKey ,
212- currentVapidDetails . privateKey
234+ currentVapidDetails . privateKey ,
235+ contentEncoding
213236 ) ;
214237
215238 requestDetails . headers . Authorization = vapidHeaders . Authorization ;
216- if ( requestDetails . headers [ 'Crypto-Key' ] ) {
217- requestDetails . headers [ 'Crypto-Key' ] += ';' +
218- vapidHeaders [ 'Crypto-Key' ] ;
219- } else {
220- requestDetails . headers [ 'Crypto-Key' ] = vapidHeaders [ 'Crypto-Key' ] ;
239+
240+ if ( contentEncoding === webPushConstants . supportedContentEncodings . AES_GCM ) {
241+ if ( requestDetails . headers [ 'Crypto-Key' ] ) {
242+ requestDetails . headers [ 'Crypto-Key' ] += ';' +
243+ vapidHeaders [ 'Crypto-Key' ] ;
244+ } else {
245+ requestDetails . headers [ 'Crypto-Key' ] = vapidHeaders [ 'Crypto-Key' ] ;
246+ }
221247 }
222248 }
223249
0 commit comments