Merge branch 'master' of github.com:commandline/medallion
This commit is contained in:
commit
a265a703d6
4 changed files with 20 additions and 20 deletions
|
@ -13,7 +13,7 @@ license = "MIT"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.4"
|
base64 = "0.4"
|
||||||
openssl = "0.9.11"
|
openssl = "0.9.11"
|
||||||
serde = "0.9"
|
serde = "1.0"
|
||||||
serde_json = "0.9"
|
serde_json = "1.0"
|
||||||
serde_derive = "0.9"
|
serde_derive = "1.0"
|
||||||
time = "0.1"
|
time = "0.1"
|
||||||
|
|
|
@ -1,18 +1,19 @@
|
||||||
use base64::{encode_config, decode_config, URL_SAFE_NO_PAD};
|
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 serde_json::{self, Value};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
|
|
||||||
use super::error::Error;
|
use super::error::Error;
|
||||||
use super::Result;
|
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
|
/// 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
|
/// 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
|
/// 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: Serialize + Deserialize> {
|
pub struct Header<T> {
|
||||||
pub alg: Algorithm,
|
pub alg: Algorithm,
|
||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
pub headers: Option<T>,
|
pub headers: Option<T>,
|
||||||
|
@ -29,7 +30,7 @@ pub enum Algorithm {
|
||||||
RS512,
|
RS512,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Serialize + Deserialize> Header<T> {
|
impl<T: Serialize + DeserializeOwned> Header<T> {
|
||||||
pub fn from_base64(raw: &str) -> Result<Header<T>> {
|
pub fn from_base64(raw: &str) -> Result<Header<T>> {
|
||||||
let data = decode_config(raw, URL_SAFE_NO_PAD)?;
|
let data = decode_config(raw, URL_SAFE_NO_PAD)?;
|
||||||
let own: Header<T> = serde_json::from_slice(&data)?;
|
let own: Header<T> = serde_json::from_slice(&data)?;
|
||||||
|
@ -69,7 +70,7 @@ impl<T: Serialize + Deserialize> Header<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Serialize + Deserialize> Default for Header<T> {
|
impl<T> Default for Header<T> {
|
||||||
fn default() -> Header<T> {
|
fn default() -> Header<T> {
|
||||||
Header {
|
Header {
|
||||||
alg: Algorithm::HS256,
|
alg: Algorithm::HS256,
|
||||||
|
|
16
src/lib.rs
16
src/lib.rs
|
@ -9,7 +9,8 @@ extern crate serde_derive;
|
||||||
extern crate serde_json;
|
extern crate serde_json;
|
||||||
extern crate time;
|
extern crate time;
|
||||||
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::Serialize;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
pub use error::Error;
|
pub use error::Error;
|
||||||
pub use header::Header;
|
pub use header::Header;
|
||||||
pub use header::Algorithm;
|
pub use header::Algorithm;
|
||||||
|
@ -29,10 +30,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> {
|
||||||
where H: Serialize + Deserialize + PartialEq,
|
|
||||||
C: Serialize + Deserialize + PartialEq
|
|
||||||
{
|
|
||||||
raw: Option<String>,
|
raw: Option<String>,
|
||||||
pub header: Header<H>,
|
pub header: Header<H>,
|
||||||
pub payload: Payload<C>,
|
pub payload: Payload<C>,
|
||||||
|
@ -40,8 +38,8 @@ pub struct Token<H, C>
|
||||||
|
|
||||||
/// Provide the ability to parse a token, verify it and sign/serialize it.
|
/// Provide the ability to parse a token, verify it and sign/serialize it.
|
||||||
impl<H, C> Token<H, C>
|
impl<H, C> Token<H, C>
|
||||||
where H: Serialize + Deserialize + PartialEq,
|
where H: Serialize + DeserializeOwned,
|
||||||
C: Serialize + Deserialize + PartialEq
|
C: Serialize + DeserializeOwned
|
||||||
{
|
{
|
||||||
pub fn new(header: Header<H>, payload: Payload<C>) -> Token<H, C> {
|
pub fn new(header: Header<H>, payload: Payload<C>) -> Token<H, C> {
|
||||||
Token {
|
Token {
|
||||||
|
@ -89,8 +87,8 @@ impl<H, C> Token<H, C>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<H, C> PartialEq for Token<H, C>
|
impl<H, C> PartialEq for Token<H, C>
|
||||||
where H: Serialize + Deserialize + PartialEq,
|
where H: PartialEq,
|
||||||
C: Serialize + Deserialize + PartialEq
|
C: PartialEq
|
||||||
{
|
{
|
||||||
fn eq(&self, other: &Token<H, C>) -> bool {
|
fn eq(&self, other: &Token<H, C>) -> bool {
|
||||||
self.header == other.header && self.payload == other.payload
|
self.header == other.header && self.payload == other.payload
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
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::{Deserialize, Serialize};
|
use serde::Serialize;
|
||||||
|
use serde::de::DeserializeOwned;
|
||||||
use serde_json;
|
use serde_json;
|
||||||
use serde_json::value::Value;
|
use serde_json::value::Value;
|
||||||
use super::Result;
|
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
|
/// 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: Serialize + Deserialize + PartialEq> {
|
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")]
|
||||||
|
@ -32,7 +33,7 @@ pub struct Payload<T: Serialize + Deserialize + PartialEq> {
|
||||||
/// satisfies Claims' generic parameter as simply and clearly as possible.
|
/// satisfies Claims' generic parameter as simply and clearly as possible.
|
||||||
pub type DefaultPayload = Payload<()>;
|
pub type DefaultPayload = Payload<()>;
|
||||||
|
|
||||||
impl<T: Serialize + Deserialize + PartialEq> Payload<T> {
|
impl<T: Serialize + DeserializeOwned> Payload<T> {
|
||||||
/// This implementation simply parses the base64 data twice, first parsing out the standard
|
/// 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
|
/// claims then any custom claims, assigning the latter into a copy of the former before
|
||||||
/// returning registered and custom claims.
|
/// returning registered and custom claims.
|
||||||
|
|
Loading…
Reference in a new issue