Change to ? operator
This commit is contained in:
parent
94b83d12aa
commit
3c95e1b163
4 changed files with 20 additions and 20 deletions
|
@ -11,9 +11,9 @@ use medallion::{
|
||||||
};
|
};
|
||||||
|
|
||||||
fn load_key(keypath: &str) -> Result<String, Error> {
|
fn load_key(keypath: &str) -> Result<String, Error> {
|
||||||
let mut key_file = try!(File::open(keypath));
|
let mut key_file = File::open(keypath)?;
|
||||||
let mut key = String::new();
|
let mut key = String::new();
|
||||||
try!(key_file.read_to_string(&mut key));
|
key_file.read_to_string(&mut key)?;
|
||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,10 +40,10 @@ impl<T: Serialize + Deserialize> Component for Claims<T> {
|
||||||
/// This implementation simply parses the base64 data twice, each time applying it to the
|
/// This implementation simply parses the base64 data twice, each time applying it to the
|
||||||
/// registered and private claims.
|
/// registered and private claims.
|
||||||
fn from_base64(raw: &str) -> Result<Claims<T>> {
|
fn from_base64(raw: &str) -> Result<Claims<T>> {
|
||||||
let data = try!(decode_config(raw, URL_SAFE));
|
let data = decode_config(raw, URL_SAFE)?;
|
||||||
let reg_claims: Registered = try!(serde_json::from_slice(&data));
|
let reg_claims: Registered = serde_json::from_slice(&data)?;
|
||||||
|
|
||||||
let pri_claims: T = try!(serde_json::from_slice(&data));
|
let pri_claims: T = serde_json::from_slice(&data)?;
|
||||||
|
|
||||||
|
|
||||||
Ok(Claims {
|
Ok(Claims {
|
||||||
|
@ -58,7 +58,7 @@ impl<T: Serialize + Deserialize> Component for Claims<T> {
|
||||||
if let Value::Object(mut reg_map) = serde_json::to_value(&self.reg)? {
|
if let Value::Object(mut reg_map) = serde_json::to_value(&self.reg)? {
|
||||||
if let Value::Object(pri_map) = serde_json::to_value(&self.private)? {
|
if let Value::Object(pri_map) = serde_json::to_value(&self.private)? {
|
||||||
reg_map.extend(pri_map);
|
reg_map.extend(pri_map);
|
||||||
let s = try!(serde_json::to_string(®_map));
|
let s = serde_json::to_string(®_map)?;
|
||||||
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
|
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
|
||||||
Ok(enc)
|
Ok(enc)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub fn verify(target: &str, data: &str, key: &[u8], algorithm: &Algorithm) -> Re
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sign_hmac(data: &str, key: &[u8], digest: MessageDigest) -> Result<String> {
|
fn sign_hmac(data: &str, key: &[u8], digest: MessageDigest) -> Result<String> {
|
||||||
let secret_key = try!(PKey::hmac(key));
|
let secret_key = PKey::hmac(key)?;
|
||||||
|
|
||||||
let mut signer = Signer::new(digest, &secret_key)?;
|
let mut signer = Signer::new(digest, &secret_key)?;
|
||||||
signer.update(data.as_bytes())?;
|
signer.update(data.as_bytes())?;
|
||||||
|
@ -67,7 +67,7 @@ fn verify_rsa(signature: &str, data: &str, key: &[u8], digest: MessageDigest) ->
|
||||||
let pkey = PKey::from_rsa(public_key)?;
|
let pkey = PKey::from_rsa(public_key)?;
|
||||||
let mut verifier = Verifier::new(digest, &pkey)?;
|
let mut verifier = Verifier::new(digest, &pkey)?;
|
||||||
verifier.update(data.as_bytes())?;
|
verifier.update(data.as_bytes())?;
|
||||||
Ok(try!(verifier.finish(&signature_bytes)))
|
Ok(verifier.finish(&signature_bytes)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -128,9 +128,9 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_key(keypath: &str) -> Result<String, Error> {
|
fn load_key(keypath: &str) -> Result<String, Error> {
|
||||||
let mut key_file = try!(File::open(keypath));
|
let mut key_file = File::open(keypath)?;
|
||||||
let mut key = String::new();
|
let mut key = String::new();
|
||||||
try!(key_file.read_to_string(&mut key));
|
key_file.read_to_string(&mut key)?;
|
||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/lib.rs
20
src/lib.rs
|
@ -50,14 +50,14 @@ impl<T> Component for T
|
||||||
|
|
||||||
/// Parse from a string.
|
/// Parse from a string.
|
||||||
fn from_base64(raw: &str) -> Result<T> {
|
fn from_base64(raw: &str) -> Result<T> {
|
||||||
let data = try!(decode_config(raw, URL_SAFE));
|
let data = decode_config(raw, URL_SAFE)?;
|
||||||
let s = try!(String::from_utf8(data));
|
let s = String::from_utf8(data)?;
|
||||||
Ok(try!(serde_json::from_str(&*s)))
|
Ok(serde_json::from_str(&*s)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode to a string.
|
/// Encode to a string.
|
||||||
fn to_base64(&self) -> Result<String> {
|
fn to_base64(&self) -> Result<String> {
|
||||||
let s = try!(serde_json::to_string(&self));
|
let s = serde_json::to_string(&self)?;
|
||||||
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
|
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
|
||||||
Ok(enc)
|
Ok(enc)
|
||||||
}
|
}
|
||||||
|
@ -80,8 +80,8 @@ impl<H, C> Token<H, C>
|
||||||
|
|
||||||
Ok(Token {
|
Ok(Token {
|
||||||
raw: Some(raw.into()),
|
raw: Some(raw.into()),
|
||||||
header: try!(Component::from_base64(pieces[0])),
|
header: Component::from_base64(pieces[0])?,
|
||||||
claims: try!(Component::from_base64(pieces[1])),
|
claims: Component::from_base64(pieces[1])?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,8 +102,8 @@ impl<H, C> Token<H, C>
|
||||||
/// Generate the signed token from a key with the specific algorithm as a url-safe, base64
|
/// Generate the signed token from a key with the specific algorithm as a url-safe, base64
|
||||||
/// string.
|
/// string.
|
||||||
pub fn signed(&self, key: &[u8]) -> Result<String> {
|
pub fn signed(&self, key: &[u8]) -> Result<String> {
|
||||||
let header = try!(Component::to_base64(&self.header));
|
let header = Component::to_base64(&self.header)?;
|
||||||
let claims = try!(self.claims.to_base64());
|
let claims = self.claims.to_base64()?;
|
||||||
let data = format!("{}.{}", header, claims);
|
let data = format!("{}.{}", header, claims);
|
||||||
|
|
||||||
let sig = crypt::sign(&*data, key, &self.header.alg())?;
|
let sig = crypt::sign(&*data, key, &self.header.alg())?;
|
||||||
|
@ -172,9 +172,9 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_key(keypath: &str) -> Result<String, Error> {
|
fn load_key(keypath: &str) -> Result<String, Error> {
|
||||||
let mut key_file = try!(File::open(keypath));
|
let mut key_file = File::open(keypath)?;
|
||||||
let mut key = String::new();
|
let mut key = String::new();
|
||||||
try!(key_file.read_to_string(&mut key));
|
key_file.read_to_string(&mut key)?;
|
||||||
Ok(key)
|
Ok(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue