2018-07-25 09:42:00 -04:00
|
|
|
use chrono::Utc;
|
2019-11-14 13:43:43 -05:00
|
|
|
use jsonwebtoken::{
|
|
|
|
crypto::{sign, verify},
|
2019-12-29 15:50:06 -05:00
|
|
|
decode, encode, Algorithm, DecodingKey, EncodingKey, Header, Validation,
|
2019-11-14 13:43:43 -05:00
|
|
|
};
|
2019-11-03 10:46:08 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2017-02-22 02:45:28 -05:00
|
|
|
|
2019-06-07 14:08:54 -04:00
|
|
|
const RSA_ALGORITHMS: &[Algorithm] = &[
|
|
|
|
Algorithm::RS256,
|
|
|
|
Algorithm::RS384,
|
|
|
|
Algorithm::RS512,
|
|
|
|
Algorithm::PS256,
|
|
|
|
Algorithm::PS384,
|
|
|
|
Algorithm::PS512,
|
|
|
|
];
|
2017-02-22 02:45:28 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
2019-07-06 14:36:32 -04:00
|
|
|
pub struct Claims {
|
2017-02-22 02:45:28 -05:00
|
|
|
sub: String,
|
2018-07-25 09:42:00 -04:00
|
|
|
company: String,
|
|
|
|
exp: i64,
|
2017-02-22 02:45:28 -05:00
|
|
|
}
|
|
|
|
|
2019-11-03 07:22:51 -05:00
|
|
|
#[test]
|
|
|
|
fn round_trip_sign_verification_pem_pkcs1() {
|
2019-11-06 17:32:13 -05:00
|
|
|
let privkey_pem = include_bytes!("private_rsa_key_pkcs1.pem");
|
|
|
|
let pubkey_pem = include_bytes!("public_rsa_key_pkcs1.pem");
|
2019-11-03 07:22:51 -05:00
|
|
|
|
|
|
|
for &alg in RSA_ALGORITHMS {
|
2019-12-29 12:42:35 -05:00
|
|
|
let encrypted =
|
2021-02-19 15:04:17 -05:00
|
|
|
sign(b"hello world", &EncodingKey::from_rsa_pem(privkey_pem).unwrap(), alg).unwrap();
|
2020-11-17 08:45:07 -05:00
|
|
|
let is_valid = verify(
|
|
|
|
&encrypted,
|
|
|
|
b"hello world",
|
|
|
|
&DecodingKey::from_rsa_pem(pubkey_pem).unwrap(),
|
|
|
|
alg,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-11-03 07:22:51 -05:00
|
|
|
assert!(is_valid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_sign_verification_pem_pkcs8() {
|
2019-11-06 17:32:13 -05:00
|
|
|
let privkey_pem = include_bytes!("private_rsa_key_pkcs8.pem");
|
|
|
|
let pubkey_pem = include_bytes!("public_rsa_key_pkcs8.pem");
|
2019-11-03 07:22:51 -05:00
|
|
|
|
|
|
|
for &alg in RSA_ALGORITHMS {
|
2019-12-29 12:42:35 -05:00
|
|
|
let encrypted =
|
2021-02-19 15:04:17 -05:00
|
|
|
sign(b"hello world", &EncodingKey::from_rsa_pem(privkey_pem).unwrap(), alg).unwrap();
|
2020-11-17 08:45:07 -05:00
|
|
|
let is_valid = verify(
|
|
|
|
&encrypted,
|
|
|
|
b"hello world",
|
|
|
|
&DecodingKey::from_rsa_pem(pubkey_pem).unwrap(),
|
|
|
|
alg,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-12-29 15:50:06 -05:00
|
|
|
assert!(is_valid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_sign_verification_der() {
|
|
|
|
let privkey_der = include_bytes!("private_rsa_key.der");
|
|
|
|
let pubkey_der = include_bytes!("public_rsa_key.der");
|
|
|
|
|
|
|
|
for &alg in RSA_ALGORITHMS {
|
2021-02-19 15:04:17 -05:00
|
|
|
let encrypted = sign(b"hello world", &EncodingKey::from_rsa_der(privkey_der), alg).unwrap();
|
|
|
|
let is_valid =
|
|
|
|
verify(&encrypted, b"hello world", &DecodingKey::from_rsa_der(pubkey_der), alg)
|
|
|
|
.unwrap();
|
2019-11-03 07:22:51 -05:00
|
|
|
assert!(is_valid);
|
2019-06-07 14:08:54 -04:00
|
|
|
}
|
2017-04-10 23:40:01 -04:00
|
|
|
}
|
2017-02-22 02:45:28 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn round_trip_claim() {
|
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_string(),
|
2018-07-25 09:42:00 -04:00
|
|
|
company: "ACME".to_string(),
|
|
|
|
exp: Utc::now().timestamp() + 10000,
|
2017-02-22 02:45:28 -05:00
|
|
|
};
|
2019-12-29 15:50:06 -05:00
|
|
|
let privkey_pem = include_bytes!("private_rsa_key_pkcs1.pem");
|
|
|
|
let pubkey_pem = include_bytes!("public_rsa_key_pkcs1.pem");
|
2019-06-16 12:00:00 -04:00
|
|
|
|
2019-06-07 14:08:54 -04:00
|
|
|
for &alg in RSA_ALGORITHMS {
|
2019-12-29 12:42:35 -05:00
|
|
|
let token =
|
2019-12-29 15:50:06 -05:00
|
|
|
encode(&Header::new(alg), &my_claims, &EncodingKey::from_rsa_pem(privkey_pem).unwrap())
|
2019-12-29 12:42:35 -05:00
|
|
|
.unwrap();
|
2019-06-16 12:00:00 -04:00
|
|
|
let token_data = decode::<Claims>(
|
|
|
|
&token,
|
2019-12-29 15:50:06 -05:00
|
|
|
&DecodingKey::from_rsa_pem(pubkey_pem).unwrap(),
|
2019-06-16 12:00:00 -04:00
|
|
|
&Validation::new(alg),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-06-07 14:08:54 -04:00
|
|
|
assert_eq!(my_claims, token_data.claims);
|
|
|
|
assert!(token_data.header.kid.is_none());
|
|
|
|
}
|
2017-02-22 02:45:28 -05:00
|
|
|
}
|
2019-05-15 10:20:25 -04:00
|
|
|
|
2019-11-09 06:42:40 -05:00
|
|
|
#[test]
|
|
|
|
fn rsa_modulus_exponent() {
|
|
|
|
let privkey = include_str!("private_rsa_key_pkcs1.pem");
|
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_string(),
|
|
|
|
company: "ACME".to_string(),
|
|
|
|
exp: Utc::now().timestamp() + 10000,
|
|
|
|
};
|
|
|
|
let n = "yRE6rHuNR0QbHO3H3Kt2pOKGVhQqGZXInOduQNxXzuKlvQTLUTv4l4sggh5_CYYi_cvI-SXVT9kPWSKXxJXBXd_4LkvcPuUakBoAkfh-eiFVMh2VrUyWyj3MFl0HTVF9KwRXLAcwkREiS3npThHRyIxuy0ZMeZfxVL5arMhw1SRELB8HoGfG_AtH89BIE9jDBHZ9dLelK9a184zAf8LwoPLxvJb3Il5nncqPcSfKDDodMFBIMc4lQzDKL5gvmiXLXB1AGLm8KBjfE8s3L5xqi-yUod-j8MtvIj812dkS4QMiRVN_by2h3ZY8LYVGrqZXZTcgn2ujn8uKjXLZVD5TdQ";
|
|
|
|
let e = "AQAB";
|
|
|
|
|
2019-12-29 12:42:35 -05:00
|
|
|
let encrypted = encode(
|
|
|
|
&Header::new(Algorithm::RS256),
|
|
|
|
&my_claims,
|
|
|
|
&EncodingKey::from_rsa_pem(privkey.as_ref()).unwrap(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-12-29 15:50:06 -05:00
|
|
|
let res = decode::<Claims>(
|
|
|
|
&encrypted,
|
2021-02-19 15:41:08 -05:00
|
|
|
&DecodingKey::from_rsa_components(n, e).unwrap(),
|
2019-12-29 15:50:06 -05:00
|
|
|
&Validation::new(Algorithm::RS256),
|
|
|
|
);
|
2019-11-09 06:42:40 -05:00
|
|
|
assert!(res.is_ok());
|
|
|
|
}
|
2019-11-03 10:36:19 -05:00
|
|
|
|
2019-11-15 14:16:38 -05:00
|
|
|
// https://jwt.io/ is often used for examples so ensure their example works with jsonwebtoken
|
|
|
|
#[test]
|
|
|
|
fn roundtrip_with_jwtio_example_jey() {
|
|
|
|
let privkey_pem = include_bytes!("private_jwtio.pem");
|
|
|
|
let pubkey_pem = include_bytes!("public_jwtio.pem");
|
|
|
|
|
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_string(),
|
|
|
|
company: "ACME".to_string(),
|
|
|
|
exp: Utc::now().timestamp() + 10000,
|
|
|
|
};
|
|
|
|
|
|
|
|
for &alg in RSA_ALGORITHMS {
|
2019-12-29 12:42:35 -05:00
|
|
|
let token =
|
|
|
|
encode(&Header::new(alg), &my_claims, &EncodingKey::from_rsa_pem(privkey_pem).unwrap())
|
|
|
|
.unwrap();
|
2019-12-29 15:50:06 -05:00
|
|
|
let token_data = decode::<Claims>(
|
|
|
|
&token,
|
|
|
|
&DecodingKey::from_rsa_pem(pubkey_pem).unwrap(),
|
|
|
|
&Validation::new(alg),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-11-15 14:16:38 -05:00
|
|
|
assert_eq!(my_claims, token_data.claims);
|
|
|
|
}
|
|
|
|
}
|