use std::sync::Arc;
use secrecy::SecretString;
use super::AUTH_MODE_OAUTH_CLIENT_CREDENTIALS;
use super::provider::{OAuthError, TokenProvider};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AuthMode {
Static,
OAuthClientCredentials,
}
pub fn parse_auth_mode(raw: Option<&str>) -> Result<AuthMode, String> {
match raw.map(str::trim) {
None | Some("") | Some("bearer") | Some("header") => Ok(AuthMode::Static),
Some(AUTH_MODE_OAUTH_CLIENT_CREDENTIALS) => Ok(AuthMode::OAuthClientCredentials),
Some(other) => {
let shown: String = other.chars().filter(|c| !c.is_control()).take(40).collect();
Err(format!(
"unknown auth_mode '{shown}'; this runner understands bearer, header and \
{AUTH_MODE_OAUTH_CLIENT_CREDENTIALS}. The route is refused rather than \
sending its credential in a way that was not chosen"
))
}
}
}
#[derive(Clone)]
pub struct ClientCredentials {
pub client_id: String,
pub client_secret: SecretString,
}
impl std::fmt::Debug for ClientCredentials {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ClientCredentials")
.field("client_id", &self.client_id)
.field("client_secret", &"[REDACTED]")
.finish()
}
}
impl ClientCredentials {
pub fn from_secret_bytes(bytes: &[u8]) -> Result<Self, String> {
let text = std::str::from_utf8(bytes)
.map_err(|_| "the stored client credentials are not valid UTF-8".to_string())?;
let value: serde_json::Value = serde_json::from_str(text.trim()).map_err(|_| {
"the stored client credentials are not JSON; expected \
{\"client_id\":...,\"client_secret\":...}"
.to_string()
})?;
let field = |name: &str| {
value
.get(name)
.and_then(|v| v.as_str())
.map(str::trim)
.filter(|v| !v.is_empty())
.map(str::to_string)
.ok_or_else(|| format!("the stored client credentials have no non-empty {name}"))
};
Ok(Self {
client_id: field("client_id")?,
client_secret: SecretString::from(field("client_secret")?),
})
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct RouteAuthSpec<'a> {
pub auth_mode: Option<&'a str>,
pub oauth_endpoint: Option<&'a str>,
pub oauth_scope: Option<&'a str>,
pub oauth_audience: Option<&'a str>,
}
#[derive(Clone)]
pub struct RouteOAuth {
key: String,
endpoint: String,
scope: Option<String>,
audience: Option<String>,
credentials: ClientCredentials,
provider: Arc<TokenProvider>,
}
impl std::fmt::Debug for RouteOAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RouteOAuth")
.field("key", &self.key)
.field("endpoint", &self.endpoint)
.field("scope", &self.scope)
.field("audience", &self.audience)
.field("credentials", &self.credentials)
.finish()
}
}
impl RouteOAuth {
pub async fn token(&self) -> Result<SecretString, OAuthError> {
self.provider
.token_for(
&self.key,
&self.endpoint,
self.scope.as_deref(),
self.audience.as_deref(),
&self.credentials.client_id,
&self.credentials.client_secret,
)
.await
}
pub async fn invalidate(&self) {
self.provider.invalidate(&self.key).await;
}
#[must_use]
pub fn with_provider(mut self, provider: Arc<TokenProvider>) -> Self {
self.provider = provider;
self
}
pub fn key(&self) -> &str {
&self.key
}
}
#[derive(Debug, Clone)]
pub enum ResolvedAuth {
Static { token: Option<String> },
OAuth(RouteOAuth),
}
pub fn route_token_key(
category: &str,
tenant: &str,
team: Option<&str>,
unit: Option<&str>,
id: &str,
) -> String {
[
tenant,
team.filter(|t| !t.is_empty()).unwrap_or("_"),
unit.filter(|u| !u.is_empty()).unwrap_or("_"),
category,
id,
]
.join("\u{1f}")
}
pub fn resolve_route_auth(
spec: &RouteAuthSpec<'_>,
secret: Option<&str>,
key: String,
provider: Arc<TokenProvider>,
) -> Result<ResolvedAuth, String> {
match parse_auth_mode(spec.auth_mode)? {
AuthMode::Static => {
if let Some(secret) = secret
&& looks_like_client_credentials(secret)
{
return Err(
"the stored secret holds OAuth client credentials but the route's \
auth_mode is not oauth_client_credentials; refusing to send it as a token"
.to_string(),
);
}
Ok(ResolvedAuth::Static {
token: secret.map(str::to_string),
})
}
AuthMode::OAuthClientCredentials => {
let endpoint = spec
.oauth_endpoint
.map(str::trim)
.filter(|e| !e.is_empty())
.ok_or_else(|| {
"auth_mode oauth_client_credentials needs an oauth_endpoint".to_string()
})?;
let parsed = url::Url::parse(endpoint)
.map_err(|_| "oauth_endpoint is not a valid URL".to_string())?;
if !matches!(parsed.scheme(), "https" | "http") {
return Err("oauth_endpoint must be an https URL".to_string());
}
let secret = secret.ok_or_else(|| {
"auth_mode oauth_client_credentials needs stored client credentials, but none \
could be read"
.to_string()
})?;
let credentials = ClientCredentials::from_secret_bytes(secret.as_bytes())?;
let clean = |v: Option<&str>| {
v.map(str::trim)
.filter(|v| !v.is_empty() && !v.chars().any(char::is_control))
.map(str::to_string)
};
Ok(ResolvedAuth::OAuth(RouteOAuth {
key,
endpoint: endpoint.to_string(),
scope: clean(spec.oauth_scope),
audience: clean(spec.oauth_audience),
credentials,
provider,
}))
}
}
}
fn looks_like_client_credentials(secret: &str) -> bool {
let trimmed = secret.trim();
trimmed.starts_with('{')
&& serde_json::from_str::<serde_json::Value>(trimmed)
.ok()
.is_some_and(|v| v.get("client_secret").is_some())
}