@@ -344,9 +344,11 @@ impl AuthManager {
344344 ///
345345 /// Returns `None` if no token is cached or if the cached token is expired.
346346 pub fn auth_cached ( & self ) -> Option < AuthToken > {
347- // Use blocking read for sync access
348- let cache = self . cache . blocking_read ( ) ;
349- cache. token . as_ref ( ) . filter ( |t| !t. is_expired ( ) ) . cloned ( )
347+ // Try non-blocking read first, fall back to try_read
348+ match self . cache . try_read ( ) {
349+ Ok ( cache) => cache. token . as_ref ( ) . filter ( |t| !t. is_expired ( ) ) . cloned ( ) ,
350+ Err ( _) => None , // Lock contention, return None
351+ }
350352 }
351353
352354 /// Force reload authentication from storage.
@@ -392,10 +394,7 @@ impl AuthManager {
392394 let auth_file = self . config . cortex_home . join ( "auth.json" ) ;
393395 if auth_file. exists ( ) {
394396 if let Err ( e) = tokio:: fs:: remove_file ( & auth_file) . await {
395- warn ! (
396- "AuthManager.logout: failed to remove auth file: {}" ,
397- e
398- ) ;
397+ warn ! ( "AuthManager.logout: failed to remove auth file: {}" , e) ;
399398 }
400399 }
401400
@@ -438,7 +437,10 @@ impl AuthManager {
438437 refresh_token : Option < & str > ,
439438 expires_at : Option < u64 > ,
440439 ) -> AuthResult < ( ) > {
441- debug ! ( "AuthManager.save_oauth_tokens: saving tokens for {}" , provider) ;
440+ debug ! (
441+ "AuthManager.save_oauth_tokens: saving tokens for {}" ,
442+ provider
443+ ) ;
442444
443445 // Save access token
444446 let access_account = format ! ( "access_token_{}" , provider) ;
@@ -477,7 +479,8 @@ impl AuthManager {
477479 let cache = self . cache . read ( ) . await ;
478480 if let Some ( ref token) = cache. token {
479481 token. can_refresh ( )
480- && ( token. expires_soon ( self . config . refresh_threshold_secs ) || token. is_expired ( ) )
482+ && ( token. expires_soon ( self . config . refresh_threshold_secs )
483+ || token. is_expired ( ) )
481484 } else {
482485 false
483486 }
@@ -495,10 +498,7 @@ impl AuthManager {
495498 self . reload ( ) . await ?;
496499
497500 let cache = self . cache . read ( ) . await ;
498- cache
499- . token
500- . clone ( )
501- . ok_or ( AuthError :: NotAuthenticated )
501+ cache. token . clone ( ) . ok_or ( AuthError :: NotAuthenticated )
502502 }
503503
504504 /// Internal: Attempt to refresh the token.
@@ -511,9 +511,8 @@ impl AuthManager {
511511 . and_then ( |t| t. refresh_token ( ) . map ( |s| s. to_string ( ) ) )
512512 } ;
513513
514- let refresh_token = refresh_token. ok_or_else ( || {
515- AuthError :: RefreshFailed ( "No refresh token available" . to_string ( ) )
516- } ) ?;
514+ let refresh_token = refresh_token
515+ . ok_or_else ( || AuthError :: RefreshFailed ( "No refresh token available" . to_string ( ) ) ) ?;
517516
518517 // Note: Actual token refresh would be implemented here
519518 // This would involve calling the OAuth provider's token endpoint
@@ -587,11 +586,7 @@ impl AuthManager {
587586 }
588587 Ok ( None ) => continue ,
589588 Err ( e) => {
590- trace ! (
591- "AuthManager: keyring load error for {}: {}" ,
592- provider,
593- e
594- ) ;
589+ trace ! ( "AuthManager: keyring load error for {}: {}" , provider, e) ;
595590 continue ;
596591 }
597592 }
@@ -627,8 +622,8 @@ impl AuthManager {
627622 . map_err ( AuthError :: IoError ) ?;
628623
629624 // Parse the auth file
630- let auth_data: serde_json:: Value = serde_json :: from_str ( & content )
631- . map_err ( |e| AuthError :: ParseError ( e. to_string ( ) ) ) ?;
625+ let auth_data: serde_json:: Value =
626+ serde_json :: from_str ( & content ) . map_err ( |e| AuthError :: ParseError ( e. to_string ( ) ) ) ?;
632627
633628 // Check for API key
634629 if let Some ( api_key) = auth_data. get ( "OPENAI_API_KEY" ) . and_then ( |v| v. as_str ( ) ) {
@@ -757,27 +752,25 @@ mod tests {
757752 #[ test]
758753 fn test_auth_token_with_expiry ( ) {
759754 let future_time = current_timestamp ( ) + 3600 ;
760- let token = AuthToken :: new ( "test-token" , "Bearer" , "openai" )
761- . with_expiry ( future_time) ;
762-
755+ let token = AuthToken :: new ( "test-token" , "Bearer" , "openai" ) . with_expiry ( future_time) ;
756+
763757 assert ! ( !token. is_expired( ) ) ;
764758 assert ! ( !token. expires_soon( 300 ) ) ;
765759 }
766760
767761 #[ test]
768762 fn test_auth_token_expired ( ) {
769763 let past_time = current_timestamp ( ) - 100 ;
770- let token = AuthToken :: new ( "test-token" , "Bearer" , "openai" )
771- . with_expiry ( past_time) ;
772-
764+ let token = AuthToken :: new ( "test-token" , "Bearer" , "openai" ) . with_expiry ( past_time) ;
765+
773766 assert ! ( token. is_expired( ) ) ;
774767 }
775768
776769 #[ test]
777770 fn test_auth_token_with_refresh ( ) {
778- let token = AuthToken :: new ( "test-token" , "Bearer" , "openai" )
779- . with_refresh_token ( "refresh-token" ) ;
780-
771+ let token =
772+ AuthToken :: new ( "test-token" , "Bearer" , "openai" ) . with_refresh_token ( "refresh-token" ) ;
773+
781774 assert ! ( token. can_refresh( ) ) ;
782775 assert_eq ! ( token. refresh_token( ) , Some ( "refresh-token" ) ) ;
783776 }
@@ -793,7 +786,10 @@ mod tests {
793786 let config = AuthManagerConfig :: default ( ) ;
794787 assert ! ( config. auto_refresh) ;
795788 assert ! ( config. enable_env_api_key) ;
796- assert_eq ! ( config. refresh_threshold_secs, DEFAULT_REFRESH_THRESHOLD_SECS ) ;
789+ assert_eq ! (
790+ config. refresh_threshold_secs,
791+ DEFAULT_REFRESH_THRESHOLD_SECS
792+ ) ;
797793 }
798794
799795 #[ test]
0 commit comments