Skip to content

Commit 20eaffc

Browse files
committed
f r's
1 parent d1fcd1f commit 20eaffc

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

rust/vapid/src/error.rs

-8
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ pub enum VapidErrorKind {
2222
InternalError(String),
2323
}
2424

25-
/*
26-
impl VapidError {
27-
pub fn kind(&self) -> &VapidErrorKind {
28-
self.inner.get_context()
29-
}
30-
}
31-
*/
32-
3325
impl Fail for VapidError {
3426
fn cause(&self) -> Option<&Fail> {
3527
self.inner.cause()

rust/vapid/src/lib.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,14 @@
3131
//! ```
3232
3333
extern crate base64;
34-
#[macro_use()]
34+
extern crate failure;
3535
extern crate openssl;
3636
extern crate serde_json;
3737
extern crate time;
3838

3939
use std::collections::HashMap;
40+
use std::fs;
4041
use std::hash::BuildHasher;
41-
use std::fs::File;
42-
use std::io::Read;
43-
use std::io::Write;
4442
use std::path::Path;
4543

4644
use 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

296289
pub 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

Comments
 (0)