@@ -17,11 +17,12 @@ function chr(c){
17
17
}
18
18
19
19
var vapid = {
20
- /* Generate and verify a VAPID token */
21
-
22
- errs : {
23
- enus : {
20
+ /* English:US */
21
+ enus : {
22
+ info : {
24
23
OK_VAPID_KEYS : "VAPID Keys defined." ,
24
+ }
25
+ errs : {
25
26
ERR_VAPID_KEY : "VAPID generate keys error: " ,
26
27
ERR_PUB_R_KEY : "Invalid Public Key record. Please use a valid RAW Formatted record." ,
27
28
ERR_PUB_D_KEY : "Invalid Public Key record. Please use a valid DER Formatted record." ,
@@ -35,9 +36,12 @@ var vapid = {
35
36
}
36
37
} ,
37
38
39
+ lang : this . enus ;
40
+
38
41
_private_key: "" ,
39
42
_public_key : "" ,
40
43
44
+ /* Generate and verify a VAPID token */
41
45
generate_keys : function ( ) {
42
46
/* Generate the public and private keys
43
47
*/
@@ -48,10 +52,10 @@ var vapid = {
48
52
. then ( keys => {
49
53
this . _private_key = keys . privateKey ;
50
54
this . _public_key = keys . publicKey ;
51
- console . info ( this . errs . enus . OK_VAPID_KEYS ) ;
55
+ console . info ( this . lang . info . OK_VAPID_KEYS ) ;
52
56
} )
53
57
. catch ( fail => {
54
- console . error ( this . errs . enus . ERR_VAPID_KEY , fail ) ;
58
+ console . error ( this . lang . errs . ERR_VAPID_KEY , fail ) ;
55
59
} ) ;
56
60
} ,
57
61
@@ -150,11 +154,18 @@ var vapid = {
150
154
} ) ;
151
155
} ,
152
156
157
+ export_public_raw : function ( ) {
158
+ return webCrypto . exportKey ( 'raw' , this . _public_key )
159
+ . then ( key => {
160
+ return this . url_btoa ( key ) ;
161
+ } )
162
+ } ,
163
+
153
164
import_public_raw : function ( raw ) {
154
165
if ( typeof ( raw ) == "string" ) {
155
166
raw = this . url_atob ( raw ) ;
156
167
}
157
- let err = new Error ( this . errs . enus . ERR_PUB_KEY ) ;
168
+ let err = new Error ( this . lang . errs . ERR_PUB_KEY ) ;
158
169
159
170
// Raw is supposed to start with a 0x04, but some libraries don't. sigh.
160
171
if ( raw . length == 65 && raw [ 0 ] != 4 ) {
@@ -189,7 +200,7 @@ var vapid = {
189
200
derArray = this . url_atob ( derArray ) ;
190
201
}
191
202
/* Super light weight public key import function */
192
- let err = new Error ( this . errs . enus . ERR_PUB_D_KEY ) ;
203
+ let err = new Error ( this . lang . errs . ERR_PUB_D_KEY ) ;
193
204
// Does the record begin with "\x30"
194
205
if ( derArray [ 0 ] != 48 ) { throw err }
195
206
// is this an ECDSA record? (looking for \x2a and \x86
@@ -228,14 +239,14 @@ var vapid = {
228
239
* to specify VAPID auth.
229
240
*/
230
241
if ( this . _public_key == "" ) {
231
- throw new Error ( this . errs . enus . ERR_NO_KEYS ) ;
242
+ throw new Error ( this . lang . errs . ERR_NO_KEYS ) ;
232
243
}
233
244
if ( ! claims . hasOwnProperty ( "exp" ) ) {
234
245
claims . exp = parseInt ( Date . now ( ) * .001 ) + 86400 ;
235
246
}
236
247
[ "sub" , "aud" ] . forEach ( function ( key ) {
237
248
if ( ! claims . hasOwnProperty ( key ) ) {
238
- throw new Error ( this . errs . enus . ERR_CLAIM_MIS , key ) ;
249
+ throw new Error ( this . lang . errs . ERR_CLAIM_MIS , key ) ;
239
250
}
240
251
} )
241
252
let alg = { name :"ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
@@ -266,10 +277,29 @@ var vapid = {
266
277
} )
267
278
} )
268
279
. catch ( err => {
269
- console . error ( this . errs . enus . ERR_SIGN , err ) ;
280
+ console . error ( this . lang . errs . ERR_SIGN , err ) ;
270
281
} )
271
282
} ,
272
283
284
+ validate : function ( string ) {
285
+ /* Sign the token for the developer Dashboard */
286
+ let alg = { name :"ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
287
+ let t2v = this . url_atob ( string ) ;
288
+ return webCrypto . sign ( alg , this . _private_key , t2v )
289
+ . then ( signed => {
290
+ let sig = this . url_btoa ( signed ) ;
291
+ return sig ;
292
+ } ) ;
293
+ } ,
294
+
295
+ validateCheck : function ( sig , string ) {
296
+ /* verify a given signature string matches */
297
+ let alg = { name : "ECDSA" , namedCurve : "P-256" , hash :{ name :"SHA-256" } } ;
298
+ let vsig = this . url_atob ( sig ) ;
299
+ let t2v = this . url_atob ( string ) ;
300
+ return webCrypto . verify ( alg , this . _public_key , vsig , t2v ) ;
301
+ } ,
302
+
273
303
verify : function ( token , public_key = null ) {
274
304
/* Verify a VAPID token.
275
305
*
@@ -302,22 +332,22 @@ var vapid = {
302
332
} ) ;
303
333
}
304
334
if ( this . _public_key == "" ) {
305
- throw new Error ( this . errs . enus . ERR_NO_KEYS ) ;
335
+ throw new Error ( this . lang . errs . ERR_NO_KEYS ) ;
306
336
}
307
337
308
- let alg = { name : "ECDSA" , namedCurve : "P-256" , hash : "SHA-256" } ;
338
+ let alg = { name : "ECDSA" , namedCurve : "P-256" , hash : { name : "SHA-256" } } ;
309
339
let items = token . split ( '.' ) ;
310
340
let signature ;
311
341
let key ;
312
342
try {
313
343
signature = this . url_atob ( items [ 2 ] ) ;
314
344
} catch ( err ) {
315
- throw new Error ( this . errs . enus . ERR_VERIFY_SG + err . message ) ;
345
+ throw new Error ( this . lang . errs . ERR_VERIFY_SG + err . message ) ;
316
346
}
317
347
try {
318
348
key = this . url_atob ( items [ 1 ] ) ;
319
349
} catch ( err ) {
320
- throw new Error ( this . errs . enus . ERR_VERIFY_KE + err . message ) ;
350
+ throw new Error ( this . lang . errs . ERR_VERIFY_KE + err . message ) ;
321
351
}
322
352
let content = items . slice ( 0 , 2 ) . join ( '.' ) ;
323
353
let signatory = this . _str_to_array ( content ) ;
@@ -331,11 +361,11 @@ var vapid = {
331
361
return JSON . parse ( String . fromCharCode
332
362
. apply ( null , this . url_atob ( items [ 1 ] ) ) )
333
363
}
334
- throw new Error ( this . errs . enus . ERR_SIGNATURE ) ;
364
+ throw new Error ( this . lang . errs . ERR_SIGNATURE ) ;
335
365
} )
336
366
. catch ( err => {
337
- console . error ( this . errs . enus . ERR_VERIFY , err ) ;
338
- throw new Error ( this . errs . enus . ERR_VERIFY + ": " + err . message ) ;
367
+ console . error ( this . lang . errs . ERR_VERIFY , err ) ;
368
+ throw new Error ( this . lang . errs . ERR_VERIFY + ": " + err . message ) ;
339
369
} ) ;
340
370
}
341
371
}
0 commit comments