@@ -7,9 +7,9 @@ use openidconnect::{
77 PkceCodeChallenge , PkceCodeVerifier , Scope , TokenResponse , UserInfoClaims ,
88} ;
99use shield:: {
10- ConfigurationError , CreateEmailAddress , CreateUser , Provider , ProviderError , Response , Session ,
11- SessionError , ShieldError , SignInCallbackRequest , SignInRequest , SignOutRequest , Subprovider ,
12- UpdateUser , User ,
10+ Authentication , ConfigurationError , CreateEmailAddress , CreateUser , Provider , ProviderError ,
11+ Response , Session , SessionError , ShieldError , SignInCallbackRequest , SignInRequest ,
12+ SignOutRequest , Subprovider , UpdateUser , User ,
1313} ;
1414use tracing:: debug;
1515
@@ -341,7 +341,7 @@ impl<U: User> Provider for OidcProvider<U> {
341341
342342 let connection = self
343343 . create_oidc_connection (
344- subprovider. id ,
344+ subprovider. id . clone ( ) ,
345345 user. id ( ) ,
346346 claims. subject ( ) . to_string ( ) ,
347347 token_response,
@@ -352,6 +352,8 @@ impl<U: User> Provider for OidcProvider<U> {
352352 }
353353 } ;
354354
355+ debug ! ( "signed in {:?} {:?}" , user. id( ) , connection) ;
356+
355357 session. renew ( ) . await ?;
356358
357359 {
@@ -360,13 +362,20 @@ impl<U: User> Provider for OidcProvider<U> {
360362 . lock ( )
361363 . map_err ( |err| SessionError :: Lock ( err. to_string ( ) ) ) ?;
362364
363- session_data. user_id = Some ( user. id ( ) ) ;
365+ session_data. csrf = None ;
366+ session_data. nonce = None ;
367+ session_data. verifier = None ;
368+
369+ session_data. authentication = Some ( Authentication {
370+ provider_id : self . id ( ) ,
371+ subprovider_id : Some ( subprovider. id ) ,
372+ user_id : user. id ( ) ,
373+ } ) ;
374+ session_data. oidc_connection_id = Some ( connection. id ) ;
364375 }
365376
366377 session. update ( ) . await ?;
367378
368- debug ! ( "signed in {:?} {:?}" , user. id( ) , connection) ;
369-
370379 // TODO: Should be configurable.
371380 Ok ( Response :: Redirect ( "/" . to_owned ( ) ) )
372381 }
@@ -381,25 +390,37 @@ impl<U: User> Provider for OidcProvider<U> {
381390 None => return Err ( ProviderError :: SubproviderMissing . into ( ) ) ,
382391 } ;
383392
384- // TODO: find access token
385- let token = AccessToken :: new ( "" . to_owned ( ) ) ;
386-
387- let client = subprovider. oidc_client ( ) . await ?;
393+ let connection_id = {
394+ let session_data = session. data ( ) ;
395+ let session_data = session_data
396+ . lock ( )
397+ . map_err ( |err| SessionError :: Lock ( err. to_string ( ) ) ) ?;
388398
389- let revocation_request = match client. revoke_token ( token. into ( ) ) {
390- Ok ( revocation_request) => Some ( revocation_request) ,
391- Err ( openidconnect:: ConfigurationError :: MissingUrl ( "revocation" ) ) => None ,
392- Err ( err) => return Err ( ConfigurationError :: Invalid ( err. to_string ( ) ) . into ( ) ) ,
399+ session_data. oidc_connection_id . clone ( )
393400 } ;
394401
395- if let Some ( revocation_request) = revocation_request {
396- revocation_request
397- . request_async ( async_http_client)
398- . await
399- . expect ( "TODO: revocation request error" ) ;
400- }
402+ if let Some ( connection_id) = connection_id {
403+ if let Some ( connection) = self . storage . oidc_connection_by_id ( & connection_id) . await ? {
404+ debug ! ( "revoking access token {:?}" , connection. access_token) ;
405+
406+ let token = AccessToken :: new ( connection. access_token ) ;
401407
402- session. purge ( ) . await ?;
408+ let client = subprovider. oidc_client ( ) . await ?;
409+
410+ let revocation_request = match client. revoke_token ( token. into ( ) ) {
411+ Ok ( revocation_request) => Some ( revocation_request) ,
412+ Err ( openidconnect:: ConfigurationError :: MissingUrl ( "revocation" ) ) => None ,
413+ Err ( err) => return Err ( ConfigurationError :: Invalid ( err. to_string ( ) ) . into ( ) ) ,
414+ } ;
415+
416+ if let Some ( revocation_request) = revocation_request {
417+ revocation_request
418+ . request_async ( async_http_client)
419+ . await
420+ . map_err ( |err| ShieldError :: Request ( err. to_string ( ) ) ) ?;
421+ }
422+ }
423+ }
403424
404425 // TODO: Should be configurable.
405426 Ok ( Response :: Redirect ( "/" . to_owned ( ) ) )
0 commit comments