1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Legacy support.

use crate::algorithm::{self, SigningAlgorithm, VerifyingAlgorithm};
use crate::error::Error;
use crate::token::verified::split_components;
use crate::{FromBase64, ToBase64, SEPARATOR};
use digest::generic_array::ArrayLength;
use digest::*;
use hmac::{Hmac, Mac};

pub use crate::legacy::claims::Claims;
pub use crate::legacy::claims::Registered;
pub use crate::legacy::header::Header;

pub mod claims;
pub mod header;

#[deprecated(note = "Please use jwt::Token instead")]
#[derive(Debug, Default)]
pub struct Token<H, C>
where
    H: Component,
    C: Component,
{
    raw: Option<String>,
    pub header: H,
    pub claims: C,
}

impl<H, C> Token<H, C>
where
    H: Component,
    C: Component,
{
    pub fn new(header: H, claims: C) -> Token<H, C> {
        Token {
            raw: None,
            header: header,
            claims: claims,
        }
    }

    /// Parse a token from a string.
    pub fn parse(raw: &str) -> Result<Token<H, C>, Error> {
        let components: Vec<_> = raw.split(SEPARATOR).collect();
        let (header, claims) = match &*components {
            [header, claims, _signature] => (
                Component::from_base64(header)?,
                Component::from_base64(claims)?,
            ),
            _ => return Err(Error::Format),
        };

        Ok(Token {
            raw: Some(raw.into()),
            header,
            claims,
        })
    }

    /// Verify a from_base64d token with a key and a given hashing algorithm.
    /// Make sure to check the token's algorithm before applying.
    pub fn verify<D>(&self, key: &[u8], _digest: D) -> bool
    where
        D: Input
            + BlockInput
            + FixedOutput
            + Reset
            + Default
            + Clone
            + algorithm::rust_crypto::TypeLevelAlgorithmType,
        D::BlockSize: ArrayLength<u8>,
        D::OutputSize: ArrayLength<u8>,
    {
        self.raw
            .as_ref()
            .ok_or(Error::Format)
            .and_then(|token| split_components(&*token))
            .and_then(|[header, claims, signature]| {
                // This will panic for bad key sizes. Returning an error
                // would probably be better, but for now, I want to keep the
                // API as stable as possible
                let hmac = Hmac::<D>::new_varkey(key).unwrap();
                VerifyingAlgorithm::verify(&hmac, &header, &claims, &signature)
            })
            .unwrap_or(false)
    }

    /// Generate the signed token from a key and a given hashing algorithm.
    pub fn signed<D>(&self, key: &[u8], _digest: D) -> Result<String, Error>
    where
        D: Input
            + BlockInput
            + FixedOutput
            + Reset
            + Default
            + Clone
            + algorithm::rust_crypto::TypeLevelAlgorithmType,
        D::BlockSize: ArrayLength<u8>,
        D::OutputSize: ArrayLength<u8>,
    {
        let data = [self.header.to_base64()?, self.claims.to_base64()?].join(SEPARATOR);

        // This will panic for bad key sizes. Returning an error
        // would probably be better, but for now, I want to keep the
        // API as stable as possible
        let hmac = Hmac::<D>::new_varkey(key).unwrap();
        let mut components = data.split(SEPARATOR);
        let header = components.next().unwrap();
        let claims = components.next().unwrap();
        let signature = SigningAlgorithm::sign(&hmac, header, claims).unwrap();

        let signed_token = [data, signature].join(SEPARATOR);

        Ok(signed_token)
    }
}

impl<H, C> PartialEq for Token<H, C>
where
    H: Component + PartialEq,
    C: Component + PartialEq,
{
    fn eq(&self, other: &Token<H, C>) -> bool {
        self.header == other.header && self.claims == other.claims
    }
}

#[deprecated(
    note = "This is usually implemented through a blanket impl, but if needed use the ToBase64 and FromBase64 traits"
)]
pub trait Component: Sized {
    fn from_base64<Input: ?Sized + AsRef<[u8]>>(raw: &Input) -> Result<Self, Error>;
    fn to_base64(&self) -> Result<String, Error>;
}

impl<T: ToBase64 + FromBase64> Component for T {
    /// Parse from a string.
    fn from_base64<Input: ?Sized + AsRef<[u8]>>(raw: &Input) -> Result<T, Error> {
        FromBase64::from_base64(raw)
    }

    /// Encode to a string.
    fn to_base64(&self) -> Result<String, Error> {
        ToBase64::to_base64(self).map(Into::<String>::into)
    }
}

#[cfg(test)]
mod tests {
    use crate::algorithm::AlgorithmType::Hs256;
    use crate::claims::Claims;
    use crate::header::Header;
    use crate::legacy::Token;
    use digest::Digest;
    use sha2::Sha256;

    #[test]
    pub fn raw_data() {
        let raw = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
        let token = Token::<Header, Claims>::parse(raw).unwrap();

        {
            assert_eq!(token.header.algorithm, Hs256);
        }
        assert!(token.verify("secret".as_bytes(), Sha256::new()));
    }

    #[test]
    pub fn roundtrip() {
        let token: Token<Header, Claims> = Default::default();
        let key = "secret".as_bytes();
        let raw = token.signed(key, Sha256::new()).unwrap();
        let same = Token::parse(&*raw).unwrap();

        assert_eq!(token, same);
        assert!(same.verify(key, Sha256::new()));
    }
}