From 72b0aef896f2f3237e9d397b848dc7b348adc1cd Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Fri, 19 Jul 2024 14:16:09 +0200 Subject: [PATCH] Move from_trimmed_str into tokens module --- server/src/client_api.rs | 4 +-- server/src/frontend_api.rs | 10 +++--- server/src/http/feature_refresher.rs | 4 +-- server/src/tokens.rs | 39 +++++++++++++++++++++-- server/src/types.rs | 46 +++------------------------- 5 files changed, 51 insertions(+), 52 deletions(-) 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 be92dab1..4816471f 100644 --- a/server/src/tokens.rs +++ b/server/src/tokens.rs @@ -3,10 +3,10 @@ use std::future::{ready, Ready}; use std::str::FromStr; use actix_web::dev::Payload; -use actix_web::FromRequest; use actix_web::http::header::HeaderValue; -use actix_web::HttpRequest; use actix_web::web::Data; +use actix_web::FromRequest; +use actix_web::HttpRequest; use crate::cli::EdgeMode; use crate::cli::TokenHeader; @@ -180,6 +180,41 @@ impl EdgeToken { 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() { + if projects == "[]" { + vec![] + } else { + vec![projects.clone()] + } + } else { + return Err(EdgeError::TokenParseError(s.into())); + }; + if let Some(env_and_key) = token_parts.get(1) { + let e_a_k: Vec = env_and_key + .split('.') + .take(2) + .map(|s| s.to_string()) + .collect(); + if e_a_k.len() != 2 { + return Err(EdgeError::TokenParseError(s.into())); + } + Ok(EdgeToken { + environment: e_a_k.first().cloned(), + projects: token_projects, + token_type: None, + token: s.into(), + status: TokenValidationStatus::Unknown, + }) + } else { + Err(EdgeError::TokenParseError(s.into())) + } + } else { + Err(EdgeError::TokenParseError(s.into())) + } + } } #[cfg(test)] mod tests { diff --git a/server/src/types.rs b/server/src/types.rs index be74ab9e..e68c5fda 100644 --- a/server/src/types.rs +++ b/server/src/types.rs @@ -1,13 +1,13 @@ -use std::{ - collections::HashMap, - hash::{Hash, Hasher}, - str::FromStr, -}; use std::cmp::min; use std::fmt; use std::fmt::{Debug, Display, Formatter}; use std::net::IpAddr; use std::sync::Arc; +use std::{ + collections::HashMap, + hash::{Hash, Hasher}, + str::FromStr, +}; use actix_web::{http::header::EntityTag, web::Json}; use async_trait::async_trait; @@ -215,42 +215,6 @@ impl EdgeToken { }) .unwrap() } - - 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() { - if projects == "[]" { - vec![] - } else { - vec![projects.clone()] - } - } else { - return Err(EdgeError::TokenParseError(s.into())); - }; - if let Some(env_and_key) = token_parts.get(1) { - let e_a_k: Vec = env_and_key - .split('.') - .take(2) - .map(|s| s.to_string()) - .collect(); - if e_a_k.len() != 2 { - return Err(EdgeError::TokenParseError(s.into())); - } - Ok(EdgeToken { - environment: e_a_k.first().cloned(), - projects: token_projects, - token_type: None, - token: s.into(), - status: TokenValidationStatus::Unknown, - }) - } else { - Err(EdgeError::TokenParseError(s.into())) - } - } else { - Err(EdgeError::TokenParseError(s.into())) - } - } } #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, utoipa::ToSchema)]