1
1
use anyhow:: { Context , Error } ;
2
2
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 , reqwest:: async_http_client, AccessToken as OAuthAccessToken , AuthType , AuthUrl , AuthorizationCode , ClientId , ClientSecret , CsrfToken , IntrospectionUrl , PkceCodeChallenge , PkceCodeVerifier , RedirectUrl , RefreshToken , Scope , TokenUrl
6
4
} ;
7
5
use reqwest:: Url ;
8
6
9
- use super :: { AccessToken , OAuthConfig } ;
7
+ use super :: { AccessToken , IntrospectionTokenResponse , OAuthConfig } ;
10
8
11
9
pub struct OAuthClient {
12
10
config : OAuthConfig ,
@@ -31,7 +29,7 @@ impl OAuthClient {
31
29
let token_endpoint =
32
30
TokenUrl :: new ( config. token_endpoint . clone ( ) ) . context ( "Failed to parse token url" ) ?;
33
31
34
- let client = BasicClient :: new (
32
+ let mut client = BasicClient :: new (
35
33
ClientId :: new ( config. client_id . clone ( ) ) ,
36
34
Some ( ClientSecret :: new ( config. client_secret . clone ( ) ) ) ,
37
35
auth_url,
@@ -40,6 +38,12 @@ impl OAuthClient {
40
38
. set_auth_type ( AuthType :: RequestBody )
41
39
. set_redirect_uri ( redirect_uri) ;
42
40
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
+
43
47
Ok ( Self { config, client } )
44
48
}
45
49
@@ -105,4 +109,18 @@ impl OAuthClient {
105
109
106
110
Ok ( response. unwrap ( ) )
107
111
}
112
+
113
+ pub async fn introspect_token (
114
+ & self ,
115
+ token : String ,
116
+ ) -> Result < IntrospectionTokenResponse , Error >
117
+ {
118
+ let response = self
119
+ . client
120
+ . introspect ( & OAuthAccessToken :: new ( token) ) ?
121
+ . request_async ( async_http_client)
122
+ . await ?;
123
+
124
+ Ok ( response)
125
+ }
108
126
}
0 commit comments