Get tests (almost) working

This commit is contained in:
Thomas Gideon 2017-02-14 13:33:00 -05:00
parent 5e81d2fccb
commit 81a965caef
3 changed files with 36 additions and 29 deletions

View file

@ -3,12 +3,12 @@ use Component;
use error::Error;
use serde::{Deserialize, Serialize};
use serde_json;
use serde_json::value::{from_value, to_value, Map, Value};
use serde_json::value::{Value};
#[derive(Debug, Default, PartialEq)]
pub struct Claims {
pub struct Claims<T: Serialize + Deserialize> {
pub reg: Registered,
pub private: Value
pub private: T
}
#[derive(Debug, Default, PartialEq, Serialize, Deserialize)]
@ -22,11 +22,8 @@ pub struct Registered {
pub jti: Option<String>,
}
/// JWT Claims. Registered claims are directly accessible via the `Registered`
/// struct embedded, while private fields are a map that contains `Json`
/// values.
impl Claims {
pub fn new(reg: Registered, private: Value) -> Claims {
impl<T: Serialize + Deserialize> Claims<T>{
pub fn new(reg: Registered, private: T) -> Claims<T> {
Claims {
reg: reg,
private: private
@ -34,31 +31,34 @@ impl Claims {
}
}
impl Component for Claims {
fn from_base64(raw: &str) -> Result<Claims, Error> {
impl<T: Serialize + Deserialize> Component for Claims<T> {
fn from_base64(raw: &str) -> Result<Claims<T>, Error> {
let data = try!(decode(raw));
let reg_claims: Registered = try!(serde_json::from_slice(&data));
let pri_claims: Value = try!(serde_json::from_slice(&data));
let pri_claims: T = try!(serde_json::from_slice(&data));
Ok(Claims{
Ok(Claims {
reg: reg_claims,
private: pri_claims
})
}
fn to_base64(&self) -> Result<String, Error> {
let mut value = try!(serde_json::to_value(&self.reg));
let mut obj_value = &value.as_object_mut().unwrap();
// TODO iterate private claims and add to JSON Map
//let mut pri_value = self.private.as_object_mut().unwrap();
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)? {
reg_map.extend(pri_map);
let s = try!(serde_json::to_string(&reg_map));
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
Ok(enc)
} else {
Err(Error::Custom("Could not access registered claims.".to_owned()))
}
} else {
Err(Error::Custom("Could not access private claims.".to_owned()))
}
//obj_value.extend(pri_value.into_iter());
let s = try!(serde_json::to_string(&obj_value));
let enc = encode_config((&*s).as_bytes(), URL_SAFE);
Ok(enc)
}
}
@ -68,10 +68,13 @@ mod tests {
use claims::{Claims, Registered};
use Component;
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
struct EmptyClaim { }
#[test]
fn from_base64() {
let enc = "ew0KICAiaXNzIjogIm1pa2t5YW5nLmNvbSIsDQogICJleHAiOiAxMzAyMzE5MTAwLA0KICAibmFtZSI6ICJNaWNoYWVsIFlhbmciLA0KICAiYWRtaW4iOiB0cnVlDQp9";
let claims = Claims::from_base64(enc).unwrap();
let claims: Claims<EmptyClaim> = Claims::from_base64(enc).unwrap();
assert_eq!(claims.reg.iss.unwrap(), "mikkyang.com");
assert_eq!(claims.reg.exp.unwrap(), 1302319100);
@ -80,7 +83,7 @@ mod tests {
#[test]
fn multiple_types() {
let enc = "ew0KICAiaXNzIjogIm1pa2t5YW5nLmNvbSIsDQogICJleHAiOiAxMzAyMzE5MTAwLA0KICAibmFtZSI6ICJNaWNoYWVsIFlhbmciLA0KICAiYWRtaW4iOiB0cnVlDQp9";
let claims = Registered::from_base64(enc).unwrap();
let claims = Registered::from_base64(enc).unwrap();
assert_eq!(claims.iss.unwrap(), "mikkyang.com");
assert_eq!(claims.exp.unwrap(), 1302319100);
@ -88,7 +91,7 @@ mod tests {
#[test]
fn roundtrip() {
let mut claims: Claims = Default::default();
let mut claims: Claims<EmptyClaim> = Default::default();
claims.reg.iss = Some("mikkyang.com".into());
claims.reg.exp = Some(1302319100);
let enc = claims.to_base64().unwrap();

View file

@ -8,6 +8,7 @@ pub enum Error {
Utf8(FromUtf8Error),
Base64(Base64Error),
JSON(serde_json::Error),
Custom(String),
}
macro_rules! error_wrap {

View file

@ -168,11 +168,14 @@ mod tests {
use std::fs::File;
use openssl::hash::MessageDigest;
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
struct EmptyClaim { }
#[test]
pub fn sign_data() {
let header = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
let claims = "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9";
let real_sig = "TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
let real_sig = "TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ=";
let data = format!("{}.{}", header, claims);
let sig = sign(&*data, "secret".as_bytes(), MessageDigest::sha256());
@ -184,7 +187,7 @@ mod tests {
pub fn sign_data_rsa() {
let header = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9";
let claims = "eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9";
let real_sig = "nXdpIkFQYZXZ0VlJjHmAc5/aewHCCJpT5jP1fpexUCF/9m3NxlC7uYNXAl6NKno520oh9wVT4VV/vmPeEin7BnnoIJNPcImWcUzkYpLTrDBntiF9HCuqFaniuEVzlf8dVlRJgo8QxhmUZEjyDFjPZXZxPlPV1LD6hrtItxMKZbh1qoNY3OL7Mwo+WuSRQ0mmKj+/y3weAmx/9EaTLY639uD8+o5iZxIIf85U4e55Wdp+C9FJ4RxyHpjgoG8p87IbChfleSdWcZL3NZuxjRCHVWgS1uYG0I+LqBWpWyXnJ1zk6+w4tfxOYpZFMOIyq4tY2mxJQ78Kvcu8bTO7UdI7iA";
let real_sig = "nXdpIkFQYZXZ0VlJjHmAc5_aewHCCJpT5jP1fpexUCF_9m3NxlC7uYNXAl6NKno520oh9wVT4VV_vmPeEin7BnnoIJNPcImWcUzkYpLTrDBntiF9HCuqFaniuEVzlf8dVlRJgo8QxhmUZEjyDFjPZXZxPlPV1LD6hrtItxMKZbh1qoNY3OL7Mwo-WuSRQ0mmKj-_y3weAmx_9EaTLY639uD8-o5iZxIIf85U4e55Wdp-C9FJ4RxyHpjgoG8p87IbChfleSdWcZL3NZuxjRCHVWgS1uYG0I-LqBWpWyXnJ1zk6-w4tfxOYpZFMOIyq4tY2mxJQ78Kvcu8bTO7UdI7iA==";
let data = format!("{}.{}", header, claims);
let key = load_key("./examples/privateKey.pem").unwrap();
@ -218,7 +221,7 @@ mod tests {
#[test]
pub fn raw_data() {
let raw = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
let token = Token::<DefaultHeader, Claims>::parse(raw).unwrap();
let token = Token::<DefaultHeader, Claims<EmptyClaim>>::parse(raw).unwrap();
{
assert_eq!(token.header.alg, HS256);
@ -228,7 +231,7 @@ mod tests {
#[test]
pub fn roundtrip() {
let token: Token<DefaultHeader, Claims> = Default::default();
let token: Token<DefaultHeader, Claims<EmptyClaim>> = Default::default();
let key = "secret".as_bytes();
let raw = token.signed(key).unwrap();
let same = Token::parse(&*raw).unwrap();
@ -239,7 +242,7 @@ mod tests {
#[test]
pub fn roundtrip_rsa() {
let token: Token<DefaultHeader, Claims> = Token {
let token: Token<DefaultHeader, Claims<EmptyClaim>> = Token {
header: DefaultHeader {
alg: RS512,
..Default::default()