use std::sync::Arc;
use std::time::{Duration, Instant};
use futures::future::BoxFuture;
use serde::Deserialize;
use crate::config::OidcConfig;
use crate::core::lifecycle::CloseGroup;
use crate::core::{Error, HealthStatus, HttpTransport, Result};
#[derive(Debug, Clone)]
pub struct OidcClient {
inner: Arc<Inner>,
}
#[derive(Debug)]
struct Inner {
config: OidcConfig,
transport: HttpTransport,
on_close: CloseGroup,
}
pub const DISCOVERY_PATH: &str = ".well-known/openid-configuration";
#[derive(Deserialize)]
struct Discovery {
issuer: Option<String>,
userinfo_endpoint: Option<String>,
}
pub fn relative_endpoint(base: &str, endpoint: &str) -> String {
let root = base.trim_end_matches('/');
if let Some(rest) = endpoint.strip_prefix(root).filter(|r| r.starts_with('/')) {
return rest.to_owned();
}
match url::Url::parse(endpoint) {
Ok(u) => match u.query() {
Some(q) => format!("{}?{q}", u.path()),
None => u.path().to_owned(),
},
Err(_) => endpoint.to_owned(),
}
}
impl OidcClient {
pub fn new(config: OidcConfig) -> Result<Self> {
config.validate()?;
let transport = HttpTransport::new(config.common())?;
if let Some(token) = config.token() {
transport.add_known_secret(token);
}
Ok(OidcClient {
inner: Arc::new(Inner {
config,
transport,
on_close: CloseGroup::new(),
}),
})
}
pub fn from_env() -> Result<Self> {
Self::new(OidcConfig::from_env()?)
}
pub fn config(&self) -> &OidcConfig {
&self.inner.config
}
pub fn transport(&self) -> &HttpTransport {
&self.inner.transport
}
pub fn on_close<F>(&self, f: F)
where
F: FnOnce() -> BoxFuture<'static, ()> + Send + 'static,
{
self.inner.on_close.register(f);
}
pub async fn health(&self) -> Result<HealthStatus> {
let resp = self
.transport()
.get(DISCOVERY_PATH)
.operation("health")
.ok_status(500)
.ok_status(503)
.send()
.await;
let resp = match resp {
Ok(resp) => resp,
Err(err) => return HealthStatus::from_error(err),
};
if resp.status() >= 300 {
return Ok(HealthStatus::unhealthy("unhealthy")
.with_detail("httpStatus", resp.status().to_string()));
}
let mut status = HealthStatus::healthy("ok");
if let Some(issuer) = resp.json::<Discovery>().ok().and_then(|d| d.issuer) {
status = status.with_detail("issuer", issuer);
}
Ok(status)
}
pub async fn ping(&self) -> Result<Duration> {
let started = Instant::now();
let doc = self
.transport()
.get(DISCOVERY_PATH)
.operation("ping")
.send()
.await?;
let endpoint = doc
.json::<Discovery>()?
.userinfo_endpoint
.filter(|e| !e.is_empty())
.ok_or_else(|| Error::unsupported("ping: the provider has no userinfo endpoint"))?;
let path = relative_endpoint(self.config().url().as_str(), &endpoint);
let mut req = self.transport().get(&path).operation("ping");
if let Some(token) = self.config().token() {
req = req.bearer(token);
}
req.send().await?;
Ok(started.elapsed())
}
pub async fn server_version(&self) -> Result<String> {
self.transport().ensure_open("serverVersion")?;
Err(Error::unsupported(
"serverVersion: OpenID Connect providers expose no version",
))
}
pub async fn close(&self) {
self.inner.on_close.close().await;
self.inner.transport.close();
}
pub fn is_closed(&self) -> bool {
self.inner.transport.is_closed()
}
}