Precompute headers
This commit is contained in:
parent
4ebcfca049
commit
0f41c31687
31
src/lib.rs
31
src/lib.rs
|
@ -19,7 +19,7 @@ use crypto::util::fixed_time_eq;
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
use errors::Error;
|
use errors::Error;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Copy, Clone, RustcDecodable, RustcEncodable)]
|
#[derive(Debug, PartialEq, Copy, Clone)]
|
||||||
/// The algorithms supported for signing/verifying
|
/// The algorithms supported for signing/verifying
|
||||||
pub enum Algorithm {
|
pub enum Algorithm {
|
||||||
HS256,
|
HS256,
|
||||||
|
@ -47,23 +47,46 @@ impl<T> Part for T where T: Encodable + Decodable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, RustcEncodable, RustcDecodable)]
|
#[derive(Debug, PartialEq)]
|
||||||
/// A basic JWT header part, the alg is automatically filled for use
|
/// A basic JWT header part, the alg is automatically filled for use
|
||||||
/// It's missing things like the kid but that's for later
|
/// It's missing things like the kid but that's for later
|
||||||
pub struct Header {
|
pub struct Header {
|
||||||
typ: String,
|
typ: &'static str,
|
||||||
alg: Algorithm,
|
alg: Algorithm,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Header {
|
impl Header {
|
||||||
pub fn new(algorithm: Algorithm) -> Header {
|
pub fn new(algorithm: Algorithm) -> Header {
|
||||||
Header {
|
Header {
|
||||||
typ: "JWT".to_owned(),
|
typ: "JWT",
|
||||||
alg: algorithm,
|
alg: algorithm,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Part for Header {
|
||||||
|
fn from_base64<B: AsRef<[u8]>>(encoded: B) -> Result<Self, Error> where Self: Sized {
|
||||||
|
let algoritm = match encoded.as_ref() {
|
||||||
|
b"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" => { Algorithm::HS256 },
|
||||||
|
b"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9" => { Algorithm::HS384 },
|
||||||
|
b"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9" => { Algorithm::HS512 },
|
||||||
|
_ => return Err(Error::InvalidToken)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(Header::new(algoritm))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_base64(&self) -> Result<String, Error> {
|
||||||
|
let encoded = match self.alg {
|
||||||
|
Algorithm::HS256 => { "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" },
|
||||||
|
Algorithm::HS384 => { "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9" },
|
||||||
|
Algorithm::HS512 => { "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9" },
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(encoded.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Take the payload of a JWT and sign it using the algorithm given.
|
/// Take the payload of a JWT and sign it using the algorithm given.
|
||||||
/// Returns the base64 url safe encoded of the hmac result
|
/// Returns the base64 url safe encoded of the hmac result
|
||||||
fn sign(data: &str, secret: &[u8], algorithm: Algorithm) -> String {
|
fn sign(data: &str, secret: &[u8], algorithm: Algorithm) -> String {
|
||||||
|
|
Loading…
Reference in New Issue