jsonwebtoken/tests/ecdsa/mod.rs

97 lines
2.9 KiB
Rust
Raw Normal View History

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};
use time::OffsetDateTime;
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
2019-07-06 14:36:32 -04:00
pub struct Claims {
sub: String,
company: String,
exp: i64,
}
2019-12-29 15:50:06 -05:00
#[test]
fn round_trip_sign_verification_pk8() {
let privkey = include_bytes!("private_ecdsa_key.pk8");
let pubkey = include_bytes!("public_ecdsa_key.pk8");
let encrypted =
sign(b"hello world", &EncodingKey::from_ec_der(privkey), Algorithm::ES256).unwrap();
let is_valid =
verify(&encrypted, b"hello world", &DecodingKey::from_ec_der(pubkey), Algorithm::ES256)
.unwrap();
2019-12-29 15:50:06 -05:00
assert!(is_valid);
}
2022-01-28 16:37:40 -05:00
#[cfg(feature = "use_pem")]
#[test]
fn round_trip_sign_verification_pem() {
2019-12-29 15:50:06 -05:00
let privkey_pem = include_bytes!("private_ecdsa_key.pem");
let pubkey_pem = include_bytes!("public_ecdsa_key.pem");
let encrypted =
sign(b"hello world", &EncodingKey::from_ec_pem(privkey_pem).unwrap(), Algorithm::ES256)
.unwrap();
2019-12-29 15:50:06 -05:00
let is_valid = verify(
&encrypted,
2020-11-17 08:45:07 -05:00
b"hello world",
2019-12-29 15:50:06 -05:00
&DecodingKey::from_ec_pem(pubkey_pem).unwrap(),
Algorithm::ES256,
)
.unwrap();
assert!(is_valid);
}
2022-01-28 16:37:40 -05:00
#[cfg(feature = "use_pem")]
#[test]
fn round_trip_claim() {
2019-12-29 15:50:06 -05:00
let privkey_pem = include_bytes!("private_ecdsa_key.pem");
let pubkey_pem = include_bytes!("public_ecdsa_key.pem");
let my_claims = Claims {
sub: "b@b.com".to_string(),
company: "ACME".to_string(),
exp: OffsetDateTime::now_utc().unix_timestamp() + 10000,
};
2019-12-29 12:42:35 -05:00
let token = encode(
&Header::new(Algorithm::ES256),
&my_claims,
2019-12-29 15:50:06 -05:00
&EncodingKey::from_ec_pem(privkey_pem).unwrap(),
)
.unwrap();
let token_data = decode::<Claims>(
&token,
&DecodingKey::from_ec_pem(pubkey_pem).unwrap(),
&Validation::new(Algorithm::ES256),
2019-12-29 12:42:35 -05:00
)
.unwrap();
assert_eq!(my_claims, token_data.claims);
}
// https://jwt.io/ is often used for examples so ensure their example works with jsonwebtoken
2022-01-28 16:37:40 -05:00
#[cfg(feature = "use_pem")]
#[test]
fn roundtrip_with_jwtio_example() {
// We currently do not support SEC1 so we use the converted PKCS8 formatted
2019-12-29 15:50:06 -05:00
let privkey_pem = include_bytes!("private_jwtio_pkcs8.pem");
let pubkey_pem = include_bytes!("public_jwtio.pem");
let my_claims = Claims {
sub: "b@b.com".to_string(),
company: "ACME".to_string(),
exp: OffsetDateTime::now_utc().unix_timestamp() + 10000,
};
2019-12-29 12:42:35 -05:00
let token = encode(
&Header::new(Algorithm::ES384),
&my_claims,
2019-12-29 15:50:06 -05:00
&EncodingKey::from_ec_pem(privkey_pem).unwrap(),
)
.unwrap();
let token_data = decode::<Claims>(
&token,
&DecodingKey::from_ec_pem(pubkey_pem).unwrap(),
&Validation::new(Algorithm::ES384),
2019-12-29 12:42:35 -05:00
)
.unwrap();
assert_eq!(my_claims, token_data.claims);
}