31
31
//! ```
32
32
33
33
extern crate base64;
34
- # [ macro_use ( ) ]
34
+ extern crate failure ;
35
35
extern crate openssl;
36
36
extern crate serde_json;
37
37
extern crate time;
38
38
39
39
use std:: collections:: HashMap ;
40
+ use std:: fs;
40
41
use std:: hash:: BuildHasher ;
41
- use std:: fs:: File ;
42
- use std:: io:: Read ;
43
- use std:: io:: Write ;
44
42
use std:: path:: Path ;
45
43
46
44
use openssl:: bn:: BigNumContext ;
@@ -69,9 +67,7 @@ impl Key {
69
67
where
70
68
P : AsRef < Path > ,
71
69
{
72
- let mut pem_data = Vec :: new ( ) ;
73
- let mut file = File :: open ( & path) ?;
74
- file. read_to_end ( & mut pem_data) ?;
70
+ let pem_data = fs:: read ( & path) ?;
75
71
Ok ( Key {
76
72
key : PKey :: private_key_from_pem ( & pem_data) ?. ec_key ( ) . unwrap ( ) ,
77
73
} )
@@ -80,10 +76,7 @@ impl Key {
80
76
/// Write the VAPID private key as a PEM to `path`
81
77
pub fn to_pem ( & self , path : & Path ) -> error:: VapidResult < ( ) > {
82
78
let key_data: Vec < u8 > = self . key . private_key_to_pem ( ) ?;
83
- File :: create ( & path)
84
- . map_err ( |e| { error:: VapidErrorKind :: InternalError ( format ! ( "Could not create file {:?}, {:?}" , path, e) ) } ) ?
85
- . write_all ( & key_data)
86
- . map_err ( |e| { error:: VapidErrorKind :: InternalError ( format ! ( "Could not write to file {:?}, {:?}" , path, e) ) } ) ?;
79
+ fs:: write ( & path, & key_data) ?;
87
80
Ok ( ( ) )
88
81
}
89
82
@@ -295,8 +288,8 @@ pub fn sign<S: BuildHasher>(
295
288
296
289
pub fn verify ( auth_token : String ) -> Result < HashMap < String , serde_json:: Value > , String > {
297
290
//Verify that the auth token string matches for the verification token string
298
- let auth_token = parse_auth_token ( & auth_token . clone ( ) )
299
- . expect ( "Authorization header is invalid." ) ;
291
+ let auth_token =
292
+ parse_auth_token ( & auth_token . clone ( ) ) . expect ( "Authorization header is invalid." ) ;
300
293
let pub_ec_key =
301
294
Key :: from_public_raw ( auth_token. k ) . expect ( "'k' token is not a valid public key" ) ;
302
295
let pub_key = & match PKey :: from_ec_key ( pub_ec_key) {
@@ -454,7 +447,8 @@ mod tests {
454
447
match sign ( key, & mut claims) {
455
448
Ok ( _) => panic ! ( "Failed to reject invalid sub" ) ,
456
449
Err ( err) => {
457
- // not sure how to
450
+ // not sure how to capture quoted elements in a string
451
+ // e.g. errstr.contains("\"sub\" not a valid HTML") fails.
458
452
let errstr = format ! ( "{:?}" , err) ;
459
453
assert ! ( errstr. contains( "not a valid HTML reference" ) ) ;
460
454
}
0 commit comments