use jwt_lab::{Jwk, Jwks, Key, Algorithm};
#[test]
fn test_oct_jwk() {
let jwk = Jwk {
kty: "oct".to_string(),
kid: Some("test-key".to_string()),
alg: Some("HS256".to_string()),
use_: Some("sig".to_string()),
n: None,
e: None,
d: None,
p: None,
q: None,
dp: None,
dq: None,
qi: None,
crv: None,
x: None,
y: None,
k: Some("dGVzdC1zZWNyZXQ".to_string()), };
let key = jwk.to_key_for(Algorithm::HS256).unwrap();
match key {
Key::Hs(bytes) => {
assert_eq!(bytes, b"test-secret");
}
_ => panic!("Expected HS key"),
}
}
#[test]
fn test_jwks_selection() {
let jwks = Jwks {
keys: vec![
Jwk {
kty: "oct".to_string(),
kid: Some("key1".to_string()),
alg: Some("HS256".to_string()),
use_: Some("sig".to_string()),
n: None,
e: None,
d: None,
p: None,
q: None,
dp: None,
dq: None,
qi: None,
crv: None,
x: None,
y: None,
k: Some("dGVzdC1rZXkx".to_string()), },
Jwk {
kty: "oct".to_string(),
kid: Some("key2".to_string()),
alg: Some("HS256".to_string()),
use_: Some("sig".to_string()),
n: None,
e: None,
d: None,
p: None,
q: None,
dp: None,
dq: None,
qi: None,
crv: None,
x: None,
y: None,
k: Some("dGVzdC1rZXky".to_string()), },
],
};
let key1 = jwks.select_for("key1", Algorithm::HS256).unwrap();
let key2 = jwks.select_for("key2", Algorithm::HS256).unwrap();
match (key1, key2) {
(Key::Hs(bytes1), Key::Hs(bytes2)) => {
assert_eq!(bytes1, b"test-key1");
assert_eq!(bytes2, b"test-key2");
}
_ => panic!("Expected HS keys"),
}
assert!(jwks.select_for("missing", Algorithm::HS256).is_err());
}