11/* Javascript VAPID library.
22 *
3- * Requires: common.js :: mzcc
3+ * Requires: common.js
44 *
55 */
66
1414 var webCrypto = window . crypto . subtle ;
1515}
1616
17-
1817class VapidToken {
19- constructor ( aud , sub , exp , lang ) {
18+ constructor ( aud , sub , exp , lang , mzcc ) {
2019 /* Construct a base VAPID token.
2120 *
2221 * VAPID allows for self identification of a subscription update.
@@ -26,6 +25,11 @@ class VapidToken {
2625 * :param exp: Expiration - UTC expiration of this update. Defaults
2726 * to now + 24 hours
2827 */
28+
29+ if ( mzcc == undefined ) {
30+ mzcc = new MozCommon ( ) ;
31+ }
32+ this . mzcc = mzcc ;
2933 this . _claims = { } ;
3034 this . _claims [ 'aud' ] = aud || "" ;
3135 if ( sub !== undefined ) {
@@ -88,9 +92,9 @@ class VapidToken {
8892 */
8993 return webCrypto . exportKey ( 'jwk' , this . _public_key )
9094 . 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 ) )
9498 } )
9599 . catch ( err => {
96100 console . error ( "public raw format" , err ) ;
@@ -105,7 +109,7 @@ class VapidToken {
105109 * :returns: a promise from the imported key.
106110 */
107111 if ( typeof ( raw ) == "string" ) {
108- raw = mzcc . _strToArray ( mzcc . fromUrlBase64 ( raw ) ) ;
112+ raw = this . mzcc . strToArray ( this . mzcc . fromUrlBase64 ( raw ) ) ;
109113 }
110114 let err = new Error ( this . lang . errs . ERR_PUB_KEY ) ;
111115
@@ -115,9 +119,9 @@ class VapidToken {
115119 }
116120
117121 raw = raw . slice ( - 64 ) ;
118- let x = mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
122+ let x = this . mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
119123 raw . slice ( 0 , 32 ) ) ) ;
120- let y = mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
124+ let y = this . mzcc . toUrlBase64 ( String . fromCharCode . apply ( null ,
121125 raw . slice ( 32 , 64 ) ) ) ;
122126
123127 // Convert to a JWK and import it.
@@ -139,7 +143,7 @@ class VapidToken {
139143 sign ( claims ) {
140144 /* Sign a claims object and return the headers that can be used to
141145 * decrypt the string.
142- * *
146+ *
143147 * :param claims: An object containing the VAPID claims.
144148 * :returns: a promise containing an object identifying the headers
145149 * and values to include to specify VAPID auth.
@@ -157,18 +161,19 @@ class VapidToken {
157161 throw new Error ( this . lang . errs . ERR_CLAIM_MIS , "aud" ) ;
158162 }
159163 let alg = { name :"ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
160- let headStr = mzcc . toUrlBase64 (
164+ let headStr = this . mzcc . toUrlBase64 (
161165 JSON . stringify ( { typ :"JWT" , alg :"ES256" } ) ) ;
162- let claimStr = mzcc . toUrlBase64 (
166+ let claimStr = this . mzcc . toUrlBase64 (
163167 JSON . stringify ( claims ) ) ;
164168 let content = headStr + "." + claimStr ;
165- let signatory = mzcc . _strToArray ( content ) ;
169+ let signatory = this . mzcc . strToArray ( content ) ;
166170 return webCrypto . sign (
167171 alg ,
168172 this . _private_key ,
169173 signatory )
170174 . then ( signature => {
171- let sig = mzcc . toUrlBase64 ( mzcc . _arrayToStr ( signature ) ) ;
175+ let sig = this . mzcc . toUrlBase64 (
176+ this . mzcc . arrayToStr ( signature ) ) ;
172177 /* The headers consist of the constructed JWT as the
173178 * "authorization" and the raw Public key as the p256ecdsa
174179 * element of "Crypto-Key"
@@ -236,17 +241,18 @@ class VapidToken {
236241 let signature ;
237242 let key ;
238243 try {
239- signature = mzcc . _strToArray ( mzcc . fromUrlBase64 ( items [ 2 ] ) ) ;
244+ signature = this . mzcc . strToArray (
245+ this . mzcc . fromUrlBase64 ( items [ 2 ] ) ) ;
240246 } catch ( err ) {
241247 throw new Error ( this . lang . errs . ERR_VERIFY_SG + err . message ) ;
242248 }
243249 try {
244- key = mzcc . _strToArray ( mzcc . fromUrlBase64 ( items [ 1 ] ) ) ;
250+ key = this . mzcc . strToArray ( this . mzcc . fromUrlBase64 ( items [ 1 ] ) ) ;
245251 } catch ( err ) {
246252 throw new Error ( this . lang . errs . ERR_VERIFY_KE + err . message ) ;
247253 }
248254 let content = items . slice ( 0 , 2 ) . join ( '.' ) ;
249- let signatory = mzcc . _strToArray ( content ) ;
255+ let signatory = this . mzcc . strToArray ( content ) ;
250256 return webCrypto . verify (
251257 alg ,
252258 this . _public_key ,
@@ -257,7 +263,8 @@ class VapidToken {
257263 return JSON . parse (
258264 String . fromCharCode . apply (
259265 null ,
260- mzcc . _strToArray ( mzcc . fromUrlBase64 ( items [ 1 ] ) ) ) )
266+ this . mzcc . strToArray (
267+ this . mzcc . fromUrlBase64 ( items [ 1 ] ) ) ) )
261268 }
262269 throw new Error ( this . lang . errs . ERR_SIGNATURE ) ;
263270 } )
@@ -285,10 +292,10 @@ class VapidToken {
285292 * :returns: the signature value to paste back into the Dashboard.
286293 */
287294 let alg = { name :"ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
288- let t2v = mzcc . _strToArray ( string ) ;
295+ let t2v = this . mzcc . strToArray ( string ) ;
289296 return webCrypto . sign ( alg , this . _private_key , t2v )
290297 . then ( signed => {
291- let sig = mzcc . toUrlBase64 ( mzcc . _arrayToStr ( signed ) ) ;
298+ let sig = this . mzcc . toUrlBase64 ( this . mzcc . arrayToStr ( signed ) ) ;
292299 return sig ;
293300 } ) ;
294301 }
@@ -303,8 +310,8 @@ class VapidToken {
303310 * :returns: Boolean indicating successful verification.
304311 */
305312 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 ) ) ;
308315 return webCrypto . verify ( alg , this . _public_key , vsig , t2v ) ;
309316 }
310317}
0 commit comments