diff --git a/pkgs/idp-provider/examples/test-rp.rs b/pkgs/idp-provider/examples/test-rp.rs index 74ab36b455..c1342c9284 100644 --- a/pkgs/idp-provider/examples/test-rp.rs +++ b/pkgs/idp-provider/examples/test-rp.rs @@ -185,14 +185,7 @@ fn main() { } let jwks: serde_json::Value = serde_json::from_str(&body).unwrap_or_else(|_| run("jwks is not JSON")); - let jwk = &jwks["keys"][0]; - let kid = jwk["kid"].as_str().unwrap_or("").to_string(); let decode = |s: &str| B64URL.decode(s).unwrap_or_default(); - let pubkey = RsaPublicKey::new( - BigUint::from_bytes_be(&decode(jwk["n"].as_str().unwrap_or(""))), - BigUint::from_bytes_be(&decode(jwk["e"].as_str().unwrap_or(""))), - ) - .unwrap_or_else(|_| run("JWKS key is not a valid RSA public key")); // 3. Authorize (local door) with PKCE. let verifier = random_b64url(32); @@ -265,9 +258,25 @@ fn main() { } let header: serde_json::Value = serde_json::from_slice(&decode(parts[0])).unwrap_or_else(|_| run("bad jwt header")); - if header["alg"] != "RS256" || header["kid"] != kid { - run("id_token header alg/kid does not match JWKS"); + if header["alg"] != "RS256" { + run("id_token header alg is not RS256"); } + // Select the JWKS key by the token's kid: during a rollover + // overlap the set holds several keys, and keys[0] is not + // necessarily the one that signed. + let kid = header["kid"].as_str().unwrap_or("").to_string(); + let jwk = jwks["keys"] + .as_array() + .and_then(|keys| { + keys.iter() + .find(|k| k["kid"].as_str() == Some(kid.as_str())) + }) + .unwrap_or_else(|| run("id_token kid is not in the JWKS")); + let pubkey = RsaPublicKey::new( + BigUint::from_bytes_be(&decode(jwk["n"].as_str().unwrap_or(""))), + BigUint::from_bytes_be(&decode(jwk["e"].as_str().unwrap_or(""))), + ) + .unwrap_or_else(|_| run("JWKS key is not a valid RSA public key")); let signature = rsa::pkcs1v15::Signature::try_from(decode(parts[2]).as_slice()) .unwrap_or_else(|_| run("bad signature encoding")); VerifyingKey::::new(pubkey)