use std::collections::HashMap;
use std::sync::{Arc, PoisonError, RwLock};
use std::time::{Duration, Instant};
use async_trait::async_trait;
use jsonwebtoken::jwk::{
AlgorithmParameters, EllipticCurve, Jwk, JwkSet, KeyAlgorithm, PublicKeyUse,
};
use jsonwebtoken::{Algorithm, DecodingKey, Validation, decode, decode_header, errors::ErrorKind};
use puerto_core::auth::{AuthError, Authenticator, Principal};
use serde::Deserialize;
use serde_json::{Map, Value};
use super::{OidcConfig, RolesClaim, claims};
pub const LEEWAY: Duration = Duration::from_secs(60);
pub const REFETCH_INTERVAL: Duration = Duration::from_secs(60);
const CONNECT_TIMEOUT: Duration = Duration::from_secs(5);
const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum OidcError {
#[error("could not reach the auth provider at {url}: {cause}")]
Unreachable { url: String, cause: String },
#[error("{url} is not an OpenID Connect provider's answer: {cause}")]
Invalid { url: String, cause: String },
#[error(
"AUTH_ISSUER is {configured} but the provider calls itself {discovered} — set AUTH_ISSUER to the provider's issuer"
)]
IssuerMismatch {
configured: String,
discovered: String,
},
#[error("{url} publishes no RS256 or ES256 signing key")]
NoSigningKey { url: String },
}
pub struct OidcAuthenticator {
http: reqwest::Client,
issuer: String,
audience: String,
jwks_uri: String,
roles_claim: RolesClaim,
keys: RwLock<HashMap<String, Arc<SigningKey>>>,
last_refetch: tokio::sync::Mutex<Option<Instant>>,
}
struct SigningKey {
key: DecodingKey,
algorithm: Algorithm,
}
#[derive(Deserialize)]
struct Discovery {
issuer: String,
jwks_uri: String,
}
enum FetchError {
Unreachable(String),
Invalid(String),
}
impl OidcAuthenticator {
pub async fn discover(config: OidcConfig) -> Result<Self, OidcError> {
let url = format!(
"{}/.well-known/openid-configuration",
config.issuer.trim_end_matches('/')
);
let http = reqwest::Client::builder()
.connect_timeout(CONNECT_TIMEOUT)
.timeout(REQUEST_TIMEOUT)
.build()
.map_err(|err| OidcError::Unreachable {
url: url.clone(),
cause: err.to_string(),
})?;
let discovery: Discovery = get_json(&http, &url).await.map_err(|err| err.at(&url))?;
if discovery.issuer.trim_end_matches('/') != config.issuer.trim_end_matches('/') {
return Err(OidcError::IssuerMismatch {
configured: config.issuer,
discovered: discovery.issuer,
});
}
let keys = fetch_keys(&http, &discovery.jwks_uri)
.await
.map_err(|err| err.at(&discovery.jwks_uri))?;
if keys.is_empty() {
return Err(OidcError::NoSigningKey {
url: discovery.jwks_uri,
});
}
tracing::info!(issuer = %discovery.issuer, keys = keys.len(), "oidc: provider discovered");
Ok(Self {
http,
issuer: discovery.issuer,
audience: config.audience,
jwks_uri: discovery.jwks_uri,
roles_claim: config.roles_claim,
keys: RwLock::new(keys),
last_refetch: tokio::sync::Mutex::new(None),
})
}
fn cached(&self, kid: &str) -> Option<Arc<SigningKey>> {
self.keys
.read()
.unwrap_or_else(PoisonError::into_inner)
.get(kid)
.cloned()
}
async fn key(&self, kid: &str) -> Result<Arc<SigningKey>, AuthError> {
if let Some(key) = self.cached(kid) {
return Ok(key);
}
let mut last_refetch = self.last_refetch.lock().await;
if let Some(key) = self.cached(kid) {
return Ok(key);
}
if last_refetch.is_some_and(|at| at.elapsed() < REFETCH_INTERVAL) {
tracing::debug!(kid, "oidc: unknown key, refetched less than a minute ago");
return Err(AuthError::Invalid);
}
*last_refetch = Some(Instant::now());
let keys = fetch_keys(&self.http, &self.jwks_uri)
.await
.map_err(|err| {
tracing::warn!(error = %err.at(&self.jwks_uri), "oidc: could not refetch the keys");
AuthError::Unavailable
})?;
*self.keys.write().unwrap_or_else(PoisonError::into_inner) = keys;
self.cached(kid).ok_or_else(|| {
tracing::debug!(kid, "oidc: unknown key, not in the refetched keys either");
AuthError::Invalid
})
}
fn validation(&self, algorithm: Algorithm) -> Validation {
let mut validation = Validation::new(algorithm);
validation.leeway = LEEWAY.as_secs();
validation.validate_nbf = true;
validation.set_issuer(&[&self.issuer]);
validation.set_audience(&[&self.audience]);
validation.set_required_spec_claims(&["exp", "iss", "aud", "sub"]);
validation
}
}
#[async_trait]
impl Authenticator for OidcAuthenticator {
async fn authenticate(&self, token: &str) -> Result<Principal, AuthError> {
let header = decode_header(token).map_err(|err| rejected(&err))?;
if !matches!(header.alg, Algorithm::RS256 | Algorithm::ES256) {
tracing::debug!(alg = ?header.alg, "oidc: token rejected, algorithm not allowed");
return Err(AuthError::Invalid);
}
let Some(kid) = header.kid else {
tracing::debug!("oidc: token rejected, no kid");
return Err(AuthError::Invalid);
};
let key = self.key(&kid).await?;
if key.algorithm != header.alg {
tracing::debug!(kid, alg = ?header.alg, "oidc: token rejected, not the key's algorithm");
return Err(AuthError::Invalid);
}
let claims = decode::<Map<String, Value>>(token, &key.key, &self.validation(header.alg))
.map_err(|err| rejected(&err))?
.claims;
claims::principal(&claims, &self.roles_claim)
}
}
fn rejected(err: &jsonwebtoken::errors::Error) -> AuthError {
tracing::debug!(error = %err, "oidc: token rejected");
match err.kind() {
ErrorKind::ExpiredSignature => AuthError::Expired,
_ => AuthError::Invalid,
}
}
impl FetchError {
fn at(self, url: &str) -> OidcError {
let url = url.to_string();
match self {
Self::Unreachable(cause) => OidcError::Unreachable { url, cause },
Self::Invalid(cause) => OidcError::Invalid { url, cause },
}
}
}
async fn get_json<T: serde::de::DeserializeOwned>(
http: &reqwest::Client,
url: &str,
) -> Result<T, FetchError> {
let response = http
.get(url)
.send()
.await
.map_err(|err| FetchError::Unreachable(err.to_string()))?;
let status = response.status();
if status.is_server_error() {
return Err(FetchError::Unreachable(format!("answered {status}")));
}
if !status.is_success() {
return Err(FetchError::Invalid(format!("answered {status}")));
}
response
.json()
.await
.map_err(|err| FetchError::Invalid(err.to_string()))
}
async fn fetch_keys(
http: &reqwest::Client,
url: &str,
) -> Result<HashMap<String, Arc<SigningKey>>, FetchError> {
let set: JwkSet = get_json(http, url).await?;
Ok(set
.keys
.iter()
.filter_map(|jwk| Some((jwk.common.key_id.clone()?, Arc::new(signing_key(jwk)?))))
.collect())
}
fn signing_key(jwk: &Jwk) -> Option<SigningKey> {
if matches!(jwk.common.public_key_use, Some(PublicKeyUse::Encryption)) {
return None;
}
let algorithm = match (&jwk.algorithm, jwk.common.key_algorithm) {
(AlgorithmParameters::RSA(_), None | Some(KeyAlgorithm::RS256)) => Algorithm::RS256,
(AlgorithmParameters::EllipticCurve(ec), None | Some(KeyAlgorithm::ES256))
if ec.curve == EllipticCurve::P256 =>
{
Algorithm::ES256
}
_ => return None,
};
let key = DecodingKey::from_jwk(jwk).ok()?;
Some(SigningKey { key, algorithm })
}