1717
1818use crate :: error:: { CortexError , Result } ;
1919
20+ fn has_token_value ( token : & str ) -> bool {
21+ !token. trim ( ) . is_empty ( )
22+ }
23+
2024/// Get authentication token with optional instance override.
2125///
2226/// Priority order:
@@ -44,15 +48,15 @@ use crate::error::{CortexError, Result};
4448pub fn get_auth_token ( instance_token : Option < & str > ) -> Result < String > {
4549 // Priority 1: Instance token (if provided and non-empty)
4650 if let Some ( token) = instance_token {
47- if !token . is_empty ( ) {
51+ if has_token_value ( token ) {
4852 tracing:: debug!( source = "instance" , "Using auth token from client instance" ) ;
4953 return Ok ( token. to_string ( ) ) ;
5054 }
5155 }
5256
5357 // Priority 2: CORTEX_AUTH_TOKEN environment variable
5458 if let Ok ( token) = std:: env:: var ( "CORTEX_AUTH_TOKEN" ) {
55- if !token . is_empty ( ) {
59+ if has_token_value ( & token ) {
5660 tracing:: debug!(
5761 source = "env_var" ,
5862 "Using auth token from CORTEX_AUTH_TOKEN"
@@ -63,7 +67,7 @@ pub fn get_auth_token(instance_token: Option<&str>) -> Result<String> {
6367
6468 // Priority 3: CORTEX_API_KEY environment variable (alias for GitHub Actions workflow)
6569 if let Ok ( token) = std:: env:: var ( "CORTEX_API_KEY" ) {
66- if !token . is_empty ( ) {
70+ if has_token_value ( & token ) {
6771 tracing:: debug!( source = "env_var" , "Using auth token from CORTEX_API_KEY" ) ;
6872 return Ok ( token) ;
6973 }
@@ -95,17 +99,17 @@ pub fn get_auth_token_optional(instance_token: Option<&str>) -> Option<String> {
9599/// Useful for fast availability checks in UI.
96100pub fn is_authenticated ( instance_token : Option < & str > ) -> bool {
97101 // Check instance token
98- if instance_token. map_or ( false , |t| !t . is_empty ( ) ) {
102+ if instance_token. is_some_and ( has_token_value ) {
99103 return true ;
100104 }
101105
102106 // Check CORTEX_AUTH_TOKEN env var
103- if std:: env:: var ( "CORTEX_AUTH_TOKEN" ) . map_or ( false , |t| !t . is_empty ( ) ) {
107+ if std:: env:: var ( "CORTEX_AUTH_TOKEN" ) . is_ok_and ( |t| has_token_value ( & t ) ) {
104108 return true ;
105109 }
106110
107111 // Check CORTEX_API_KEY env var (alias)
108- if std:: env:: var ( "CORTEX_API_KEY" ) . map_or ( false , |t| !t . is_empty ( ) ) {
112+ if std:: env:: var ( "CORTEX_API_KEY" ) . is_ok_and ( |t| has_token_value ( & t ) ) {
109113 return true ;
110114 }
111115
@@ -142,9 +146,18 @@ mod tests {
142146 fn test_is_authenticated_with_instance ( ) {
143147 assert ! ( is_authenticated( Some ( "token" ) ) ) ;
144148 assert ! ( !is_authenticated( Some ( "" ) ) ) ;
149+ assert ! ( !is_authenticated( Some ( " " ) ) ) ;
145150 assert ! ( !is_authenticated( None ) ) ;
146151 }
147152
153+ #[ test]
154+ fn test_has_token_value_rejects_whitespace_only_tokens ( ) {
155+ assert ! ( has_token_value( "token" ) ) ;
156+ assert ! ( !has_token_value( "" ) ) ;
157+ assert ! ( !has_token_value( " " ) ) ;
158+ assert ! ( !has_token_value( "\n \t " ) ) ;
159+ }
160+
148161 #[ test]
149162 fn test_auth_header_format ( ) {
150163 let header = auth_header ( Some ( "my-token" ) ) ;
0 commit comments