jwt-lab 0.1.1

JWT crate for Rust: decode, verify, sign, mutate, JWK/JWKS, algorithm selection, time validation, and secure APIs.
Documentation
//! JWK and JWKS functionality tests
//!
//! These tests verify that JSON Web Keys can be parsed, converted to internal
//! Key types, and selected from JWKS collections correctly.

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()), // "test-secret" in base64url
    };

    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()), // "test-key1" in base64url
            },
            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()), // "test-key2" in base64url
            },
        ],
    };

    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"),
    }

    // Test missing key
    assert!(jwks.select_for("missing", Algorithm::HS256).is_err());
}