Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rust/vapid/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.4.0

* Changed `VapidErrors` to be more Clippy friendly
* updates for latest rust


# 0.2.0

Due to changes in the OpenSSL library, several calls changed form from `0.1.0`
Expand Down
4 changes: 2 additions & 2 deletions rust/vapid/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vapid"
version = "0.3.0"
version = "0.4.0"
authors = ["jrconlin <jconlin+git@mozilla.com>"]
edition = "2018"
description = "An implementation of the RFC 8292 Voluntary Application Server Identification (VAPID) Auth header generator"
Expand All @@ -11,5 +11,5 @@ license = "MPL 2.0"
openssl = "0.10"
serde_json = "1.0"
base64 = "0.13"
time = "0.2"
time = "0.3"
failure = "0.1"
8 changes: 4 additions & 4 deletions rust/vapid/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ pub struct VapidError {
#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum VapidErrorKind {
#[fail(display = "Invalid public key")]
PublicKeyError,
PublicKey,
#[fail(display = "VAPID error: {}", _0)]
VapidError(String),
Protocol(String),
#[fail(display = "Internal Error {:?}", _0)]
InternalError(String),
Internal(String),
}

impl Fail for VapidError {
Expand Down Expand Up @@ -52,6 +52,6 @@ impl From<Context<VapidErrorKind>> for VapidError {

impl From<Error> for VapidError {
fn from(err: Error) -> VapidError {
VapidErrorKind::InternalError(format!("Error: {:?}", err)).into()
VapidErrorKind::Internal(format!("Error: {:?}", err)).into()
}
}
25 changes: 12 additions & 13 deletions rust/vapid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Key {
let group = ec::EcGroup::from_curve_name(nid::Nid::X9_62_PRIME256V1)?;
if bytes.len() != 65 || bytes[0] != 4 {
// It's not a properly tagged key.
return Err(error::VapidErrorKind::PublicKeyError.into());
return Err(error::VapidErrorKind::PublicKey.into());
}
let point = ec::EcPoint::from_bytes(&group, &bytes, &mut ctx)?;
Ok(ec::EcKey::from_public_key(&group, &point)?)
Expand Down Expand Up @@ -186,14 +186,14 @@ pub fn sign<S: BuildHasher>(
match claims.get("sub") {
Some(sub) => {
if !sub.as_str().unwrap().starts_with("mailto") {
return Err(error::VapidErrorKind::VapidError(
return Err(error::VapidErrorKind::Protocol(
"'sub' not a valid HTML reference".to_owned(),
)
.into());
}
}
None => {
return Err(error::VapidErrorKind::VapidError("'sub' not found".to_owned()).into());
return Err(error::VapidErrorKind::Protocol("'sub' not found".to_owned()).into());
}
}
let today = SystemTime::now();
Expand All @@ -205,21 +205,21 @@ pub fn sign<S: BuildHasher>(
Some(exp) => {
let exp_val = exp.as_i64().unwrap();
if (exp_val as u64) < to_secs(today) {
return Err(error::VapidErrorKind::VapidError(
return Err(error::VapidErrorKind::Protocol(
r#""exp" already expired"#.to_owned(),
)
.into());
}
if (exp_val as u64) > to_secs(tomorrow) {
return Err(error::VapidErrorKind::VapidError(
return Err(error::VapidErrorKind::Protocol(
r#""exp" set too far ahead"#.to_owned(),
)
.into());
}
}
None => {
// We already do an insertion on empty, so this should never trigger.
return Err(error::VapidErrorKind::VapidError(
return Err(error::VapidErrorKind::Protocol(
r#""exp" failed to initialize"#.to_owned(),
)
.into());
Expand All @@ -238,7 +238,7 @@ pub fn sign<S: BuildHasher>(
let mut signer = match Signer::new(MessageDigest::sha256(), &pub_key) {
Ok(t) => t,
Err(err) => {
return Err(error::VapidErrorKind::VapidError(format!(
return Err(error::VapidErrorKind::Protocol(format!(
"Could not sign the claims: {:?}",
err
))
Expand Down Expand Up @@ -290,8 +290,7 @@ pub fn sign<S: BuildHasher>(

pub fn verify(auth_token: String) -> Result<HashMap<String, serde_json::Value>, String> {
//Verify that the auth token string matches for the verification token string
let auth_token =
parse_auth_token(&auth_token).expect("Authorization header is invalid.");
let auth_token = parse_auth_token(&auth_token).expect("Authorization header is invalid.");
let pub_ec_key =
Key::from_public_raw(auth_token.k).expect("'k' token is not a valid public key");
let pub_key = &match PKey::from_ec_key(pub_ec_key) {
Expand Down Expand Up @@ -401,18 +400,18 @@ mod tests {
assert!(result.contains(" vapid "));

// tear apart the auth token for the happy bits
let token = result.split(" ").nth(2).unwrap();
let sub_parts: Vec<&str> = token.split(",").collect();
let token = result.split(' ').nth(2).unwrap();
let sub_parts: Vec<&str> = token.split(',').collect();
let mut auth_parts: HashMap<String, String> = HashMap::new();
for kvi in &sub_parts {
let kv: Vec<String> = kvi.splitn(2, "=").map(|x| String::from(x)).collect();
let kv: Vec<String> = kvi.splitn(2, '=').map(String::from).collect();
auth_parts.insert(kv[0].clone(), kv[1].clone());
}
assert!(auth_parts.contains_key("t"));
assert!(auth_parts.contains_key("k"));

// now tear apart the token
let token: Vec<&str> = auth_parts.get("t").unwrap().split(".").collect();
let token: Vec<&str> = auth_parts.get("t").unwrap().split('.').collect();
assert_eq!(token.len(), 3);

let content =
Expand Down