Add defaults for generic parameters (#5)
This commit is contained in:
parent
cb304d20e9
commit
1b594ff60e
4 changed files with 38 additions and 39 deletions
23
Cargo.toml
23
Cargo.toml
|
@ -1,25 +1,24 @@
|
||||||
[package]
|
[package]
|
||||||
name = "medallion"
|
|
||||||
version = "2.2.2"
|
|
||||||
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
|
authors = ["Thomas Gideon <cmdln@thecommandline.net>"]
|
||||||
|
categories = ["cryptography", "authentication", "web-programming", "data-structures"]
|
||||||
description = "JWT library for rust using serde, serde_json and openssl"
|
description = "JWT library for rust using serde, serde_json and openssl"
|
||||||
homepage = "http://github.com/commandline/medallion"
|
|
||||||
repository = "http://github.com/commandline/medallion"
|
|
||||||
documentation = "https://commandline.github.io/medallion/"
|
documentation = "https://commandline.github.io/medallion/"
|
||||||
readme = "README.md"
|
homepage = "http://github.com/commandline/medallion"
|
||||||
keywords = ["JWT", "token", "web", "JSON", "RSA"]
|
keywords = ["JWT", "token", "web", "JSON", "RSA"]
|
||||||
categories = ["cryptography", "authentication", "web-programming",
|
|
||||||
"data-structures"]
|
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
name = "medallion"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "http://github.com/commandline/medallion"
|
||||||
|
version = "2.2.3"
|
||||||
|
[badges]
|
||||||
|
[badges.travis-ci]
|
||||||
|
branch = "master"
|
||||||
|
repository = "https://travis-ci.org/commandline/medallion"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.9"
|
base64 = "0.9"
|
||||||
openssl = "0.10"
|
openssl = "0.10"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
|
||||||
serde_derive = "1.0"
|
serde_derive = "1.0"
|
||||||
|
serde_json = "1.0"
|
||||||
time = "0.1"
|
time = "0.1"
|
||||||
|
|
||||||
[badges]
|
|
||||||
travis-ci = { repository = "https://travis-ci.org/commandline/medallion", branch = "master" }
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use base64::{decode_config, encode_config, URL_SAFE_NO_PAD};
|
use base64::{decode_config, encode_config, URL_SAFE_NO_PAD};
|
||||||
use serde::Serialize;
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
use serde::Serialize;
|
||||||
use serde_json::{self, Value};
|
use serde_json::{self, Value};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ use super::Result;
|
||||||
/// depending on the application whereas claims seem to be shared as a function of registerest and
|
/// depending on the application whereas claims seem to be shared as a function of registerest and
|
||||||
/// public claims.
|
/// public claims.
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct Header<T> {
|
pub struct Header<T = ()> {
|
||||||
pub alg: Algorithm,
|
pub alg: Algorithm,
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
pub headers: Option<T>,
|
pub headers: Option<T>,
|
||||||
|
|
42
src/lib.rs
42
src/lib.rs
|
@ -14,17 +14,17 @@ extern crate serde_derive;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate time;
|
extern crate time;
|
||||||
|
|
||||||
use serde::Serialize;
|
|
||||||
use serde::de::DeserializeOwned;
|
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use header::Header;
|
|
||||||
pub use header::Algorithm;
|
pub use header::Algorithm;
|
||||||
|
pub use header::Header;
|
||||||
pub use payload::{DefaultPayload, Payload};
|
pub use payload::{DefaultPayload, Payload};
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
|
use serde::Serialize;
|
||||||
|
|
||||||
|
mod crypt;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
mod header;
|
mod header;
|
||||||
mod payload;
|
mod payload;
|
||||||
mod crypt;
|
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ pub type DefaultToken<H> = Token<H, ()>;
|
||||||
|
|
||||||
/// Main struct representing a JSON Web Token, composed of a header and a set of claims.
|
/// Main struct representing a JSON Web Token, composed of a header and a set of claims.
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct Token<H, C> {
|
pub struct Token<H = (), C = ()> {
|
||||||
raw: Option<String>,
|
raw: Option<String>,
|
||||||
pub header: Header<H>,
|
pub header: Header<H>,
|
||||||
pub payload: Payload<C>,
|
pub payload: Payload<C>,
|
||||||
|
@ -104,11 +104,11 @@ where
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use {DefaultPayload, DefaultToken, Header};
|
use super::Algorithm::{HS256, RS512};
|
||||||
use openssl;
|
use openssl;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use time::{self, Duration, Tm};
|
use time::{self, Duration, Tm};
|
||||||
use super::Algorithm::{HS256, RS512};
|
use {DefaultPayload, DefaultToken, Header, Payload, Token};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
pub fn raw_data() {
|
pub fn raw_data() {
|
||||||
|
@ -124,16 +124,16 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
pub fn roundtrip_hmac() {
|
pub fn roundtrip_hmac() {
|
||||||
let now = time::now();
|
let now = time::now();
|
||||||
let header: Header<()> = Default::default();
|
let header: Header<()> = Header::default();
|
||||||
let payload = DefaultPayload {
|
let payload = DefaultPayload {
|
||||||
nbf: Some(now.to_timespec().sec as u64),
|
nbf: Some(now.to_timespec().sec as u64),
|
||||||
exp: Some((now + Duration::minutes(5)).to_timespec().sec as u64),
|
exp: Some((now + Duration::minutes(5)).to_timespec().sec as u64),
|
||||||
..Default::default()
|
..DefaultPayload::default()
|
||||||
};
|
};
|
||||||
let token = DefaultToken::new(header, payload);
|
let token = Token::new(header, payload);
|
||||||
let key = "secret".as_bytes();
|
let key = "secret".as_bytes();
|
||||||
let raw = token.sign(key).unwrap();
|
let raw = token.sign(key).unwrap();
|
||||||
let same = DefaultToken::parse(&*raw).unwrap();
|
let same = Token::parse(&*raw).unwrap();
|
||||||
|
|
||||||
assert_eq!(token, same);
|
assert_eq!(token, same);
|
||||||
assert!(same.verify(key).unwrap());
|
assert!(same.verify(key).unwrap());
|
||||||
|
@ -145,7 +145,7 @@ mod tests {
|
||||||
let token = create_for_range(now, now + Duration::minutes(-5));
|
let token = create_for_range(now, now + Duration::minutes(-5));
|
||||||
let key = "secret".as_bytes();
|
let key = "secret".as_bytes();
|
||||||
let raw = token.sign(key).unwrap();
|
let raw = token.sign(key).unwrap();
|
||||||
let same = DefaultToken::parse(&*raw).unwrap();
|
let same = Token::parse(&*raw).unwrap();
|
||||||
|
|
||||||
assert_eq!(token, same);
|
assert_eq!(token, same);
|
||||||
assert_eq!(false, same.verify(key).unwrap());
|
assert_eq!(false, same.verify(key).unwrap());
|
||||||
|
@ -157,7 +157,7 @@ mod tests {
|
||||||
let token = create_for_range(now + Duration::minutes(5), now + Duration::minutes(10));
|
let token = create_for_range(now + Duration::minutes(5), now + Duration::minutes(10));
|
||||||
let key = "secret".as_bytes();
|
let key = "secret".as_bytes();
|
||||||
let raw = token.sign(key).unwrap();
|
let raw = token.sign(key).unwrap();
|
||||||
let same = DefaultToken::parse(&*raw).unwrap();
|
let same = Token::parse(&*raw).unwrap();
|
||||||
|
|
||||||
assert_eq!(token, same);
|
assert_eq!(token, same);
|
||||||
assert_eq!(false, same.verify(key).unwrap());
|
assert_eq!(false, same.verify(key).unwrap());
|
||||||
|
@ -168,16 +168,16 @@ mod tests {
|
||||||
let rsa_keypair = openssl::rsa::Rsa::generate(2048).unwrap();
|
let rsa_keypair = openssl::rsa::Rsa::generate(2048).unwrap();
|
||||||
let header: Header<()> = Header {
|
let header: Header<()> = Header {
|
||||||
alg: RS512,
|
alg: RS512,
|
||||||
..Default::default()
|
..Header::default()
|
||||||
};
|
};
|
||||||
let token = DefaultToken {
|
let token = DefaultToken {
|
||||||
header: header,
|
header: header,
|
||||||
..Default::default()
|
..Token::default()
|
||||||
};
|
};
|
||||||
let raw = token
|
let raw = token
|
||||||
.sign(&rsa_keypair.private_key_to_pem().unwrap())
|
.sign(&rsa_keypair.private_key_to_pem().unwrap())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let same = DefaultToken::parse(&*raw).unwrap();
|
let same = Token::parse(&*raw).unwrap();
|
||||||
|
|
||||||
assert_eq!(token, same);
|
assert_eq!(token, same);
|
||||||
assert!(
|
assert!(
|
||||||
|
@ -186,13 +186,13 @@ mod tests {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_for_range(nbf: Tm, exp: Tm) -> DefaultToken<()> {
|
fn create_for_range(nbf: Tm, exp: Tm) -> Token {
|
||||||
let header: Header<()> = Default::default();
|
let header: Header = Header::default();
|
||||||
let payload = DefaultPayload {
|
let payload = Payload {
|
||||||
nbf: Some(nbf.to_timespec().sec as u64),
|
nbf: Some(nbf.to_timespec().sec as u64),
|
||||||
exp: Some(exp.to_timespec().sec as u64),
|
exp: Some(exp.to_timespec().sec as u64),
|
||||||
..Default::default()
|
..Payload::default()
|
||||||
};
|
};
|
||||||
DefaultToken::new(header, payload)
|
Token::new(header, payload)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
|
use super::Result;
|
||||||
use base64::{decode_config, encode_config, URL_SAFE_NO_PAD};
|
use base64::{decode_config, encode_config, URL_SAFE_NO_PAD};
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use serde::Serialize;
|
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
use serde::Serialize;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use serde_json::value::Value;
|
use serde_json::value::Value;
|
||||||
use super::Result;
|
|
||||||
use time::{self, Timespec};
|
use time::{self, Timespec};
|
||||||
|
|
||||||
/// A default claim set, including the standard, or registered, claims and the ability to specify
|
/// A default claim set, including the standard, or registered, claims and the ability to specify
|
||||||
/// your own as custom claims.
|
/// your own as custom claims.
|
||||||
#[derive(Debug, Serialize, Deserialize, Default, PartialEq)]
|
#[derive(Debug, Serialize, Deserialize, Default, PartialEq)]
|
||||||
pub struct Payload<T> {
|
pub struct Payload<T = ()> {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub iss: Option<String>,
|
pub iss: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
@ -100,9 +100,9 @@ impl<T: Serialize + DeserializeOwned> Payload<T> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use super::{DefaultPayload, Payload};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use time::{self, Duration};
|
use time::{self, Duration};
|
||||||
use super::{DefaultPayload, Payload};
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
|
#[derive(Default, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
struct CustomClaims {
|
struct CustomClaims {
|
||||||
|
|
Loading…
Reference in a new issue