mod response;
use std::collections::HashMap;
use std::time::Duration;
use reqwest::Client as HttpClient;
use reqwest::header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE};
use crate::client_auth::{OAuthErrorBody, basic_auth_header, body_snippet, read_capped_body};
use crate::token::ClientAuthMethod;
use crate::{IdentityError, Result};
pub use response::{Introspection, IntrospectionAudience};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
const REDACTED: &str = "<redacted>";
const RESERVED_PARAMS: &[&str] = &["token", "token_type_hint", "client_id", "client_secret"];
pub struct IntrospectionClient {
http: HttpClient,
introspection_endpoint: String,
client_id: String,
client_secret: String,
auth_method: ClientAuthMethod,
extra_params: HashMap<String, String>,
timeout: Duration,
allow_http: bool,
}
impl std::fmt::Debug for IntrospectionClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IntrospectionClient")
.field("introspection_endpoint", &self.introspection_endpoint)
.field("client_id", &self.client_id)
.field("client_secret", &REDACTED)
.field("auth_method", &self.auth_method)
.field("extra_params", &self.extra_params)
.field("timeout", &self.timeout)
.field("allow_http", &self.allow_http)
.finish_non_exhaustive()
}
}
impl IntrospectionClient {
pub fn builder() -> IntrospectionClientBuilder {
IntrospectionClientBuilder::new()
}
pub async fn introspect(
&self,
token: &str,
token_type_hint: Option<&str>,
) -> Result<Introspection> {
let scheme = self.introspection_endpoint.to_ascii_lowercase();
let scheme_ok =
scheme.starts_with("https://") || (self.allow_http && scheme.starts_with("http://"));
if !scheme_ok {
return Err(IdentityError::Configuration(format!(
"introspection endpoint {:?} must use https (enable allow_http for development)",
self.introspection_endpoint
)));
}
let mut form: Vec<(String, String)> = vec![("token".to_string(), token.to_string())];
if let Some(hint) = token_type_hint
&& !hint.is_empty()
{
form.push(("token_type_hint".to_string(), hint.to_string()));
}
let mut basic_header: Option<String> = None;
match self.auth_method {
ClientAuthMethod::ClientSecretPost => {
form.push(("client_id".to_string(), self.client_id.clone()));
form.push(("client_secret".to_string(), self.client_secret.clone()));
}
ClientAuthMethod::ClientSecretBasic => {
basic_header = Some(basic_auth_header(&self.client_id, &self.client_secret));
}
}
for (key, value) in &self.extra_params {
if RESERVED_PARAMS.contains(&key.as_str()) || form.iter().any(|(k, _)| k == key) {
continue;
}
form.push((key.clone(), value.clone()));
}
let mut request = self
.http
.post(&self.introspection_endpoint)
.timeout(self.timeout)
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.header(ACCEPT, "application/json")
.form(&form);
if let Some(header) = basic_header {
request = request.header(AUTHORIZATION, header);
}
let response = request.send().await.map_err(|e| {
IdentityError::Http(format!("post {}: {e}", self.introspection_endpoint))
})?;
let status = response.status();
let body = read_capped_body(response).await?;
if !status.is_success() {
if let Ok(err) = serde_json::from_slice::<OAuthErrorBody>(&body)
&& !err.error.is_empty()
{
return Err(IdentityError::TokenEndpoint {
error: err.error,
description: err.error_description,
error_uri: err.error_uri,
status: status.as_u16(),
});
}
return Err(IdentityError::Http(format!(
"introspection request to {} failed: HTTP {} with non-OAuth body: {}",
self.introspection_endpoint,
status.as_u16(),
body_snippet(&body)
)));
}
serde_json::from_slice(&body).map_err(|e| {
IdentityError::Deserialization(format!(
"parse introspection response from {}: {e}",
self.introspection_endpoint
))
})
}
}
#[derive(Default)]
pub struct IntrospectionClientBuilder {
http: Option<HttpClient>,
introspection_endpoint: Option<String>,
client_id: Option<String>,
client_secret: Option<String>,
auth_method: ClientAuthMethod,
extra_params: HashMap<String, String>,
timeout: Duration,
allow_http: bool,
}
impl std::fmt::Debug for IntrospectionClientBuilder {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IntrospectionClientBuilder")
.field("introspection_endpoint", &self.introspection_endpoint)
.field("client_id", &self.client_id)
.field(
"client_secret",
&self.client_secret.as_ref().map(|_| REDACTED),
)
.field("auth_method", &self.auth_method)
.field("extra_params", &self.extra_params)
.field("timeout", &self.timeout)
.field("allow_http", &self.allow_http)
.finish_non_exhaustive()
}
}
impl IntrospectionClientBuilder {
fn new() -> Self {
Self::default()
}
pub fn client_id(mut self, client_id: impl Into<String>) -> Self {
self.client_id = Some(client_id.into());
self
}
pub fn client_secret(mut self, client_secret: impl Into<String>) -> Self {
self.client_secret = Some(client_secret.into());
self
}
pub fn introspection_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.introspection_endpoint = Some(endpoint.into());
self
}
pub fn auth_method(mut self, method: ClientAuthMethod) -> Self {
self.auth_method = method;
self
}
pub fn extra_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.extra_params.insert(key.into(), value.into());
self
}
pub fn extra_params(mut self, params: HashMap<String, String>) -> Self {
self.extra_params.extend(params);
self
}
pub fn http_client(mut self, client: HttpClient) -> Self {
self.http = Some(client);
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
if !timeout.is_zero() {
self.timeout = timeout;
}
self
}
pub fn allow_http(mut self, allow: bool) -> Self {
self.allow_http = allow;
self
}
pub fn build(self) -> Result<IntrospectionClient> {
let client_id = self.client_id.unwrap_or_default();
if client_id.is_empty() {
return Err(IdentityError::Configuration(
"client_id is required".to_string(),
));
}
let client_secret = self.client_secret.unwrap_or_default();
if client_secret.is_empty() {
return Err(IdentityError::Configuration(
"client_secret is required: the introspection endpoint requires client authentication with both client_id and client_secret (RFC 7662 §2.1)"
.to_string(),
));
}
let introspection_endpoint = self.introspection_endpoint.unwrap_or_default();
if introspection_endpoint.is_empty() {
return Err(IdentityError::Configuration(
"introspection_endpoint is required".to_string(),
));
}
Ok(IntrospectionClient {
http: self.http.unwrap_or_else(crate::http::secure_client),
introspection_endpoint,
client_id,
client_secret,
auth_method: self.auth_method,
extra_params: self.extra_params,
timeout: if self.timeout.is_zero() {
DEFAULT_TIMEOUT
} else {
self.timeout
},
allow_http: self.allow_http,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ProviderMetadata;
use wiremock::matchers::{body_string_contains, header, header_exists, method, path};
use wiremock::{Mock, MockServer, Request, ResponseTemplate};
const ACTIVE_BODY: &str = r#"{"active":true,"scope":"read","client_id":"rs-client","token_type":"Bearer","exp":1419356238,"extension_field":"twenty-seven"}"#;
fn form_of(req: &Request) -> HashMap<String, String> {
url::form_urlencoded::parse(&req.body)
.into_owned()
.collect()
}
fn client(endpoint: &str) -> IntrospectionClientBuilder {
IntrospectionClient::builder()
.client_id("rs-client")
.client_secret("rs-secret")
.introspection_endpoint(endpoint)
.allow_http(true)
}
async fn mount(server: &MockServer, template: ResponseTemplate) {
Mock::given(method("POST"))
.and(path("/introspect"))
.respond_with(template)
.mount(server)
.await;
}
#[tokio::test]
async fn active_token_returns_typed_response() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(200).set_body_string(ACTIVE_BODY),
)
.await;
let ir = client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect("introspection succeeds");
assert!(ir.active);
assert_eq!(ir.scope.as_deref(), Some("read"));
assert_eq!(ir.client_id.as_deref(), Some("rs-client"));
assert_eq!(ir.token_type.as_deref(), Some("Bearer"));
assert_eq!(ir.exp, Some(1419356238));
assert_eq!(
ir.extra.get("extension_field").and_then(|v| v.as_str()),
Some("twenty-seven")
);
let requests = server.received_requests().await.unwrap();
let req = requests.last().unwrap();
assert_eq!(req.method.to_string(), "POST");
assert_eq!(
req.headers.get("content-type").unwrap().to_str().unwrap(),
"application/x-www-form-urlencoded"
);
let form = form_of(req);
assert_eq!(form.get("token").map(String::as_str), Some("the-token"));
}
#[tokio::test]
async fn inactive_token_returns_active_false() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(200).set_body_string(r#"{"active":false}"#),
)
.await;
let ir = client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("garbage", None)
.await
.expect("introspection succeeds");
assert!(!ir.active);
assert_eq!(ir.username, None);
}
#[tokio::test]
async fn client_secret_basic_is_default() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/introspect"))
.and(header_exists("authorization"))
.respond_with(ResponseTemplate::new(200).set_body_string(ACTIVE_BODY))
.mount(&server)
.await;
client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect("introspection succeeds");
let requests = server.received_requests().await.unwrap();
let req = requests.last().unwrap();
let auth = req.headers.get("authorization").unwrap().to_str().unwrap();
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
assert_eq!(
auth,
format!("Basic {}", BASE64_STANDARD.encode("rs-client:rs-secret"))
);
let form = form_of(req);
assert!(!form.contains_key("client_id"), "no client_id in body");
assert!(
!form.contains_key("client_secret"),
"no client_secret in body"
);
}
#[tokio::test]
async fn client_secret_post_uses_body() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(200).set_body_string(ACTIVE_BODY),
)
.await;
client(&format!("{}/introspect", server.uri()))
.auth_method(ClientAuthMethod::ClientSecretPost)
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect("introspection succeeds");
let requests = server.received_requests().await.unwrap();
let req = requests.last().unwrap();
assert!(
req.headers.get("authorization").is_none(),
"no Basic header"
);
let form = form_of(req);
assert_eq!(form.get("client_id").map(String::as_str), Some("rs-client"));
assert_eq!(
form.get("client_secret").map(String::as_str),
Some("rs-secret")
);
}
#[tokio::test]
async fn token_type_hint_is_sent() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(200).set_body_string(ACTIVE_BODY),
)
.await;
client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", Some("refresh_token"))
.await
.expect("introspection succeeds");
let requests = server.received_requests().await.unwrap();
let form = form_of(requests.last().unwrap());
assert_eq!(form.get("token").map(String::as_str), Some("the-token"));
assert_eq!(
form.get("token_type_hint").map(String::as_str),
Some("refresh_token")
);
}
#[tokio::test]
async fn wrong_token_type_hint_still_succeeds() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/introspect"))
.and(body_string_contains("token_type_hint=refresh_token"))
.respond_with(ResponseTemplate::new(200).set_body_string(ACTIVE_BODY))
.mount(&server)
.await;
let ir = client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", Some("refresh_token"))
.await
.expect("wrong hint must not fail the request");
assert!(ir.active);
}
#[tokio::test]
async fn error_response_maps_to_token_endpoint() {
let server = MockServer::start().await;
let body =
r#"{"error":"invalid_client","error_description":"Client authentication failed"}"#;
mount(&server, ResponseTemplate::new(401).set_body_string(body)).await;
let err = client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect_err("401 must fail");
match err {
IdentityError::TokenEndpoint {
error,
description,
status,
..
} => {
assert_eq!(error, "invalid_client");
assert_eq!(description.as_deref(), Some("Client authentication failed"));
assert_eq!(status, 401);
}
other => panic!("expected TokenEndpoint, got {other:?}"),
}
}
#[tokio::test]
async fn non_oauth_error_body_maps_to_http() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(500).set_body_string("upstream boom"),
)
.await;
let err = client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect_err("500 must fail");
assert!(matches!(err, IdentityError::Http(_)), "{err:?}");
}
#[tokio::test]
async fn missing_active_is_deserialization_error() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(200).set_body_string(r#"{"scope":"read"}"#),
)
.await;
let err = client(&format!("{}/introspect", server.uri()))
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect_err("missing active must fail");
assert!(matches!(err, IdentityError::Deserialization(_)), "{err:?}");
}
#[tokio::test]
async fn endpoint_resolved_from_discovery() {
let server = MockServer::start().await;
mount(
&server,
ResponseTemplate::new(200).set_body_string(ACTIVE_BODY),
)
.await;
let disco = format!(
r#"{{
"issuer": "{base}",
"authorization_endpoint": "{base}/authorize",
"token_endpoint": "{base}/token",
"introspection_endpoint": "{base}/introspect",
"jwks_uri": "{base}/jwks",
"response_types_supported": ["code"],
"subject_types_supported": ["public"],
"id_token_signing_alg_values_supported": ["RS256"]
}}"#,
base = server.uri()
);
let meta: ProviderMetadata = serde_json::from_str(&disco).expect("parse discovery");
let endpoint = meta
.introspection_endpoint
.expect("introspection_endpoint present");
let ir = client(&endpoint)
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect("introspection succeeds");
assert!(ir.active);
let requests = server.received_requests().await.unwrap();
assert_eq!(requests.last().unwrap().url.path(), "/introspect");
}
#[tokio::test]
async fn extra_params_applied_reserved_guarded() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/introspect"))
.and(header("content-type", "application/x-www-form-urlencoded"))
.respond_with(ResponseTemplate::new(200).set_body_string(ACTIVE_BODY))
.mount(&server)
.await;
client(&format!("{}/introspect", server.uri()))
.extra_param("resource", "urn:test:api")
.extra_param("token", "hacked")
.extra_param("client_id", "attacker")
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect("introspection succeeds");
let requests = server.received_requests().await.unwrap();
let form = form_of(requests.last().unwrap());
assert_eq!(
form.get("resource").map(String::as_str),
Some("urn:test:api")
);
assert_eq!(
form.get("token").map(String::as_str),
Some("the-token"),
"reserved token must not be overridden"
);
assert!(
!form.contains_key("client_id"),
"reserved client_id must not be injectable on the Basic path"
);
}
#[tokio::test]
async fn https_required_by_default() {
let err = IntrospectionClient::builder()
.client_id("rs-client")
.client_secret("rs-secret")
.introspection_endpoint("http://insecure.example/introspect")
.build()
.unwrap()
.introspect("the-token", None)
.await
.expect_err("http endpoint must be rejected");
assert!(matches!(err, IdentityError::Configuration(_)), "{err:?}");
}
#[test]
fn builder_requires_id_secret_and_endpoint() {
let missing_secret = IntrospectionClient::builder()
.client_id("rs-client")
.introspection_endpoint("https://issuer.example/introspect")
.build();
assert!(matches!(
missing_secret,
Err(IdentityError::Configuration(_))
));
let missing_id = IntrospectionClient::builder()
.client_secret("rs-secret")
.introspection_endpoint("https://issuer.example/introspect")
.build();
assert!(matches!(missing_id, Err(IdentityError::Configuration(_))));
let missing_endpoint = IntrospectionClient::builder()
.client_id("rs-client")
.client_secret("rs-secret")
.build();
assert!(matches!(
missing_endpoint,
Err(IdentityError::Configuration(_))
));
let ok = IntrospectionClient::builder()
.client_id("rs-client")
.client_secret("rs-secret")
.introspection_endpoint("https://issuer.example/introspect")
.build();
assert!(ok.is_ok());
}
#[test]
fn debug_redacts_client_secret() {
let builder = IntrospectionClient::builder()
.client_id("rs-client")
.client_secret("s3cr3t-value")
.introspection_endpoint("https://issuer.example/introspect");
assert!(!format!("{builder:?}").contains("s3cr3t-value"));
let client = builder.build().unwrap();
let dbg = format!("{client:?}");
assert!(!dbg.contains("s3cr3t-value"), "client leaked secret: {dbg}");
assert!(dbg.contains(REDACTED), "no redaction marker: {dbg}");
assert!(dbg.contains("rs-client"), "client_id should stay visible");
}
}