Skip to content

Commit

Permalink
feat: add token introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Mar 15, 2024
1 parent 63c6165 commit fc0c516
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
28 changes: 23 additions & 5 deletions baffao-core/src/oauth/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use anyhow::{Context, Error};

Check warning on line 1 in baffao-core/src/oauth/client.rs

View workflow job for this annotation

GitHub Actions / cargo fmt & test

Diff in /home/runner/work/baffao/baffao/baffao-core/src/oauth/client.rs
use oauth2::{
basic::{BasicClient, BasicTokenType},
reqwest::async_http_client,
AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EmptyExtraTokenFields,
PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, Scope, StandardTokenResponse, TokenUrl,
basic::{BasicClient, BasicTokenType}, reqwest::async_http_client, AccessToken, AuthType, AuthUrl, AuthorizationCode, ClientId, ClientSecret, CsrfToken, EmptyExtraTokenFields, IntrospectionUrl, PkceCodeChallenge, PkceCodeVerifier, RedirectUrl, Scope, StandardTokenIntrospectionResponse, StandardTokenResponse, TokenUrl
};
use reqwest::Url;

Expand Down Expand Up @@ -32,7 +29,7 @@ impl OAuthClient {
let token_endpoint =
TokenUrl::new(config.token_endpoint.clone()).context("Failed to parse token url")?;

let client = BasicClient::new(
let mut client = BasicClient::new(
ClientId::new(config.client_id.clone()),
Some(ClientSecret::new(config.client_secret.clone())),
auth_url,
Expand All @@ -41,6 +38,12 @@ impl OAuthClient {
.set_auth_type(AuthType::RequestBody)
.set_redirect_uri(redirect_uri);

if let Some(introspection_endpoint) = &config.introspection_endpoint {
let introspection_endpoint = IntrospectionUrl::new(introspection_endpoint.clone())
.context("Failed to parse introspection url")?;
client = client.set_introspection_uri(introspection_endpoint);
}

Ok(Self { config, client })
}

Expand Down Expand Up @@ -87,4 +90,19 @@ impl OAuthClient {

Ok(response.unwrap())
}

pub async fn introspect_token(

Check warning on line 94 in baffao-core/src/oauth/client.rs

View workflow job for this annotation

GitHub Actions / cargo fmt & test

Diff in /home/runner/work/baffao/baffao/baffao-core/src/oauth/client.rs
&self,
token: String,
) -> Result<StandardTokenIntrospectionResponse<EmptyExtraTokenFields, BasicTokenType>, Error> {
let response = self
.client
.introspect(&AccessToken::new(token))?
.request_async(async_http_client)
.await?;

// TODO: configure introspection request depending on auth method

Ok(response)
}
}
4 changes: 4 additions & 0 deletions baffao-core/src/oauth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ pub struct OAuthConfig {
pub token_endpoint: String,
pub redirect_uri: Option<String>,
pub default_scopes: Option<Vec<String>>,

pub introspection_endpoint: Option<String>,
pub introspection_endpoint_auth_methods_supported: Option<Vec<String>>,
pub introspection_endpoint_auth_signing_alg_values_supported: Option<Vec<String>>,
}

0 comments on commit fc0c516

Please sign in to comment.