Skip to content

Commit 85dfeb9

Browse files
feat: add token introspection
1 parent a1a61fe commit 85dfeb9

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

baffao-proxy/config/example.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ client_id = "client_id"
66
client_secret = "client_secret"
77
authorization_redirect_uri = "http://127.0.0.1:3000/oauth/callback"
88
authorization_endpoint = "http://127.0.0.1:4444/oauth2/auth"
9-
token_endpoint = "http://127.0.0.1/oauth2/token"
9+
token_endpoint = "http://127.0.0.1:3000/oauth2/token"
10+
introspection_endpoint = "http://127.0.0.1:3000/oauth2/introspect"
1011
redirect_uri = "http://127.0.0.1:3000/"

baffao/src/oauth/client.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use anyhow::{Context, Error};
22
use oauth2::{
3-
basic::BasicClient, reqwest::async_http_client, AuthType, AuthUrl, AuthorizationCode, ClientId,
4-
ClientSecret, CsrfToken, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, RefreshToken, Scope,
5-
TokenUrl,
3+
basic::{BasicClient, BasicTokenType}, reqwest::async_http_client, AccessToken as OAuthAccessToken, AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EmptyExtraTokenFields, IntrospectionUrl, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, RefreshToken, Scope, StandardTokenIntrospectionResponse, TokenUrl
64
};
75
use reqwest::Url;
86

@@ -31,7 +29,7 @@ impl OAuthClient {
3129
let token_endpoint =
3230
TokenUrl::new(config.token_endpoint.clone()).context("Failed to parse token url")?;
3331

34-
let client = BasicClient::new(
32+
let mut client = BasicClient::new(
3533
ClientId::new(config.client_id.clone()),
3634
Some(ClientSecret::new(config.client_secret.clone())),
3735
auth_url,
@@ -40,6 +38,12 @@ impl OAuthClient {
4038
.set_auth_type(AuthType::RequestBody)
4139
.set_redirect_uri(redirect_uri);
4240

41+
if let Some(introspection_endpoint) = &config.introspection_endpoint {
42+
let introspection_endpoint = IntrospectionUrl::new(introspection_endpoint.clone())
43+
.context("Failed to parse introspection url")?;
44+
client = client.set_introspection_uri(introspection_endpoint);
45+
}
46+
4347
Ok(Self { config, client })
4448
}
4549

@@ -105,4 +109,20 @@ impl OAuthClient {
105109

106110
Ok(response.unwrap())
107111
}
112+
113+
pub async fn introspect_token(
114+
&self,
115+
token: String,
116+
) -> Result<StandardTokenIntrospectionResponse<EmptyExtraTokenFields, BasicTokenType>, Error>
117+
{
118+
let response = self
119+
.client
120+
.introspect(&OAuthAccessToken::new(token))?
121+
.request_async(async_http_client)
122+
.await?;
123+
124+
// TODO: configure introspection request depending on auth method
125+
126+
Ok(response)
127+
}
108128
}

baffao/src/oauth/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub struct OAuthConfig {
2424
pub token_endpoint: String,
2525
pub redirect_uri: Option<String>,
2626
pub default_scopes: Option<Vec<String>>,
27+
28+
pub introspection_endpoint: Option<String>,
29+
pub introspection_endpoint_auth_methods_supported: Option<Vec<String>>,
30+
pub introspection_endpoint_auth_signing_alg_values_supported: Option<Vec<String>>,
2731
}
2832

2933
pub type AccessToken = StandardTokenResponse<EmptyExtraTokenFields, BasicTokenType>;

0 commit comments

Comments
 (0)