1
1
/* Javascript VAPID library.
2
2
*
3
- * Requires: common.js :: mzcc
3
+ * Requires: common.js
4
4
*
5
5
*/
6
6
14
14
var webCrypto = window . crypto . subtle ;
15
15
}
16
16
17
-
18
17
class VapidToken {
19
- constructor ( aud , sub , exp , lang ) {
18
+ constructor ( aud , sub , exp , lang , mzcc ) {
20
19
/* Construct a base VAPID token.
21
20
*
22
21
* VAPID allows for self identification of a subscription update.
@@ -26,6 +25,11 @@ class VapidToken {
26
25
* :param exp: Expiration - UTC expiration of this update. Defaults
27
26
* to now + 24 hours
28
27
*/
28
+
29
+ if ( mzcc == undefined ) {
30
+ mzcc = new MozCommon ( ) ;
31
+ }
32
+ this . mzcc = mzcc ;
29
33
this . _claims = { } ;
30
34
this . _claims [ 'aud' ] = aud || "" ;
31
35
if ( sub !== undefined ) {
@@ -88,9 +92,9 @@ class VapidToken {
88
92
*/
89
93
return webCrypto . exportKey ( 'jwk' , this . _public_key )
90
94
. then ( key => {
91
- return mzcc . toUrlBase64 ( "\x04" +
92
- mzcc . fromUrlBase64 ( key . x ) +
93
- mzcc . fromUrlBase64 ( key . y ) )
95
+ return this . mzcc . toUrlBase64 ( "\x04" +
96
+ this . mzcc . fromUrlBase64 ( key . x ) +
97
+ this . mzcc . fromUrlBase64 ( key . y ) )
94
98
} )
95
99
. catch ( err => {
96
100
console . error ( "public raw format" , err ) ;
@@ -105,7 +109,7 @@ class VapidToken {
105
109
* :returns: a promise from the imported key.
106
110
*/
107
111
if ( typeof ( raw ) == "string" ) {
108
- raw = mzcc . _strToArray ( mzcc . fromUrlBase64 ( raw ) ) ;
112
+ raw = this . mzcc . strToArray ( this . mzcc . fromUrlBase64 ( raw ) ) ;
109
113
}
110
114
let err = new Error ( this . lang . errs . ERR_PUB_KEY ) ;
111
115
@@ -115,9 +119,9 @@ class VapidToken {
115
119
}
116
120
117
121
raw = raw . slice ( - 64 ) ;
118
- let x = mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
122
+ let x = this . mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
119
123
raw . slice ( 0 , 32 ) ) ) ;
120
- let y = mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
124
+ let y = this . mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
121
125
raw . slice ( 32 , 64 ) ) ) ;
122
126
123
127
// Convert to a JWK and import it.
@@ -139,7 +143,7 @@ class VapidToken {
139
143
sign ( claims ) {
140
144
/* Sign a claims object and return the headers that can be used to
141
145
* decrypt the string.
142
- * *
146
+ *
143
147
* :param claims: An object containing the VAPID claims.
144
148
* :returns: a promise containing an object identifying the headers
145
149
* and values to include to specify VAPID auth.
@@ -157,18 +161,19 @@ class VapidToken {
157
161
throw new Error ( this . lang . errs . ERR_CLAIM_MIS , "aud" ) ;
158
162
}
159
163
let alg = { name :"ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
160
- let headStr = mzcc . toUrlBase64 (
164
+ let headStr = this . mzcc . toUrlBase64 (
161
165
JSON . stringify ( { typ :"JWT" , alg :"ES256" } ) ) ;
162
- let claimStr = mzcc . toUrlBase64 (
166
+ let claimStr = this . mzcc . toUrlBase64 (
163
167
JSON . stringify ( claims ) ) ;
164
168
let content = headStr + "." + claimStr ;
165
- let signatory = mzcc . _strToArray ( content ) ;
169
+ let signatory = this . mzcc . strToArray ( content ) ;
166
170
return webCrypto . sign (
167
171
alg ,
168
172
this . _private_key ,
169
173
signatory )
170
174
. then ( signature => {
171
- let sig = mzcc . toUrlBase64 ( mzcc . _arrayToStr ( signature ) ) ;
175
+ let sig = this . mzcc . toUrlBase64 (
176
+ this . mzcc . arrayToStr ( signature ) ) ;
172
177
/* The headers consist of the constructed JWT as the
173
178
* "authorization" and the raw Public key as the p256ecdsa
174
179
* element of "Crypto-Key"
@@ -236,17 +241,18 @@ class VapidToken {
236
241
let signature ;
237
242
let key ;
238
243
try {
239
- signature = mzcc . _strToArray ( mzcc . fromUrlBase64 ( items [ 2 ] ) ) ;
244
+ signature = this . mzcc . strToArray (
245
+ this . mzcc . fromUrlBase64 ( items [ 2 ] ) ) ;
240
246
} catch ( err ) {
241
247
throw new Error ( this . lang . errs . ERR_VERIFY_SG + err . message ) ;
242
248
}
243
249
try {
244
- key = mzcc . _strToArray ( mzcc . fromUrlBase64 ( items [ 1 ] ) ) ;
250
+ key = this . mzcc . strToArray ( this . mzcc . fromUrlBase64 ( items [ 1 ] ) ) ;
245
251
} catch ( err ) {
246
252
throw new Error ( this . lang . errs . ERR_VERIFY_KE + err . message ) ;
247
253
}
248
254
let content = items . slice ( 0 , 2 ) . join ( '.' ) ;
249
- let signatory = mzcc . _strToArray ( content ) ;
255
+ let signatory = this . mzcc . strToArray ( content ) ;
250
256
return webCrypto . verify (
251
257
alg ,
252
258
this . _public_key ,
@@ -257,7 +263,8 @@ class VapidToken {
257
263
return JSON . parse (
258
264
String . fromCharCode . apply (
259
265
null ,
260
- mzcc . _strToArray ( mzcc . fromUrlBase64 ( items [ 1 ] ) ) ) )
266
+ this . mzcc . strToArray (
267
+ this . mzcc . fromUrlBase64 ( items [ 1 ] ) ) ) )
261
268
}
262
269
throw new Error ( this . lang . errs . ERR_SIGNATURE ) ;
263
270
} )
@@ -285,10 +292,10 @@ class VapidToken {
285
292
* :returns: the signature value to paste back into the Dashboard.
286
293
*/
287
294
let alg = { name :"ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
288
- let t2v = mzcc . _strToArray ( string ) ;
295
+ let t2v = this . mzcc . strToArray ( string ) ;
289
296
return webCrypto . sign ( alg , this . _private_key , t2v )
290
297
. then ( signed => {
291
- let sig = mzcc . toUrlBase64 ( mzcc . _arrayToStr ( signed ) ) ;
298
+ let sig = this . mzcc . toUrlBase64 ( this . mzcc . arrayToStr ( signed ) ) ;
292
299
return sig ;
293
300
} ) ;
294
301
}
@@ -303,8 +310,8 @@ class VapidToken {
303
310
* :returns: Boolean indicating successful verification.
304
311
*/
305
312
let alg = { name : "ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
306
- let vsig = mzcc . _strToArray ( mzcc . fromUrlBase64 ( sig ) ) ;
307
- let t2v = mzcc . _strToArray ( mzcc . fromUrlBase64 ( string ) ) ;
313
+ let vsig = this . mzcc . strToArray ( this . mzcc . fromUrlBase64 ( sig ) ) ;
314
+ let t2v = this . mzcc . strToArray ( this . mzcc . fromUrlBase64 ( string ) ) ;
308
315
return webCrypto . verify ( alg , this . _public_key , vsig , t2v ) ;
309
316
}
310
317
}
0 commit comments