use jsonwebtoken::jwk::{AlgorithmParameters, Jwk, JwkSet, KeyAlgorithm, PublicKeyUse};
use jsonwebtoken::DecodingKey;
use std::collections::HashMap;
use std::time::Duration;
const MAX_TTL: Duration = Duration::from_secs(3600);
const MIN_TTL: Duration = Duration::from_secs(60);
pub(crate) fn ttl_from_cache_control(cache_control: Option<&str>) -> Duration {
let Some(cc) = cache_control else {
return MAX_TTL;
};
for directive in cc.split(',') {
let directive = directive.trim();
if let Some(v) = directive
.strip_prefix("max-age=")
.or_else(|| directive.strip_prefix("Max-Age="))
.or_else(|| directive.strip_prefix("MAX-AGE="))
{
if let Ok(secs) = v.trim().parse::<u64>() {
return Duration::from_secs(secs).clamp(MIN_TTL, MAX_TTL);
}
}
}
MAX_TTL
}
fn admit_key(jwk: &Jwk) -> Option<(String, DecodingKey)> {
let kid = jwk.common.key_id.clone()?;
match &jwk.common.key_algorithm {
None | Some(KeyAlgorithm::RS256) => {}
Some(other) => {
tracing::debug!(
target: "plexus::auth",
kid = %kid,
alg = ?other,
"skipping JWKS key: non-RS256 algorithm label"
);
return None;
}
}
match &jwk.common.public_key_use {
None | Some(PublicKeyUse::Signature) => {}
Some(other) => {
tracing::debug!(
target: "plexus::auth",
kid = %kid,
key_use = ?other,
"skipping JWKS key: not a signature key"
);
return None;
}
}
match &jwk.algorithm {
AlgorithmParameters::RSA(_) => {}
other => {
tracing::debug!(
target: "plexus::auth",
kid = %kid,
kty = ?other,
"skipping JWKS key: non-RSA key type (RS256-only posture)"
);
return None;
}
}
match DecodingKey::from_jwk(jwk) {
Ok(key) => Some((kid, key)),
Err(e) => {
tracing::warn!(
target: "plexus::auth",
kid = %kid,
error = %e,
"skipping JWKS key: failed to build decoding key"
);
None
}
}
}
pub(crate) fn parse_jwks(body: &str) -> Result<HashMap<String, DecodingKey>, String> {
let set: JwkSet =
serde_json::from_str(body).map_err(|e| format!("JWKS parse error: {e}"))?;
Ok(set.keys.iter().filter_map(admit_key).collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ttl_defaults_to_one_hour_without_header() {
assert_eq!(ttl_from_cache_control(None), Duration::from_secs(3600));
assert_eq!(
ttl_from_cache_control(Some("no-store")),
Duration::from_secs(3600)
);
assert_eq!(
ttl_from_cache_control(Some("garbage")),
Duration::from_secs(3600)
);
}
#[test]
fn ttl_is_min_of_max_age_and_ceiling() {
assert_eq!(
ttl_from_cache_control(Some("public, max-age=600")),
Duration::from_secs(600)
);
assert_eq!(
ttl_from_cache_control(Some("max-age=86400")),
Duration::from_secs(3600)
);
assert_eq!(
ttl_from_cache_control(Some("max-age=0")),
Duration::from_secs(60)
);
assert_eq!(
ttl_from_cache_control(Some("max-age=59")),
Duration::from_secs(60)
);
}
#[test]
fn ttl_parses_max_age_among_other_directives() {
assert_eq!(
ttl_from_cache_control(Some("no-transform, max-age=120, must-revalidate")),
Duration::from_secs(120)
);
}
#[test]
fn parse_jwks_rejects_non_jwks_documents() {
assert!(parse_jwks("not json").is_err());
assert!(parse_jwks(r#"{"foo": 1}"#).is_err());
}
#[test]
fn parse_jwks_skips_keyless_kid_and_foreign_alg_entries() {
let body = r#"{
"keys": [
{"kty": "EC", "kid": "ec-1", "crv": "P-256",
"x": "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
"y": "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM"}
]
}"#;
let keys = parse_jwks(body).unwrap();
assert!(keys.is_empty());
}
}