diff --git a/server/src/client_api.rs b/server/src/client_api.rs index 72896ded..1f88dd6a 100644 --- a/server/src/client_api.rs +++ b/server/src/client_api.rs @@ -428,8 +428,8 @@ mod tests { platform_name: None, platform_version: None, sdk_version: None, - yggdrasil_version: None - } + yggdrasil_version: None, + }, }; assert_eq!(found_metric.yes, expected.yes); diff --git a/server/src/frontend_api.rs b/server/src/frontend_api.rs index 63fe6ec2..6c9abb1e 100644 --- a/server/src/frontend_api.rs +++ b/server/src/frontend_api.rs @@ -487,7 +487,7 @@ pub fn evaluate_feature( payload: r.variant.payload, }, impression_data: r.impression_data, - impressionData: r.impression_data + impressionData: r.impression_data, }) .ok_or_else(|| EdgeError::FeatureNotFound(feature_name.clone())) } @@ -736,7 +736,7 @@ pub fn frontend_from_yggdrasil( payload: resolved.variant.payload.clone(), }, impression_data: resolved.impression_data, - impressionData: resolved.impression_data + impressionData: resolved.impression_data, }) .collect::>(); FrontendResult { toggles } @@ -1007,7 +1007,7 @@ mod tests { payload: None, }, impression_data: false, - impressionData: false + impressionData: false, }], }; @@ -1065,7 +1065,7 @@ mod tests { payload: None, }, impression_data: false, - impressionData: false + impressionData: false, }], }; @@ -1121,7 +1121,7 @@ mod tests { payload: None, }, impression_data: false, - impressionData: false + impressionData: false, }], }; diff --git a/server/src/http/feature_refresher.rs b/server/src/http/feature_refresher.rs index b2a1bffe..7dc45378 100644 --- a/server/src/http/feature_refresher.rs +++ b/server/src/http/feature_refresher.rs @@ -130,8 +130,8 @@ fn client_application_from_token(token: EdgeToken, refresh_interval: i64) -> Cli platform_name: None, platform_version: None, sdk_version: Some(format!("unleash-edge:{}", build::PKG_VERSION)), - yggdrasil_version: None - } + yggdrasil_version: None, + }, } } diff --git a/server/src/tokens.rs b/server/src/tokens.rs index a84a264e..4816471f 100644 --- a/server/src/tokens.rs +++ b/server/src/tokens.rs @@ -1,11 +1,12 @@ +use std::collections::HashSet; +use std::future::{ready, Ready}; +use std::str::FromStr; + use actix_web::dev::Payload; use actix_web::http::header::HeaderValue; use actix_web::web::Data; use actix_web::FromRequest; use actix_web::HttpRequest; -use std::collections::HashSet; -use std::future::{ready, Ready}; -use std::str::FromStr; use crate::cli::EdgeMode; use crate::cli::TokenHeader; @@ -167,6 +168,19 @@ impl FromStr for EdgeToken { type Err = EdgeError; fn from_str(s: &str) -> Result { + EdgeToken::from_trimmed_str(s.trim()) + } +} + +impl EdgeToken { + pub fn offline_token(s: &str) -> Self { + let mut token = EdgeToken::try_from(s.to_string()) + .ok() + .unwrap_or_else(|| EdgeToken::no_project_or_environment(s)); + token.status = TokenValidationStatus::Validated; + token + } + pub fn from_trimmed_str(s: &str) -> Result { if s.contains(':') && s.contains('.') { let token_parts: Vec = s.split(':').take(2).map(|s| s.to_string()).collect(); let token_projects = if let Some(projects) = token_parts.first() { @@ -202,23 +216,16 @@ impl FromStr for EdgeToken { } } } - -impl EdgeToken { - pub fn offline_token(s: &str) -> Self { - let mut token = EdgeToken::try_from(s.to_string()) - .ok() - .unwrap_or_else(|| EdgeToken::no_project_or_environment(s)); - token.status = TokenValidationStatus::Validated; - token - } -} #[cfg(test)] mod tests { + use std::str::FromStr; + + use ulid::Ulid; + use crate::{ tokens::simplify, types::{EdgeToken, TokenRefresh, TokenType}, }; - use ulid::Ulid; fn test_token(token: Option<&str>, env: Option<&str>, projects: Vec<&str>) -> EdgeToken { EdgeToken { @@ -428,4 +435,23 @@ mod tests { let is_covered = self_token.same_environment_and_broader_or_equal_project_access(&fe_token); assert!(!is_covered); } + + #[test] + fn leading_or_trailing_whitespace_gets_trimmed_away_when_constructing_token() { + let some_token = "*:development.somesecretstring"; + let some_token_with_leading_whitespace = " *:development.somesecretstring"; + let some_token_with_trailing_whitespace = "*:development.somesecretstring "; + let some_token_with_leading_and_trailing_whitespace = + " *:development.somesecretstring "; + let token = EdgeToken::from_str(some_token).expect("Could not parse token"); + let token1 = + EdgeToken::from_str(some_token_with_leading_whitespace).expect("Could not parse token"); + let token2 = EdgeToken::from_str(some_token_with_trailing_whitespace) + .expect("Could not parse token"); + let token3 = EdgeToken::from_str(some_token_with_leading_and_trailing_whitespace) + .expect("Could not parse token"); + assert_eq!(token, token1); + assert_eq!(token1, token2); + assert_eq!(token2, token3); + } }