diff --git a/Cargo.toml b/Cargo.toml index 3802a4c..6fcb9fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ license = "MIT" [dependencies] base64 = "0.4" openssl = "0.9.11" -serde = "0.9" -serde_json = "0.9" -serde_derive = "0.9" +serde = "1.0" +serde_json = "1.0" +serde_derive = "1.0" time = "0.1" diff --git a/src/header.rs b/src/header.rs index 072e149..102efad 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,18 +1,19 @@ use base64::{encode_config, decode_config, URL_SAFE_NO_PAD}; -use serde::{Serialize, Deserialize}; +use serde::Serialize; +use serde::de::DeserializeOwned; use serde_json::{self, Value}; use std::default::Default; use super::error::Error; use super::Result; -/// A extensible Header that provides only algorithm field and allows for additional fields to be +/// An extensible Header that provides only algorithm field and allows for additional fields to be /// passed in via a struct that can be serialized and deserialized. Unlike the Claims struct, there /// is no convenience type alias because headers seem to vary much more greatly in practice /// depending on the application whereas claims seem to be shared as a function of registerest and /// public claims. #[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct Header { +pub struct Header { pub alg: Algorithm, #[serde(skip_serializing)] pub headers: Option, @@ -29,7 +30,7 @@ pub enum Algorithm { RS512, } -impl Header { +impl Header { pub fn from_base64(raw: &str) -> Result> { let data = decode_config(raw, URL_SAFE_NO_PAD)?; let own: Header = serde_json::from_slice(&data)?; @@ -69,7 +70,7 @@ impl Header { } } -impl Default for Header { +impl Default for Header { fn default() -> Header { Header { alg: Algorithm::HS256, diff --git a/src/lib.rs b/src/lib.rs index b67bca9..1301ee6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,8 @@ extern crate serde_derive; extern crate serde_json; extern crate time; -use serde::{Serialize, Deserialize}; +use serde::Serialize; +use serde::de::DeserializeOwned; pub use error::Error; pub use header::Header; pub use header::Algorithm; @@ -29,10 +30,7 @@ pub type DefaultToken = Token; /// Main struct representing a JSON Web Token, composed of a header and a set of claims. #[derive(Debug, Default)] -pub struct Token - where H: Serialize + Deserialize + PartialEq, - C: Serialize + Deserialize + PartialEq -{ +pub struct Token { raw: Option, pub header: Header, pub payload: Payload, @@ -40,8 +38,8 @@ pub struct Token /// Provide the ability to parse a token, verify it and sign/serialize it. impl Token - where H: Serialize + Deserialize + PartialEq, - C: Serialize + Deserialize + PartialEq + where H: Serialize + DeserializeOwned, + C: Serialize + DeserializeOwned { pub fn new(header: Header, payload: Payload) -> Token { Token { @@ -89,8 +87,8 @@ impl Token } impl PartialEq for Token - where H: Serialize + Deserialize + PartialEq, - C: Serialize + Deserialize + PartialEq + where H: PartialEq, + C: PartialEq { fn eq(&self, other: &Token) -> bool { self.header == other.header && self.payload == other.payload diff --git a/src/payload.rs b/src/payload.rs index 47b4ecf..991ee64 100644 --- a/src/payload.rs +++ b/src/payload.rs @@ -1,6 +1,7 @@ use base64::{decode_config, encode_config, URL_SAFE_NO_PAD}; use error::Error; -use serde::{Deserialize, Serialize}; +use serde::Serialize; +use serde::de::DeserializeOwned; use serde_json; use serde_json::value::Value; use super::Result; @@ -9,7 +10,7 @@ use time::{self, Timespec}; /// A default claim set, including the standard, or registered, claims and the ability to specify /// your own as custom claims. #[derive(Debug, Serialize, Deserialize, Default, PartialEq)] -pub struct Payload { +pub struct Payload { #[serde(skip_serializing_if = "Option::is_none")] pub iss: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -32,7 +33,7 @@ pub struct Payload { /// satisfies Claims' generic parameter as simply and clearly as possible. pub type DefaultPayload = Payload<()>; -impl Payload { +impl Payload { /// This implementation simply parses the base64 data twice, first parsing out the standard /// claims then any custom claims, assigning the latter into a copy of the former before /// returning registered and custom claims.