use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::{Duration, Instant};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
use dashmap::DashMap;
use futures::StreamExt;
use reqwest::header::{ACCEPT, AUTHORIZATION, HeaderValue};
use secrecy::{ExposeSecret, SecretString};
use sha2::{Digest, Sha256};
use tokio::sync::Mutex;
use super::cache::Slot;
const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);
const MAX_BODY_BYTES: usize = 64 * 1024;
const MAX_TOKEN_BYTES: usize = 8 * 1024;
const MIN_LIFETIME: Duration = Duration::from_secs(60);
const MAX_LIFETIME: Duration = Duration::from_secs(3600);
const DEFAULT_LIFETIME: Duration = Duration::from_secs(300);
const SKEW: Duration = Duration::from_secs(45);
const NEGATIVE_TTL: Duration = Duration::from_secs(15);
#[derive(Debug, Clone, thiserror::Error)]
pub enum OAuthError {
#[error("oauth token endpoint refused: {0}")]
EndpointRefused(String),
#[error("oauth token request failed: {0}")]
Transport(String),
#[error("oauth token endpoint answered HTTP {status}{}", code_suffix(.code))]
Status { status: u16, code: Option<String> },
#[error("oauth token response unusable: {0}")]
Malformed(String),
#[error("oauth token endpoint failed recently and is not being retried yet: {0}")]
Backoff(String),
}
fn code_suffix(code: &Option<String>) -> String {
match code {
Some(code) => format!(" ({code})"),
None => String::new(),
}
}
pub struct TokenProvider {
slots: DashMap<String, Arc<Mutex<Option<Slot>>>>,
clock: Arc<dyn Fn() -> Instant + Send + Sync>,
allow_loopback_http: AtomicBool,
}
impl std::fmt::Debug for TokenProvider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TokenProvider")
.field("cached_keys", &self.slots.len())
.finish_non_exhaustive()
}
}
impl Default for TokenProvider {
fn default() -> Self {
Self::new()
}
}
impl TokenProvider {
pub fn new() -> Self {
Self::with_clock(Arc::new(Instant::now))
}
pub(crate) fn with_clock(clock: Arc<dyn Fn() -> Instant + Send + Sync>) -> Self {
Self {
slots: DashMap::new(),
clock,
allow_loopback_http: AtomicBool::new(false),
}
}
pub fn shared() -> Arc<TokenProvider> {
static SHARED: OnceLock<Arc<TokenProvider>> = OnceLock::new();
Arc::clone(SHARED.get_or_init(|| Arc::new(TokenProvider::new())))
}
pub fn allow_loopback_http_for_tests(&self) {
self.allow_loopback_http.store(true, Ordering::SeqCst);
}
pub async fn token_for(
&self,
key: &str,
endpoint: &str,
scope: Option<&str>,
audience: Option<&str>,
client_id: &str,
client_secret: &SecretString,
) -> Result<SecretString, OAuthError> {
let fingerprint = fingerprint(endpoint, scope, audience, client_id, client_secret);
let slot = self
.slots
.entry(key.to_string())
.or_insert_with(|| Arc::new(Mutex::new(None)))
.clone();
let mut guard = slot.lock().await;
let now = (self.clock)();
if let Some(held) = guard.as_ref() {
if let Some(token) = held.live_token(&fingerprint, now) {
return Ok(token.clone());
}
if let Some(error) = held.recent_failure(&fingerprint, now) {
return Err(OAuthError::Backoff(error.to_string()));
}
}
let result = self
.exchange(endpoint, scope, audience, client_id, client_secret)
.await;
let now = (self.clock)();
match result {
Ok((token, lifetime)) => {
*guard = Some(Slot::Token {
fingerprint,
token: token.clone(),
valid_until: now + lifetime.saturating_sub(SKEW),
});
Ok(token)
}
Err(error) => {
if !matches!(error, OAuthError::EndpointRefused(_)) {
*guard = Some(Slot::Failed {
fingerprint,
until: now + NEGATIVE_TTL,
error: error.clone(),
});
}
Err(error)
}
}
}
pub async fn invalidate(&self, key: &str) {
let slot = self.slots.get(key).map(|entry| Arc::clone(entry.value()));
if let Some(slot) = slot {
*slot.lock().await = None;
}
}
async fn exchange(
&self,
endpoint: &str,
scope: Option<&str>,
audience: Option<&str>,
client_id: &str,
client_secret: &SecretString,
) -> Result<(SecretString, Duration), OAuthError> {
let (url, pinned) = self.vet_endpoint(endpoint).await?;
let mut builder = reqwest::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.timeout(REQUEST_TIMEOUT)
.connect_timeout(REQUEST_TIMEOUT);
if let (Some(host), Some(addrs)) = (url.host_str(), pinned.as_deref()) {
builder = builder.resolve_to_addrs(host, addrs);
}
let client = builder
.build()
.map_err(|e| OAuthError::Transport(scrub(&e)))?;
let mut form: Vec<(&str, &str)> = vec![("grant_type", "client_credentials")];
if let Some(scope) = scope.filter(|s| !s.is_empty()) {
form.push(("scope", scope));
}
if let Some(audience) = audience.filter(|a| !a.is_empty()) {
form.push(("audience", audience));
}
let response = client
.post(url)
.header(AUTHORIZATION, basic_header(client_id, client_secret)?)
.header(ACCEPT, "application/json")
.form(&form)
.send()
.await
.map_err(|e| OAuthError::Transport(scrub(&e)))?;
let status = response.status();
let body = read_capped(response).await?;
if !status.is_success() {
return Err(OAuthError::Status {
status: status.as_u16(),
code: error_code(&body),
});
}
parse_token_response(&body)
}
async fn vet_endpoint(
&self,
endpoint: &str,
) -> Result<(url::Url, Option<Vec<SocketAddr>>), OAuthError> {
let url = url::Url::parse(endpoint)
.map_err(|_| refused("the endpoint is not a valid URL".to_string()))?;
if !url.username().is_empty() || url.password().is_some() {
return Err(refused("the endpoint carries userinfo".to_string()));
}
let host = url
.host()
.ok_or_else(|| refused("the endpoint has no host".to_string()))?;
let port = url
.port_or_known_default()
.ok_or_else(|| refused("the endpoint has no port".to_string()))?;
let loopback_ok = self.allow_loopback_http.load(Ordering::SeqCst);
let (addrs, literal) = match host {
url::Host::Ipv4(ip) => (vec![SocketAddr::new(IpAddr::V4(ip), port)], true),
url::Host::Ipv6(ip) => (vec![SocketAddr::new(IpAddr::V6(ip), port)], true),
url::Host::Domain(name) => {
let resolved: Vec<SocketAddr> = tokio::net::lookup_host((name, port))
.await
.map_err(|_| refused(format!("the host '{name}' did not resolve")))?
.collect();
if resolved.is_empty() {
return Err(refused(format!("the host '{name}' did not resolve")));
}
(resolved, false)
}
};
let all_loopback = addrs.iter().all(|a| a.ip().is_loopback());
match url.scheme() {
"https" => {}
"http" if loopback_ok && all_loopback => {}
"http" => return Err(refused("only https endpoints are allowed".to_string())),
other => return Err(refused(format!("scheme '{other}' is not allowed"))),
}
for addr in &addrs {
if is_forbidden(addr.ip()) && !(loopback_ok && addr.ip().is_loopback()) {
return Err(refused(
"the endpoint resolves to a private, loopback or link-local address"
.to_string(),
));
}
}
Ok((url, (!literal).then_some(addrs)))
}
}
fn refused(reason: String) -> OAuthError {
OAuthError::EndpointRefused(reason)
}
fn scrub(error: &reqwest::Error) -> String {
let kind = if error.is_timeout() {
"timed out"
} else if error.is_connect() {
"could not connect"
} else if error.is_redirect() {
"redirects are not followed"
} else if error.is_body() || error.is_decode() {
"the response could not be read"
} else {
"request error"
};
kind.to_string()
}
fn basic_header(client_id: &str, client_secret: &SecretString) -> Result<HeaderValue, OAuthError> {
let enc = |s: &str| url::form_urlencoded::byte_serialize(s.as_bytes()).collect::<String>();
let raw = format!("{}:{}", enc(client_id), enc(client_secret.expose_secret()));
let mut value = HeaderValue::from_str(&format!("Basic {}", BASE64.encode(raw)))
.map_err(|_| OAuthError::Malformed("the client credentials are not encodable".into()))?;
value.set_sensitive(true);
Ok(value)
}
async fn read_capped(response: reqwest::Response) -> Result<Vec<u8>, OAuthError> {
if response
.content_length()
.is_some_and(|len| len > MAX_BODY_BYTES as u64)
{
return Err(OAuthError::Malformed(
"the response is larger than the allowed size".into(),
));
}
let mut stream = response.bytes_stream();
let mut body = Vec::new();
while let Some(chunk) = stream.next().await {
let chunk = chunk.map_err(|e| OAuthError::Transport(scrub(&e)))?;
if body.len() + chunk.len() > MAX_BODY_BYTES {
return Err(OAuthError::Malformed(
"the response is larger than the allowed size".into(),
));
}
body.extend_from_slice(&chunk);
}
Ok(body)
}
fn error_code(body: &[u8]) -> Option<String> {
let value: serde_json::Value = serde_json::from_slice(body).ok()?;
let code = value.get("error")?.as_str()?;
let plain = !code.is_empty()
&& code.len() <= 64
&& code
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '-' | '.'));
plain.then(|| code.to_string())
}
fn parse_token_response(body: &[u8]) -> Result<(SecretString, Duration), OAuthError> {
let value: serde_json::Value = serde_json::from_slice(body)
.map_err(|_| OAuthError::Malformed("the response is not JSON".into()))?;
let token = value
.get("access_token")
.and_then(|v| v.as_str())
.ok_or_else(|| OAuthError::Malformed("the response has no access_token".into()))?;
if token.is_empty()
|| token.len() > MAX_TOKEN_BYTES
|| token.chars().any(|c| c.is_control() || c.is_whitespace())
{
return Err(OAuthError::Malformed(
"the access_token cannot be sent as a header value".into(),
));
}
if let Some(kind) = value.get("token_type").and_then(|v| v.as_str())
&& !kind.eq_ignore_ascii_case("bearer")
{
return Err(OAuthError::Malformed("the token_type is not Bearer".into()));
}
let seconds = match value.get("expires_in") {
None | Some(serde_json::Value::Null) => None,
Some(serde_json::Value::Number(n)) => n.as_f64(),
Some(serde_json::Value::String(s)) => s.trim().parse::<f64>().ok(),
Some(_) => None,
};
let lifetime = seconds
.filter(|s| s.is_finite() && *s >= 0.0)
.map(Duration::from_secs_f64)
.unwrap_or(DEFAULT_LIFETIME)
.clamp(MIN_LIFETIME, MAX_LIFETIME);
Ok((SecretString::from(token.to_string()), lifetime))
}
fn fingerprint(
endpoint: &str,
scope: Option<&str>,
audience: Option<&str>,
client_id: &str,
client_secret: &SecretString,
) -> [u8; 32] {
let mut hasher = Sha256::new();
for part in [
endpoint,
scope.unwrap_or(""),
audience.unwrap_or(""),
client_id,
client_secret.expose_secret(),
] {
hasher.update((part.len() as u64).to_be_bytes());
hasher.update(part.as_bytes());
}
hasher.finalize().into()
}
pub(super) fn is_forbidden(ip: IpAddr) -> bool {
match ip {
IpAddr::V4(v4) => forbidden_v4(v4),
IpAddr::V6(v6) => match v6.to_ipv4_mapped() {
Some(mapped) => forbidden_v4(mapped),
None => forbidden_v6(v6),
},
}
}
fn forbidden_v4(ip: Ipv4Addr) -> bool {
let [a, b, c, _] = ip.octets();
ip.is_loopback()
|| ip.is_private()
|| ip.is_link_local()
|| ip.is_unspecified()
|| ip.is_broadcast()
|| ip.is_multicast()
|| a == 0
|| (a == 100 && (64..=127).contains(&b)) || (a == 192 && b == 0 && c == 0) || (a == 198 && (b == 18 || b == 19)) || a >= 240 }
fn forbidden_v6(ip: Ipv6Addr) -> bool {
let first = ip.segments()[0];
ip.is_loopback()
|| ip.is_unspecified()
|| ip.is_multicast()
|| (first & 0xfe00) == 0xfc00 || (first & 0xffc0) == 0xfe80 }