2019-01-31 23:11:20 -05: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};
|
2019-01-31 23:11:20 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
|
2019-07-06 14:36:32 -04:00
|
|
|
pub struct Claims {
|
2019-01-31 23:11:20 -05:00
|
|
|
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");
|
|
|
|
|
2020-01-13 13:38:33 -05:00
|
|
|
let encrypted =
|
2021-02-19 15:04:17 -05:00
|
|
|
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)
|
2020-01-13 13:38:33 -05:00
|
|
|
.unwrap();
|
2019-12-29 15:50:06 -05:00
|
|
|
assert!(is_valid);
|
|
|
|
}
|
2019-01-31 23:11:20 -05:00
|
|
|
|
2019-11-03 07:22:51 -05:00
|
|
|
#[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");
|
2021-02-19 15:04:17 -05:00
|
|
|
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();
|
2019-11-03 07:22:51 -05:00
|
|
|
assert!(is_valid);
|
|
|
|
}
|
|
|
|
|
2019-01-31 23:11:20 -05:00
|
|
|
#[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");
|
2019-01-31 23:11:20 -05:00
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_string(),
|
|
|
|
company: "ACME".to_string(),
|
|
|
|
exp: Utc::now().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();
|
2019-01-31 23:11:20 -05:00
|
|
|
assert_eq!(my_claims, token_data.claims);
|
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() {
|
|
|
|
// 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");
|
2019-11-15 14:16:38 -05:00
|
|
|
let my_claims = Claims {
|
|
|
|
sub: "b@b.com".to_string(),
|
|
|
|
company: "ACME".to_string(),
|
|
|
|
exp: Utc::now().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();
|
2019-11-15 14:16:38 -05:00
|
|
|
assert_eq!(my_claims, token_data.claims);
|
2019-01-31 23:11:20 -05:00
|
|
|
}
|