Take ref to Header, not by value
This commit is contained in:
parent
67497950c6
commit
c244e835e0
|
@ -29,7 +29,7 @@ fn main() {
|
||||||
company: "ACME".to_owned()
|
company: "ACME".to_owned()
|
||||||
};
|
};
|
||||||
let key = "secret";
|
let key = "secret";
|
||||||
let token = match encode(Header::default(), &my_claims, key.as_ref()) {
|
let token = match encode(&Header::default(), &my_claims, key.as_ref()) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(_) => panic!() // in practice you would return the error
|
Err(_) => panic!() // in practice you would return the error
|
||||||
};
|
};
|
||||||
|
|
|
@ -23,7 +23,7 @@ fn main() {
|
||||||
header.kid = Some("signing_key".to_owned());
|
header.kid = Some("signing_key".to_owned());
|
||||||
header.alg = Algorithm::HS512;
|
header.alg = Algorithm::HS512;
|
||||||
|
|
||||||
let token = match encode(header, &my_claims, key.as_ref()) {
|
let token = match encode(&header, &my_claims, key.as_ref()) {
|
||||||
Ok(t) => t,
|
Ok(t) => t,
|
||||||
Err(_) => panic!() // in practice you would return the error
|
Err(_) => panic!() // in practice you would return the error
|
||||||
};
|
};
|
||||||
|
|
|
@ -72,7 +72,7 @@ pub fn sign(signing_input: &str, key: &[u8], algorithm: Algorithm) -> Result<Str
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode the claims passed and sign the payload using the algorithm from the header and the key
|
/// Encode the claims passed and sign the payload using the algorithm from the header and the key
|
||||||
pub fn encode<T: Serialize>(header: Header, claims: &T, key: &[u8]) -> Result<String> {
|
pub fn encode<T: Serialize>(header: &Header, claims: &T, key: &[u8]) -> Result<String> {
|
||||||
let encoded_header = to_jwt_part(&header)?;
|
let encoded_header = to_jwt_part(&header)?;
|
||||||
let encoded_claims = to_jwt_part(&claims)?;
|
let encoded_claims = to_jwt_part(&claims)?;
|
||||||
let signing_input = [encoded_header.as_ref(), encoded_claims.as_ref()].join(".");
|
let signing_input = [encoded_header.as_ref(), encoded_claims.as_ref()].join(".");
|
||||||
|
|
|
@ -15,13 +15,13 @@ pub struct TokenData<T: Deserialize> {
|
||||||
pub claims: T
|
pub claims: T
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes and encodes to base64
|
/// Serializes to JSON and encodes to base64
|
||||||
pub fn to_jwt_part<T: Serialize>(input: &T) -> Result<String> {
|
pub fn to_jwt_part<T: Serialize>(input: &T) -> Result<String> {
|
||||||
let encoded = serde_json::to_string(input)?;
|
let encoded = serde_json::to_string(input)?;
|
||||||
Ok(base64::encode_config(encoded.as_bytes(), base64::URL_SAFE_NO_PAD))
|
Ok(base64::encode_config(encoded.as_bytes(), base64::URL_SAFE_NO_PAD))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decodes from base64 and deserializes
|
/// Decodes from base64 and deserializes from JSON
|
||||||
pub fn from_jwt_part<B: AsRef<str>, T: Deserialize>(encoded: B) -> Result<T> {
|
pub fn from_jwt_part<B: AsRef<str>, T: Deserialize>(encoded: B) -> Result<T> {
|
||||||
let decoded = base64::decode_config(encoded.as_ref(), base64::URL_SAFE_NO_PAD)?;
|
let decoded = base64::decode_config(encoded.as_ref(), base64::URL_SAFE_NO_PAD)?;
|
||||||
let s = String::from_utf8(decoded)?;
|
let s = String::from_utf8(decoded)?;
|
||||||
|
|
|
@ -33,7 +33,7 @@ fn encode_with_custom_header() {
|
||||||
};
|
};
|
||||||
let mut header = Header::default();
|
let mut header = Header::default();
|
||||||
header.kid = Some("kid".to_string());
|
header.kid = Some("kid".to_string());
|
||||||
let token = encode(header, &my_claims, "secret".as_ref()).unwrap();
|
let token = encode(&header, &my_claims, "secret".as_ref()).unwrap();
|
||||||
let token_data = decode::<Claims>(&token, "secret".as_ref(), Algorithm::HS256).unwrap();
|
let token_data = decode::<Claims>(&token, "secret".as_ref(), Algorithm::HS256).unwrap();
|
||||||
assert_eq!(my_claims, token_data.claims);
|
assert_eq!(my_claims, token_data.claims);
|
||||||
assert_eq!("kid", token_data.header.kid.unwrap());
|
assert_eq!("kid", token_data.header.kid.unwrap());
|
||||||
|
@ -45,7 +45,7 @@ fn round_trip_claim() {
|
||||||
sub: "b@b.com".to_string(),
|
sub: "b@b.com".to_string(),
|
||||||
company: "ACME".to_string()
|
company: "ACME".to_string()
|
||||||
};
|
};
|
||||||
let token = encode(Header::default(), &my_claims, "secret".as_ref()).unwrap();
|
let token = encode(&Header::default(), &my_claims, "secret".as_ref()).unwrap();
|
||||||
let token_data = decode::<Claims>(&token, "secret".as_ref(), Algorithm::HS256).unwrap();
|
let token_data = decode::<Claims>(&token, "secret".as_ref(), Algorithm::HS256).unwrap();
|
||||||
assert_eq!(my_claims, token_data.claims);
|
assert_eq!(my_claims, token_data.claims);
|
||||||
assert!(token_data.header.kid.is_none());
|
assert!(token_data.header.kid.is_none());
|
||||||
|
|
|
@ -25,7 +25,7 @@ fn round_trip_claim() {
|
||||||
sub: "b@b.com".to_string(),
|
sub: "b@b.com".to_string(),
|
||||||
company: "ACME".to_string()
|
company: "ACME".to_string()
|
||||||
};
|
};
|
||||||
let token = encode(Header::new(Algorithm::RS256), &my_claims, include_bytes!("private_rsa_key.der")).unwrap();
|
let token = encode(&Header::new(Algorithm::RS256), &my_claims, include_bytes!("private_rsa_key.der")).unwrap();
|
||||||
let token_data = decode::<Claims>(&token, include_bytes!("public_rsa_key.der"), Algorithm::RS256).unwrap();
|
let token_data = decode::<Claims>(&token, include_bytes!("public_rsa_key.der"), Algorithm::RS256).unwrap();
|
||||||
assert_eq!(my_claims, token_data.claims);
|
assert_eq!(my_claims, token_data.claims);
|
||||||
assert!(token_data.header.kid.is_none());
|
assert!(token_data.header.kid.is_none());
|
||||||
|
|
Loading…
Reference in New Issue