1414 var webCrypto = window . crypto . subtle ;
1515}
1616
17- class VapidToken {
17+ class VapidCore {
1818 constructor ( aud , sub , exp , lang , mzcc ) {
1919 /* Construct a base VAPID token.
2020 *
@@ -131,16 +131,14 @@ class VapidToken {
131131 key_ops : [ "verify" ] ,
132132 kty : "EC" ,
133133 x : x ,
134- y, y
134+ y : y ,
135135 } ;
136136
137137 return webCrypto . importKey ( 'jwk' , jwk , 'ECDSA' , true , [ "verify" ] )
138138 . then ( k => this . _public_key = k )
139139 }
140140
141-
142-
143- sign ( claims ) {
141+ _sign ( claims ) {
144142 /* Sign a claims object and return the headers that can be used to
145143 * decrypt the string.
146144 *
@@ -186,9 +184,8 @@ class VapidToken {
186184 return this . export_public_raw ( )
187185 . then ( pubKey => {
188186 return {
189- authorization : "WebPush " + content + "." + sig ,
190- "crypto-key" : "p256ecdsa=" + pubKey ,
191- publicKey : pubKey ,
187+ jwt : content + "." + sig ,
188+ pubkey : pubKey ,
192189 }
193190 } )
194191 } )
@@ -197,7 +194,7 @@ class VapidToken {
197194 } )
198195 }
199196
200- verify ( token , public_key = null ) {
197+ _verify ( token ) {
201198 /* Verify a VAPID token.
202199 *
203200 * Token is the Authorization Header, Public Key is the Crypto-Key
@@ -207,33 +204,8 @@ class VapidToken {
207204 */
208205
209206 // Ideally, just the bearer token, Cheat a little to be nice to the dev.
210- scheme = token . toLowerCase ( ) . split ( " " ) [ 0 ]
211- if ( scheme == "bearer" || scheme == "webpush" ) {
212- token = token . split ( " " ) [ 1 ] ;
213- }
214-
215- // Again, ideally, just the p256ecdsa token.
216- if ( public_key != null ) {
217-
218- if ( public_key . search ( 'p256ecdsa' ) > - 1 ) {
219- let sc = / p 2 5 6 e c d s a = ( [ ^ ; , ] + ) / i;
220- public_key = sc . exec ( public_key ) [ 1 ] ;
221- }
222-
223- // If there's no public key already defined, load the public_key
224- // and try again.
225- return this . import_public_raw ( public_key )
226- . then ( key => {
227- this . _public_key = key ;
228- return this . verify ( token ) ;
229- } )
230- . catch ( err => {
231- console . error ( "Verify error" , err ) ;
232- throw err ;
233- } ) ;
234- }
235207 if ( this . _public_key == "" ) {
236- throw new Error ( this . lang . errs . ERR_NO_KEYS ) ;
208+ throw new Error ( this . lang . errs . ERR_NO_KEYS ) ;
237209 }
238210
239211 let alg = { name : "ECDSA" , namedCurve : "P-256" ,
@@ -316,3 +288,89 @@ class VapidToken {
316288 return webCrypto . verify ( alg , this . _public_key , vsig , t2v ) ;
317289 }
318290}
291+
292+ class VapidToken01 extends VapidCore {
293+
294+ sign ( claims ) {
295+ return this . _sign ( claims )
296+ . then ( elements => {
297+ return {
298+ authorization : "WebPush " + elements . jwt ,
299+ "crypto-key" : "p256ecdsa=" + elements . pubkey ,
300+ publicKey : elements . pubkey ,
301+ }
302+ }
303+ )
304+ }
305+
306+ verify ( token , public_key ) {
307+ let scheme = token . toLowerCase ( ) . split ( " " ) [ 0 ]
308+ if ( scheme == "bearer" || scheme == "webpush" ) {
309+ token = token . split ( " " ) [ 1 ] ;
310+ }
311+
312+ // Again, ideally, just the p256ecdsa token.
313+ if ( public_key != null ) {
314+
315+ if ( public_key . search ( 'p256ecdsa' ) > - 1 ) {
316+ let sc = / p 2 5 6 e c d s a = ( [ ^ ; , ] + ) / i;
317+ public_key = sc . exec ( public_key ) [ 1 ] ;
318+ }
319+
320+ // If there's no public key already defined, load the public_key
321+ // and try again.
322+ return this . import_public_raw ( public_key )
323+ . then ( key => {
324+ this . _public_key = key ;
325+ return this . _verify ( token ) ;
326+ } )
327+ . catch ( err => {
328+ console . error ( "Verify error" , err ) ;
329+ throw err ;
330+ } ) ;
331+ }
332+
333+ return this . _verify ( token )
334+ }
335+ }
336+
337+ class VapidToken02 extends VapidCore {
338+
339+ sign ( claims ) {
340+ return this . _sign ( claims )
341+ . then ( elements => {
342+ return {
343+ authorization : "vapid t=" + elements . jwt + ",k=" + elements . pubkey ,
344+ publicKey : elements . pubkey ,
345+ }
346+ }
347+ )
348+ }
349+
350+ verify ( token ) {
351+ let scheme = token . toLowerCase ( ) . split ( " " ) [ 0 ]
352+ if ( scheme == "vapid" ) {
353+ token = token . split ( " " ) [ 1 ] ;
354+ }
355+ let vals = { } ;
356+ let elements = token . split ( "," ) ;
357+ for ( let element of elements ) {
358+ let label = element . slice ( 0 , 2 ) ;
359+ if ( label == "t=" ) {
360+ vals . t = element . slice ( 2 ) ;
361+ }
362+ if ( label == "k=" ) {
363+ vals . k = element . slice ( 2 ) ;
364+ }
365+ }
366+ return this . import_public_raw ( vals . k )
367+ . then ( key => {
368+ this . _public_key = key ;
369+ return this . _verify ( vals . t ) ;
370+ } )
371+ . catch ( err => {
372+ console . error ( "Verify error" , err ) ;
373+ throw err ;
374+ } ) ;
375+ }
376+ }
0 commit comments