@@ -36,7 +36,7 @@ use secrecy::{ExposeSecret, SecretString};
3636use serde:: { Deserialize , Serialize } ;
3737use thiserror:: Error ;
3838use tokio:: sync:: RwLock ;
39- use tracing:: { debug, error , info, trace, warn} ;
39+ use tracing:: { debug, info, trace, warn} ;
4040
4141/// Default service name for keyring entries.
4242const KEYRING_SERVICE : & str = "Cortex-cli" ;
@@ -253,23 +253,14 @@ impl AuthManagerConfig {
253253}
254254
255255/// Cached authentication state.
256- #[ derive( Debug ) ]
256+ #[ derive( Debug , Default ) ]
257257struct CachedAuth {
258258 /// The cached token.
259259 token : Option < AuthToken > ,
260260 /// Time of last refresh.
261261 last_refresh : Option < Instant > ,
262262}
263263
264- impl Default for CachedAuth {
265- fn default ( ) -> Self {
266- Self {
267- token : None ,
268- last_refresh : None ,
269- }
270- }
271- }
272-
273264/// Centralized authentication manager providing a single source of truth
274265/// for authentication state across the Cortex CLI application.
275266///
@@ -328,11 +319,12 @@ impl AuthManager {
328319 // First check cache
329320 {
330321 let cache = self . cache . read ( ) . await ;
331- if let Some ( ref token) = cache. token {
332- if !token. is_expired ( ) && !token. expires_soon ( self . config . refresh_threshold_secs ) {
333- trace ! ( "AuthManager.auth: returning cached token" ) ;
334- return Ok ( token. clone ( ) ) ;
335- }
322+ if let Some ( ref token) = cache. token
323+ && !token. is_expired ( )
324+ && !token. expires_soon ( self . config . refresh_threshold_secs )
325+ {
326+ trace ! ( "AuthManager.auth: returning cached token" ) ;
327+ return Ok ( token. clone ( ) ) ;
336328 }
337329 }
338330
@@ -392,10 +384,10 @@ impl AuthManager {
392384
393385 // Clear auth file if exists
394386 let auth_file = self . config . cortex_home . join ( "auth.json" ) ;
395- if auth_file. exists ( ) {
396- if let Err ( e) = tokio:: fs:: remove_file ( & auth_file) . await {
397- warn ! ( "AuthManager.logout: failed to remove auth file: {}" , e ) ;
398- }
387+ if auth_file. exists ( )
388+ && let Err ( e) = tokio:: fs:: remove_file ( & auth_file) . await
389+ {
390+ warn ! ( "AuthManager.logout: failed to remove auth file: {}" , e ) ;
399391 }
400392
401393 Ok ( ( ) )
@@ -511,7 +503,7 @@ impl AuthManager {
511503 . and_then ( |t| t. refresh_token ( ) . map ( |s| s. to_string ( ) ) )
512504 } ;
513505
514- let refresh_token = refresh_token
506+ let _refresh_token = refresh_token
515507 . ok_or_else ( || AuthError :: RefreshFailed ( "No refresh token available" . to_string ( ) ) ) ?;
516508
517509 // Note: Actual token refresh would be implemented here
@@ -527,11 +519,11 @@ impl AuthManager {
527519 trace ! ( "AuthManager.load_from_storage: loading credentials" ) ;
528520
529521 // Check environment variables first if enabled
530- if self . config . enable_env_api_key {
531- if let Some ( token) = self . load_from_env ( ) {
532- debug ! ( "AuthManager: loaded credentials from environment" ) ;
533- return Ok ( Some ( token ) ) ;
534- }
522+ if self . config . enable_env_api_key
523+ && let Some ( token) = self . load_from_env ( )
524+ {
525+ debug ! ( "AuthManager: loaded credentials from environment" ) ;
526+ return Ok ( Some ( token ) ) ;
535527 }
536528
537529 // Try to load from keyring
@@ -626,23 +618,23 @@ impl AuthManager {
626618 serde_json:: from_str ( & content) . map_err ( |e| AuthError :: ParseError ( e. to_string ( ) ) ) ?;
627619
628620 // Check for API key
629- if let Some ( api_key) = auth_data. get ( "OPENAI_API_KEY" ) . and_then ( |v| v. as_str ( ) ) {
630- if !api_key. is_empty ( ) {
631- return Ok ( Some ( AuthToken :: new ( api_key , "Bearer" , "openai" ) ) ) ;
632- }
621+ if let Some ( api_key) = auth_data. get ( "OPENAI_API_KEY" ) . and_then ( |v| v. as_str ( ) )
622+ && !api_key. is_empty ( )
623+ {
624+ return Ok ( Some ( AuthToken :: new ( api_key , "Bearer" , "openai" ) ) ) ;
633625 }
634626
635627 // Check for tokens
636- if let Some ( tokens) = auth_data. get ( "tokens" ) {
637- if let Some ( access_token) = tokens. get ( "access_token" ) . and_then ( |v| v. as_str ( ) ) {
638- let mut token = AuthToken :: new ( access_token, "Bearer" , "chatgpt" ) ;
639-
640- if let Some ( refresh_token) = tokens. get ( "refresh_token" ) . and_then ( |v| v. as_str ( ) ) {
641- token = token. with_refresh_token ( refresh_token) ;
642- }
628+ if let Some ( tokens) = auth_data. get ( "tokens" )
629+ && let Some ( access_token) = tokens. get ( "access_token" ) . and_then ( |v| v. as_str ( ) )
630+ {
631+ let mut token = AuthToken :: new ( access_token, "Bearer" , "chatgpt" ) ;
643632
644- return Ok ( Some ( token) ) ;
633+ if let Some ( refresh_token) = tokens. get ( "refresh_token" ) . and_then ( |v| v. as_str ( ) ) {
634+ token = token. with_refresh_token ( refresh_token) ;
645635 }
636+
637+ return Ok ( Some ( token) ) ;
646638 }
647639
648640 Ok ( None )
0 commit comments