Add API tokens support #9
@ -83,18 +83,58 @@ mod test {
|
||||
exp: u64,
|
||||
}
|
||||
|
||||
impl Default for Claims {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sub: "my-sub".to_string(),
|
||||
exp: time() + 100,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jwt_encode_sign_verify_valid() {
|
||||
let (pub_key, priv_key) = generate_key_pair().unwrap();
|
||||
let claims = Claims {
|
||||
sub: "my-sub".to_string(),
|
||||
exp: time() + 100,
|
||||
};
|
||||
let claims = Claims::default();
|
||||
let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!");
|
||||
let claims_out = validate_jwt(&pub_key, &jwt).expect("Failed to validate JWT!");
|
||||
|
||||
assert_eq!(claims, claims_out)
|
||||
}
|
||||
|
||||
// TODO : complete tests
|
||||
#[test]
|
||||
fn jwt_encode_sign_verify_invalid_key() {
|
||||
let (_pub_key, priv_key) = generate_key_pair().unwrap();
|
||||
let (pub_key_2, _priv_key_2) = generate_key_pair().unwrap();
|
||||
let claims = Claims::default();
|
||||
let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!");
|
||||
validate_jwt::<Claims>(&pub_key_2, &jwt).expect_err("JWT should not have validated!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jwt_verify_random_string() {
|
||||
let (pub_key, _priv_key) = generate_key_pair().unwrap();
|
||||
validate_jwt::<Claims>(&pub_key, "random_string")
|
||||
.expect_err("JWT should not have validated!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jwt_expired() {
|
||||
let (pub_key, priv_key) = generate_key_pair().unwrap();
|
||||
let claims = Claims {
|
||||
exp: time() - 100,
|
||||
..Default::default()
|
||||
};
|
||||
let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!");
|
||||
validate_jwt::<Claims>(&pub_key, &jwt).expect_err("JWT should not have validated!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jwt_invalid_signature() {
|
||||
let (pub_key, priv_key) = generate_key_pair().unwrap();
|
||||
let claims = Claims::default();
|
||||
let jwt = sign_jwt(&priv_key, &claims).expect("Failed to sign JWT!");
|
||||
validate_jwt::<Claims>(&pub_key, &format!("{jwt}bad"))
|
||||
.expect_err("JWT should not have validated!");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user