use std::fmt;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, OnceLock, PoisonError};
use std::time::{Duration, Instant};
use id_effect::{Effect, from_async};
use reqwest::Client;
use serde_json::Value;
use crate::error::AuthError;
use crate::jwt::{Algorithm, Jwk, JwtKey, select_key};
pub const DEFAULT_MIN_REFRESH_INTERVAL: Duration = Duration::from_secs(30);
pub const DEFAULT_MAX_AGE: Duration = Duration::from_secs(15 * 60);
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Clone, Debug)]
struct Config {
url: String,
min_refresh: Duration,
max_age: Duration,
timeout: Duration,
}
#[derive(Default)]
struct Cache {
keys: Arc<[JwtKey]>,
fetched_at: Option<Instant>,
attempted_at: Option<Instant>,
last_error: Option<String>,
}
#[derive(Default)]
struct Shared {
cache: Mutex<Cache>,
refreshing: tokio::sync::Mutex<()>,
http: OnceLock<Client>,
fetches: AtomicU64,
}
#[derive(Clone)]
pub struct JwksClient {
config: Arc<Config>,
shared: Arc<Shared>,
}
impl fmt::Debug for JwksClient {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JwksClient")
.field("url", &self.config.url)
.field("keys", &self.cache().keys.len())
.field("fetches", &self.fetch_count())
.finish()
}
}
impl JwksClient {
pub fn new(url: impl Into<String>) -> Self {
Self {
config: Arc::new(Config {
url: url.into(),
min_refresh: DEFAULT_MIN_REFRESH_INTERVAL,
max_age: DEFAULT_MAX_AGE,
timeout: DEFAULT_TIMEOUT,
}),
shared: Arc::new(Shared::default()),
}
}
#[must_use]
pub fn min_refresh_interval(mut self, interval: Duration) -> Self {
Arc::make_mut(&mut self.config).min_refresh = interval;
self
}
#[must_use]
pub fn max_age(mut self, max_age: Duration) -> Self {
Arc::make_mut(&mut self.config).max_age = max_age;
self
}
#[must_use]
pub fn timeout(mut self, timeout: Duration) -> Self {
Arc::make_mut(&mut self.config).timeout = timeout;
self
}
#[inline]
pub fn url(&self) -> &str {
&self.config.url
}
pub fn fetch_count(&self) -> u64 {
self.shared.fetches.load(Ordering::SeqCst)
}
pub fn cached_keys(&self) -> Vec<JwtKey> {
self.cache().keys.to_vec()
}
fn cache(&self) -> MutexGuard<'_, Cache> {
self
.shared
.cache
.lock()
.unwrap_or_else(PoisonError::into_inner)
}
pub fn refresh(&self) -> Effect<usize, AuthError> {
let this = self.clone();
from_async(move || async move {
let _running = this.shared.refreshing.lock().await;
this.fetch_and_store().await
})
}
pub fn key(&self, kid: Option<String>, alg: Algorithm) -> Effect<JwtKey, AuthError> {
let this = self.clone();
from_async(move || async move { this.lookup(kid.as_deref(), alg).await })
}
async fn lookup(&self, kid: Option<&str>, alg: Algorithm) -> Result<JwtKey, AuthError> {
if let Some(key) = self.fresh_key(kid, alg) {
return Ok(key);
}
let _running = self.shared.refreshing.lock().await;
if let Some(key) = self.fresh_key(kid, alg) {
return Ok(key);
}
let fetched = if self.may_fetch() {
Some(self.fetch_and_store().await)
} else {
None
};
let cache = self.cache();
select_key(&cache.keys, kid, alg).map_err(|unknown| match (fetched, &cache.last_error) {
(Some(Err(e)), _) => e,
(None, Some(e)) if cache.keys.is_empty() => AuthError::Unavailable(e.clone()),
_ => unknown,
})
}
fn fresh_key(&self, kid: Option<&str>, alg: Algorithm) -> Option<JwtKey> {
let cache = self.cache();
let fresh = cache
.fetched_at
.is_some_and(|t| t.elapsed() < self.config.max_age);
fresh.then(|| select_key(&cache.keys, kid, alg).ok())?
}
fn may_fetch(&self) -> bool {
let mut cache = self.cache();
let allowed = cache
.attempted_at
.is_none_or(|t| t.elapsed() >= self.config.min_refresh);
if allowed {
cache.attempted_at = Some(Instant::now());
}
allowed
}
async fn fetch_and_store(&self) -> Result<usize, AuthError> {
self.shared.fetches.fetch_add(1, Ordering::SeqCst);
let result = self.fetch().await;
let mut cache = self.cache();
cache.attempted_at = Some(Instant::now());
match result {
Ok(keys) => {
let n = keys.len();
cache.keys = keys.into();
cache.fetched_at = Some(Instant::now());
cache.last_error = None;
Ok(n)
}
Err(e) => {
cache.last_error = Some(e.to_string());
Err(e)
}
}
}
async fn fetch(&self) -> Result<Vec<JwtKey>, AuthError> {
let unavailable = |what: String| AuthError::Unavailable(format!("JWKS {what}"));
let client = match self.shared.http.get() {
Some(c) => c.clone(),
None => {
let built = Client::builder()
.timeout(self.config.timeout)
.build()
.map_err(|e| unavailable(format!("client: {e}")))?;
self.shared.http.get_or_init(|| built).clone()
}
};
let response = client
.get(&self.config.url)
.header(reqwest::header::ACCEPT, "application/json")
.send()
.await
.map_err(|e| unavailable(format!("request failed: {e}")))?;
let status = response.status();
if !status.is_success() {
return Err(unavailable(format!("endpoint answered {status}")));
}
let body = response
.bytes()
.await
.map_err(|e| unavailable(format!("body: {e}")))?;
parse_jwks(&body).map_err(unavailable)
}
}
fn parse_jwks(body: &[u8]) -> Result<Vec<JwtKey>, String> {
let doc: Value = serde_json::from_slice(body).map_err(|e| format!("is not JSON: {e}"))?;
let keys = doc
.get("keys")
.and_then(Value::as_array)
.ok_or_else(|| "has no \"keys\" array".to_owned())?;
Ok(
keys
.iter()
.filter(|k| k.get("use").and_then(Value::as_str) != Some("enc"))
.filter_map(|k| serde_json::from_value::<Jwk>(k.clone()).ok())
.filter_map(|jwk| JwtKey::from_jwk(&jwk).ok())
.collect(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn parse_skips_unusable_keys() {
let doc = json!({"keys": [
{"kty": "oct", "k": "c2VjcmV0", "kid": "h", "alg": "HS256"},
{"kty": "oct", "k": "c2VjcmV0", "kid": "enc", "use": "enc"},
{"kty": "weird", "kid": "w"},
{"kty": "oct", "k": "c2VjcmV0", "kid": "bad-alg", "alg": "RSA-OAEP"}
]});
let keys = parse_jwks(doc.to_string().as_bytes()).unwrap();
assert_eq!(keys.len(), 1);
assert_eq!(keys[0].kid(), Some("h"));
assert!(parse_jwks(b"nope").unwrap_err().contains("not JSON"));
assert!(parse_jwks(b"{}").unwrap_err().contains("keys"));
}
#[test]
fn builder_and_debug() {
let c = JwksClient::new("http://localhost/jwks")
.min_refresh_interval(Duration::from_secs(1))
.max_age(Duration::from_secs(2))
.timeout(Duration::from_secs(3));
assert_eq!(c.url(), "http://localhost/jwks");
assert_eq!(
(c.config.min_refresh, c.config.max_age, c.config.timeout),
(
Duration::from_secs(1),
Duration::from_secs(2),
Duration::from_secs(3)
)
);
assert_eq!(c.fetch_count(), 0);
assert!(c.cached_keys().is_empty());
assert!(format!("{c:?}").contains("localhost"));
}
}