Skip to content

Commit

Permalink
Move from_trimmed_str into tokens module
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk committed Jul 19, 2024
1 parent c2da8f1 commit 72b0aef
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
4 changes: 2 additions & 2 deletions server/src/client_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions server/src/frontend_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down Expand Up @@ -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::<Vec<EvaluatedToggle>>();
FrontendResult { toggles }
Expand Down Expand Up @@ -1007,7 +1007,7 @@ mod tests {
payload: None,
},
impression_data: false,
impressionData: false
impressionData: false,
}],
};

Expand Down Expand Up @@ -1065,7 +1065,7 @@ mod tests {
payload: None,
},
impression_data: false,
impressionData: false
impressionData: false,
}],
};

Expand Down Expand Up @@ -1121,7 +1121,7 @@ mod tests {
payload: None,
},
impression_data: false,
impressionData: false
impressionData: false,
}],
};

Expand Down
4 changes: 2 additions & 2 deletions server/src/http/feature_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}

Expand Down
39 changes: 37 additions & 2 deletions server/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -180,6 +180,41 @@ impl EdgeToken {
token.status = TokenValidationStatus::Validated;
token
}
pub fn from_trimmed_str(s: &str) -> Result<Self, EdgeError> {
if s.contains(':') && s.contains('.') {
let token_parts: Vec<String> = 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<String> = 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 {
Expand Down
46 changes: 5 additions & 41 deletions server/src/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -215,42 +215,6 @@ impl EdgeToken {
})
.unwrap()
}

pub fn from_trimmed_str(s: &str) -> Result<Self, EdgeError> {
if s.contains(':') && s.contains('.') {
let token_parts: Vec<String> = 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<String> = 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)]
Expand Down

0 comments on commit 72b0aef

Please sign in to comment.