2019-12-29 15:50:06 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2019-11-09 06:42:40 -05:00
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
|
2020-01-13 13:38:33 -05:00
|
|
|
use crate::algorithms::AlgorithmFamily;
|
2019-12-29 15:50:06 -05:00
|
|
|
use crate::crypto::verify;
|
2019-11-09 06:42:40 -05:00
|
|
|
use crate::errors::{new_error, ErrorKind, Result};
|
|
|
|
use crate::header::Header;
|
2019-12-29 15:50:06 -05:00
|
|
|
use crate::pem::decoder::PemEncodedKey;
|
2019-11-14 13:43:43 -05:00
|
|
|
use crate::serialization::from_jwt_part_claims;
|
2019-11-09 06:42:40 -05:00
|
|
|
use crate::validation::{validate, Validation};
|
|
|
|
|
2019-11-14 13:43:43 -05:00
|
|
|
/// The return type of a successful call to [decode](fn.decode.html).
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TokenData<T> {
|
|
|
|
/// The decoded JWT header
|
|
|
|
pub header: Header,
|
|
|
|
/// The decoded JWT claims
|
|
|
|
pub claims: T,
|
|
|
|
}
|
|
|
|
|
2019-11-09 06:42:40 -05:00
|
|
|
/// Takes the result of a rsplit and ensure we only get 2 parts
|
|
|
|
/// Errors if we don't
|
|
|
|
macro_rules! expect_two {
|
|
|
|
($iter:expr) => {{
|
|
|
|
let mut i = $iter;
|
|
|
|
match (i.next(), i.next(), i.next()) {
|
|
|
|
(Some(first), Some(second), None) => (first, second),
|
|
|
|
_ => return Err(new_error(ErrorKind::InvalidToken)),
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub(crate) enum DecodingKeyKind<'a> {
|
|
|
|
SecretOrDer(Cow<'a, [u8]>),
|
2020-02-27 12:26:58 -05:00
|
|
|
RsaModulusExponent { n: Cow<'a, str>, e: Cow<'a, str> },
|
2019-11-09 06:42:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
/// All the different kind of keys we can use to decode a JWT
|
|
|
|
/// This key can be re-used so make sure you only initialize it once if you can for better performance
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub struct DecodingKey<'a> {
|
2020-01-13 13:38:33 -05:00
|
|
|
pub(crate) family: AlgorithmFamily,
|
2019-12-29 15:50:06 -05:00
|
|
|
pub(crate) kind: DecodingKeyKind<'a>,
|
|
|
|
}
|
2019-11-09 06:42:40 -05:00
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
impl<'a> DecodingKey<'a> {
|
|
|
|
/// If you're using HMAC, use this.
|
|
|
|
pub fn from_secret(secret: &'a [u8]) -> Self {
|
2020-01-13 13:38:33 -05:00
|
|
|
DecodingKey {
|
|
|
|
family: AlgorithmFamily::Hmac,
|
|
|
|
kind: DecodingKeyKind::SecretOrDer(Cow::Borrowed(secret)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:20:41 -05:00
|
|
|
/// If you're using HMAC with a base64 encoded, use this.
|
|
|
|
pub fn from_base64_secret(secret: &str) -> Result<Self> {
|
|
|
|
let out = base64::decode(&secret)?;
|
|
|
|
Ok(DecodingKey {
|
|
|
|
family: AlgorithmFamily::Hmac,
|
|
|
|
kind: DecodingKeyKind::SecretOrDer(Cow::Owned(out)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If you are loading a public RSA key in a PEM format, use this.
|
|
|
|
pub fn from_rsa_pem(key: &'a [u8]) -> Result<Self> {
|
|
|
|
let pem_key = PemEncodedKey::new(key)?;
|
|
|
|
let content = pem_key.as_rsa_key()?;
|
|
|
|
Ok(DecodingKey {
|
|
|
|
family: AlgorithmFamily::Rsa,
|
|
|
|
kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
/// If you have (n, e) RSA public key components, use this.
|
|
|
|
pub fn from_rsa_components(modulus: &'a str, exponent: &'a str) -> Self {
|
2020-01-13 13:38:33 -05:00
|
|
|
DecodingKey {
|
|
|
|
family: AlgorithmFamily::Rsa,
|
2020-02-27 12:26:58 -05:00
|
|
|
kind: DecodingKeyKind::RsaModulusExponent {
|
|
|
|
n: Cow::Borrowed(modulus),
|
|
|
|
e: Cow::Borrowed(exponent),
|
|
|
|
},
|
2020-01-13 13:38:33 -05:00
|
|
|
}
|
2019-11-09 06:42:40 -05:00
|
|
|
}
|
|
|
|
|
2020-02-28 02:20:41 -05:00
|
|
|
/// If you have a ECDSA public key in PEM format, use this.
|
|
|
|
pub fn from_ec_pem(key: &'a [u8]) -> Result<Self> {
|
|
|
|
let pem_key = PemEncodedKey::new(key)?;
|
|
|
|
let content = pem_key.as_ec_public_key()?;
|
|
|
|
Ok(DecodingKey {
|
|
|
|
family: AlgorithmFamily::Ec,
|
|
|
|
kind: DecodingKeyKind::SecretOrDer(Cow::Owned(content.to_vec())),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-13 13:38:33 -05:00
|
|
|
/// If you know what you're doing and have a RSA DER encoded public key, use this.
|
|
|
|
pub fn from_rsa_der(der: &'a [u8]) -> Self {
|
|
|
|
DecodingKey {
|
|
|
|
family: AlgorithmFamily::Rsa,
|
|
|
|
kind: DecodingKeyKind::SecretOrDer(Cow::Borrowed(der)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If you know what you're doing and have a RSA EC encoded public key, use this.
|
|
|
|
pub fn from_ec_der(der: &'a [u8]) -> Self {
|
|
|
|
DecodingKey {
|
|
|
|
family: AlgorithmFamily::Ec,
|
|
|
|
kind: DecodingKeyKind::SecretOrDer(Cow::Borrowed(der)),
|
|
|
|
}
|
2019-12-29 15:50:06 -05:00
|
|
|
}
|
|
|
|
|
2020-02-27 12:26:58 -05:00
|
|
|
/// Convert self to `DecodingKey<'static>`.
|
|
|
|
pub fn into_static(self) -> DecodingKey<'static> {
|
|
|
|
use DecodingKeyKind::*;
|
|
|
|
let DecodingKey { family, kind } = self;
|
|
|
|
let static_kind = match kind {
|
|
|
|
SecretOrDer(key) => SecretOrDer(Cow::Owned(key.into_owned())),
|
|
|
|
RsaModulusExponent { n, e } => {
|
|
|
|
RsaModulusExponent { n: Cow::Owned(n.into_owned()), e: Cow::Owned(e.into_owned()) }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
DecodingKey { family, kind: static_kind }
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
pub(crate) fn as_bytes(&self) -> &[u8] {
|
|
|
|
match &self.kind {
|
|
|
|
DecodingKeyKind::SecretOrDer(b) => &b,
|
|
|
|
DecodingKeyKind::RsaModulusExponent { .. } => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2019-11-09 06:42:40 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
/// Decode and validate a JWT
|
2019-11-09 06:42:40 -05:00
|
|
|
///
|
|
|
|
/// If the token or its signature is invalid or the claims fail validation, it will return an error.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use serde::{Deserialize, Serialize};
|
2019-12-29 15:50:06 -05:00
|
|
|
/// use jsonwebtoken::{decode, DecodingKey, Validation, Algorithm};
|
2019-11-09 06:42:40 -05:00
|
|
|
///
|
|
|
|
/// #[derive(Debug, Serialize, Deserialize)]
|
|
|
|
/// struct Claims {
|
|
|
|
/// sub: String,
|
|
|
|
/// company: String
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let token = "a.jwt.token".to_string();
|
|
|
|
/// // Claims is a struct that implements Deserialize
|
2019-12-29 15:50:06 -05:00
|
|
|
/// let token_message = decode::<Claims>(&token, &DecodingKey::from_secret("secret".as_ref()), &Validation::new(Algorithm::HS256));
|
2019-11-09 06:42:40 -05:00
|
|
|
/// ```
|
|
|
|
pub fn decode<T: DeserializeOwned>(
|
|
|
|
token: &str,
|
2019-12-29 15:50:06 -05:00
|
|
|
key: &DecodingKey,
|
2019-11-09 06:42:40 -05:00
|
|
|
validation: &Validation,
|
|
|
|
) -> Result<TokenData<T>> {
|
2020-01-13 13:38:33 -05:00
|
|
|
for alg in &validation.algorithms {
|
|
|
|
if key.family != alg.family() {
|
|
|
|
return Err(new_error(ErrorKind::InvalidAlgorithm));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
let (signature, message) = expect_two!(token.rsplitn(2, '.'));
|
|
|
|
let (claims, header) = expect_two!(message.rsplitn(2, '.'));
|
|
|
|
let header = Header::from_encoded(header)?;
|
2019-11-09 06:42:40 -05:00
|
|
|
|
2019-12-29 15:50:06 -05:00
|
|
|
if !validation.algorithms.contains(&header.alg) {
|
|
|
|
return Err(new_error(ErrorKind::InvalidAlgorithm));
|
|
|
|
}
|
|
|
|
|
|
|
|
if !verify(signature, message, key, header.alg)? {
|
|
|
|
return Err(new_error(ErrorKind::InvalidSignature));
|
|
|
|
}
|
|
|
|
|
|
|
|
let (decoded_claims, claims_map): (T, _) = from_jwt_part_claims(claims)?;
|
|
|
|
validate(&claims_map, validation)?;
|
|
|
|
|
|
|
|
Ok(TokenData { header, claims: decoded_claims })
|
2019-11-09 06:42:40 -05:00
|
|
|
}
|
|
|
|
|
2019-11-14 13:43:43 -05:00
|
|
|
/// Decode a JWT without any signature verification/validations.
|
2019-11-09 06:42:40 -05:00
|
|
|
///
|
|
|
|
/// NOTE: Do not use this unless you know what you are doing! If the token's signature is invalid, it will *not* return an error.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use serde::{Deserialize, Serialize};
|
|
|
|
/// use jsonwebtoken::{dangerous_unsafe_decode, Validation, Algorithm};
|
|
|
|
///
|
|
|
|
/// #[derive(Debug, Serialize, Deserialize)]
|
|
|
|
/// struct Claims {
|
|
|
|
/// sub: String,
|
|
|
|
/// company: String
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let token = "a.jwt.token".to_string();
|
|
|
|
/// // Claims is a struct that implements Deserialize
|
|
|
|
/// let token_message = dangerous_unsafe_decode::<Claims>(&token);
|
|
|
|
/// ```
|
|
|
|
pub fn dangerous_unsafe_decode<T: DeserializeOwned>(token: &str) -> Result<TokenData<T>> {
|
|
|
|
let (_, message) = expect_two!(token.rsplitn(2, '.'));
|
|
|
|
let (claims, header) = expect_two!(message.rsplitn(2, '.'));
|
|
|
|
let header = Header::from_encoded(header)?;
|
|
|
|
|
|
|
|
let (decoded_claims, _): (T, _) = from_jwt_part_claims(claims)?;
|
|
|
|
|
|
|
|
Ok(TokenData { header, claims: decoded_claims })
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:43:43 -05:00
|
|
|
/// Decode a JWT without any signature verification/validations and return its [Header](struct.Header.html).
|
2019-11-09 06:42:40 -05:00
|
|
|
///
|
2019-11-14 13:43:43 -05:00
|
|
|
/// If the token has an invalid format (ie 3 parts separated by a `.`), it will return an error.
|
2019-11-09 06:42:40 -05:00
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use jsonwebtoken::decode_header;
|
|
|
|
///
|
|
|
|
/// let token = "a.jwt.token".to_string();
|
|
|
|
/// let header = decode_header(&token);
|
|
|
|
/// ```
|
|
|
|
pub fn decode_header(token: &str) -> Result<Header> {
|
|
|
|
let (_, message) = expect_two!(token.rsplitn(2, '.'));
|
|
|
|
let (_, header) = expect_two!(message.rsplitn(2, '.'));
|
|
|
|
Header::from_encoded(header)
|
|
|
|
}
|