jsonwebtoken/src/lib.rs

299 lines
9.6 KiB
Rust
Raw Normal View History

2015-10-31 11:37:15 -04:00
//! Create and parses JWT (JSON Web Tokens)
//!
#![cfg_attr(feature = "dev", allow(unstable_features))]
#![cfg_attr(feature = "dev", feature(plugin))]
#![cfg_attr(feature = "dev", plugin(clippy))]
extern crate rustc_serialize;
2015-11-01 14:32:43 -05:00
extern crate crypto;
use rustc_serialize::{json, Encodable, Decodable};
use rustc_serialize::base64::{self, ToBase64, FromBase64};
use rustc_serialize::json::{ToJson, Json};
use crypto::sha2::{Sha256, Sha384, Sha512};
2015-11-01 14:32:43 -05:00
use crypto::hmac::Hmac;
use crypto::mac::Mac;
use crypto::digest::Digest;
use crypto::util::fixed_time_eq;
2015-10-31 11:37:15 -04:00
pub mod errors;
2015-11-01 14:32:43 -05:00
use errors::Error;
use std::collections::BTreeMap;
2015-10-31 11:37:15 -04:00
2015-12-18 16:07:48 -05:00
#[derive(Debug, PartialEq, Copy, Clone, RustcDecodable, RustcEncodable)]
/// The algorithms supported for signing/verifying
2015-10-31 11:37:15 -04:00
pub enum Algorithm {
2015-11-01 17:59:42 -05:00
HS256,
HS384,
HS512
2015-10-31 11:37:15 -04:00
}
impl ToJson for Algorithm {
fn to_json(&self) -> Json {
2016-03-29 11:28:57 -04:00
match *self {
Algorithm::HS256 => Json::String("HS256".to_owned()),
Algorithm::HS384 => Json::String("HS384".to_owned()),
Algorithm::HS512 => Json::String("HS512".to_owned()),
}
}
}
2015-11-01 17:59:42 -05:00
/// A part of the JWT: header and claims specifically
/// Allows converting from/to struct with base64
2015-11-01 14:32:43 -05:00
pub trait Part {
2015-11-05 14:34:08 -05:00
type Encoded: AsRef<str>;
fn from_base64<B: AsRef<[u8]>>(encoded: B) -> Result<Self, Error> where Self: Sized;
2015-11-05 14:34:08 -05:00
fn to_base64(&self) -> Result<Self::Encoded, Error>;
2015-11-01 14:32:43 -05:00
}
2015-10-31 11:37:15 -04:00
2015-11-01 14:32:43 -05:00
impl<T> Part for T where T: Encodable + Decodable {
2015-11-05 14:34:08 -05:00
type Encoded = String;
fn to_base64(&self) -> Result<Self::Encoded, Error> {
2015-11-01 14:32:43 -05:00
let encoded = try!(json::encode(&self));
2015-11-01 17:31:46 -05:00
Ok(encoded.as_bytes().to_base64(base64::URL_SAFE))
2015-11-01 14:32:43 -05:00
}
fn from_base64<B: AsRef<[u8]>>(encoded: B) -> Result<T, Error> {
let decoded = try!(encoded.as_ref().from_base64());
2015-11-01 14:32:43 -05:00
let s = try!(String::from_utf8(decoded));
Ok(try!(json::decode(&s)))
}
}
#[derive(Debug, PartialEq, RustcDecodable)]
/// A basic JWT header part, the alg defaults to HS256 and typ is automatically
/// set to `JWT`. All the other fields are optional
2015-11-01 14:32:43 -05:00
pub struct Header {
2015-12-18 16:07:48 -05:00
typ: String,
pub alg: Algorithm,
pub jku: Option<String>,
pub kid: Option<String>,
pub x5u: Option<String>,
pub x5t: Option<String>
2015-11-01 14:32:43 -05:00
}
impl Header {
pub fn new(algorithm: Algorithm) -> Header {
2015-11-01 14:32:43 -05:00
Header {
2015-12-18 16:07:48 -05:00
typ: "JWT".to_owned(),
2015-11-01 14:32:43 -05:00
alg: algorithm,
2015-12-18 16:07:48 -05:00
jku: None,
kid: None,
x5u: None,
x5t: None
2015-11-01 14:32:43 -05:00
}
}
}
2015-12-18 16:07:48 -05:00
impl Default for Header {
fn default() -> Header {
Header::new(Algorithm::HS256)
2015-11-05 14:29:42 -05:00
}
2015-12-18 16:07:48 -05:00
}
2015-11-05 14:29:42 -05:00
impl Encodable for Header {
fn encode<S: rustc_serialize::Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
self.to_json().encode(s)
}
}
impl ToJson for Header {
fn to_json(&self) -> Json {
let mut d = BTreeMap::new();
d.insert("typ".to_string(), self.typ.to_json());
d.insert("alg".to_string(), self.alg.to_json());
// Define a macro to reduce boilerplate.
macro_rules! optional {
($field_name:ident) => (
2016-03-29 11:28:57 -04:00
if let Some(ref value) = self.$field_name {
d.insert(stringify!($field_name).to_string(), value.to_json());
}
)
}
optional!(jku);
optional!(kid);
optional!(x5u);
optional!(x5t);
Json::Object(d)
}
}
2015-12-18 16:07:48 -05:00
#[derive(Debug)]
/// The return type of a successful call to decode(...)
2015-12-18 16:07:48 -05:00
pub struct TokenData<T: Part> {
pub header: Header,
pub claims: T
2015-11-05 14:29:42 -05:00
}
2015-11-01 17:59:42 -05:00
/// Take the payload of a JWT and sign it using the algorithm given.
/// Returns the base64 url safe encoded of the hmac result
2016-04-24 03:18:26 -04:00
pub fn sign(data: &str, secret: &[u8], algorithm: Algorithm) -> String {
fn crypt<D: Digest>(digest: D, data: &str, secret: &[u8]) -> String {
let mut hmac = Hmac::new(digest, secret);
hmac.input(data.as_bytes());
hmac.result().code().to_base64(base64::URL_SAFE)
}
match algorithm {
Algorithm::HS256 => crypt(Sha256::new(), data, secret),
Algorithm::HS384 => crypt(Sha384::new(), data, secret),
Algorithm::HS512 => crypt(Sha512::new(), data, secret),
}
2015-11-01 17:31:46 -05:00
}
2015-11-01 17:59:42 -05:00
/// Compares the signature given with a re-computed signature
2016-04-24 03:18:26 -04:00
pub fn verify(signature: &str, data: &str, secret: &[u8], algorithm: Algorithm) -> bool {
fixed_time_eq(signature.as_ref(), sign(data, secret, algorithm).as_ref())
2015-11-01 14:32:43 -05:00
}
/// Encode the claims passed and sign the payload using the algorithm from the header and the secret
pub fn encode<T: Part>(header: Header, claims: &T, secret: &[u8]) -> Result<String, Error> {
2015-12-18 16:07:48 -05:00
let encoded_header = try!(header.to_base64());
2015-11-01 14:32:43 -05:00
let encoded_claims = try!(claims.to_base64());
// seems to be a tiny bit faster than format!("{}.{}", x, y)
2015-12-18 16:07:48 -05:00
let payload = [encoded_header.as_ref(), encoded_claims.as_ref()].join(".");
let signature = sign(&*payload, secret.as_ref(), header.alg);
2015-11-01 17:31:46 -05:00
Ok([payload, signature].join("."))
2015-11-01 14:32:43 -05:00
}
2015-10-31 11:37:15 -04:00
/// Used in decode: 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; // evaluate the expr
match (i.next(), i.next(), i.next()) {
(Some(first), Some(second), None) => (first, second),
_ => return Err(Error::InvalidToken)
}
}}
}
2015-11-01 17:59:42 -05:00
/// Decode a token into a Claims struct
/// If the token or its signature is invalid, it will return an error
2015-12-18 16:07:48 -05:00
pub fn decode<T: Part>(token: &str, secret: &[u8], algorithm: Algorithm) -> Result<TokenData<T>, Error> {
2015-11-05 13:09:39 -05:00
let (signature, payload) = expect_two!(token.rsplitn(2, '.'));
2015-11-01 17:31:46 -05:00
let is_valid = verify(
2015-11-05 13:09:39 -05:00
signature,
payload,
2015-12-13 10:29:12 -05:00
secret,
2015-11-01 17:31:46 -05:00
algorithm
);
if !is_valid {
return Err(Error::InvalidSignature);
}
2015-11-05 13:09:39 -05:00
let (claims, header) = expect_two!(payload.rsplitn(2, '.'));
let header = try!(Header::from_base64(header));
if header.alg != algorithm {
2015-11-02 16:22:21 -05:00
return Err(Error::WrongAlgorithmHeader);
}
2015-12-18 16:07:48 -05:00
let decoded_claims = try!(T::from_base64(claims));
2015-11-01 17:59:42 -05:00
2015-12-18 16:07:48 -05:00
Ok(TokenData { header: header, claims: decoded_claims})
2015-11-01 17:31:46 -05:00
}
2015-10-31 11:37:15 -04:00
#[cfg(test)]
mod tests {
2015-12-18 16:07:48 -05:00
use super::{encode, decode, Algorithm, Header, sign, verify};
2015-11-01 14:32:43 -05:00
2015-11-01 17:31:46 -05:00
#[derive(Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
struct Claims {
sub: String,
company: String
}
2015-11-01 14:32:43 -05:00
#[test]
fn sign_hs256() {
2015-11-05 12:32:08 -05:00
let result = sign("hello world", b"secret", Algorithm::HS256);
2015-11-01 17:31:46 -05:00
let expected = "c0zGLzKEFWj0VxWuufTXiRMk5tlI5MbGDAYhzaxIYjo";
2015-11-01 14:32:43 -05:00
assert_eq!(result, expected);
}
2015-11-01 17:31:46 -05:00
#[test]
fn verify_hs256() {
let sig = "c0zGLzKEFWj0VxWuufTXiRMk5tlI5MbGDAYhzaxIYjo";
2015-11-05 12:32:08 -05:00
let valid = verify(sig, "hello world", b"secret", Algorithm::HS256);
assert!(valid);
2015-11-01 17:31:46 -05:00
}
2015-12-18 16:07:48 -05:00
#[test]
fn encode_with_custom_header() {
// TODO: test decode value
let my_claims = Claims {
sub: "b@b.com".to_owned(),
company: "ACME".to_owned()
};
let mut header = Header::default();
header.kid = Some("kid".to_owned());
let token = encode(header, &my_claims, "secret".as_ref()).unwrap();
2015-12-18 16:07:48 -05:00
let token_data = decode::<Claims>(&token, "secret".as_ref(), Algorithm::HS256).unwrap();
assert_eq!(my_claims, token_data.claims);
assert_eq!("kid", token_data.header.kid.unwrap());
}
2015-11-01 17:31:46 -05:00
#[test]
fn round_trip_claim() {
let my_claims = Claims {
sub: "b@b.com".to_owned(),
company: "ACME".to_owned()
};
let token = encode(Header::default(), &my_claims, "secret".as_ref()).unwrap();
2015-12-18 16:07:48 -05:00
let token_data = decode::<Claims>(&token, "secret".as_ref(), Algorithm::HS256).unwrap();
assert_eq!(my_claims, token_data.claims);
assert!(token_data.header.kid.is_none());
2015-11-01 17:31:46 -05:00
}
#[test]
fn decode_token() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.I1BvFoHe94AFf09O6tDbcSB8-jp8w6xZqmyHIwPeSdY";
let claims = decode::<Claims>(token, "secret".as_ref(), Algorithm::HS256);
claims.unwrap();
}
2015-11-01 17:31:46 -05:00
#[test]
2015-11-05 12:32:08 -05:00
#[should_panic(expected = "InvalidToken")]
2015-11-01 17:31:46 -05:00
fn decode_token_missing_parts() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
2015-12-13 10:29:12 -05:00
let claims = decode::<Claims>(token, "secret".as_ref(), Algorithm::HS256);
2015-11-05 12:32:08 -05:00
claims.unwrap();
2015-11-01 17:31:46 -05:00
}
#[test]
2015-11-05 12:32:08 -05:00
#[should_panic(expected = "InvalidSignature")]
2015-11-01 17:31:46 -05:00
fn decode_token_invalid_signature() {
let token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.wrong";
2015-12-13 10:29:12 -05:00
let claims = decode::<Claims>(token, "secret".as_ref(), Algorithm::HS256);
claims.unwrap();
}
#[test]
#[should_panic(expected = "WrongAlgorithmHeader")]
fn decode_token_wrong_algorithm() {
let token = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJiQGIuY29tIiwiY29tcGFueSI6IkFDTUUifQ.pKscJVk7-aHxfmQKlaZxh5uhuKhGMAa-1F5IX5mfUwI";
let claims = decode::<Claims>(token, "secret".as_ref(), Algorithm::HS256);
claims.unwrap();
}
2015-12-13 10:29:12 -05:00
#[test]
fn decode_token_with_bytes_secret() {
let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwiY29tcGFueSI6Ikdvb2dvbCJ9.27QxgG96vpX4akKNpD1YdRGHE3_u2X35wR3EHA2eCrs";
let claims = decode::<Claims>(token, b"\x01\x02\x03", Algorithm::HS256);
2015-12-18 16:07:48 -05:00
assert!(claims.is_ok());
}
#[test]
fn decode_token_with_shuffled_header_fields() {
let token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjb21wYW55IjoiMTIzNDU2Nzg5MCIsInN1YiI6IkpvaG4gRG9lIn0.SEIZ4Jg46VGhquuwPYDLY5qHF8AkQczF14aXM3a2c28";
let claims = decode::<Claims>(token, "secret".as_ref(), Algorithm::HS256);
assert!(claims.is_ok());
2015-11-01 17:31:46 -05:00
}
2015-10-31 11:37:15 -04:00
}