3131//! ```
3232
3333extern crate base64;
34- # [ macro_use ( ) ]
34+ extern crate failure ;
3535extern crate openssl;
3636extern crate serde_json;
3737extern crate time;
3838
3939use std:: collections:: HashMap ;
40+ use std:: fs;
4041use std:: hash:: BuildHasher ;
41- use std:: fs:: File ;
42- use std:: io:: Read ;
43- use std:: io:: Write ;
4442use std:: path:: Path ;
4543
4644use openssl:: bn:: BigNumContext ;
@@ -69,9 +67,7 @@ impl Key {
6967 where
7068 P : AsRef < Path > ,
7169 {
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) ?;
7571 Ok ( Key {
7672 key : PKey :: private_key_from_pem ( & pem_data) ?. ec_key ( ) . unwrap ( ) ,
7773 } )
@@ -80,10 +76,7 @@ impl Key {
8076 /// Write the VAPID private key as a PEM to `path`
8177 pub fn to_pem ( & self , path : & Path ) -> error:: VapidResult < ( ) > {
8278 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) ?;
8780 Ok ( ( ) )
8881 }
8982
@@ -295,8 +288,8 @@ pub fn sign<S: BuildHasher>(
295288
296289pub fn verify ( auth_token : String ) -> Result < HashMap < String , serde_json:: Value > , String > {
297290 //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." ) ;
300293 let pub_ec_key =
301294 Key :: from_public_raw ( auth_token. k ) . expect ( "'k' token is not a valid public key" ) ;
302295 let pub_key = & match PKey :: from_ec_key ( pub_ec_key) {
@@ -454,7 +447,8 @@ mod tests {
454447 match sign ( key, & mut claims) {
455448 Ok ( _) => panic ! ( "Failed to reject invalid sub" ) ,
456449 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.
458452 let errstr = format ! ( "{:?}" , err) ;
459453 assert ! ( errstr. contains( "not a valid HTML reference" ) ) ;
460454 }
0 commit comments