Skip to content

Commit

Permalink
fix: trim tokens when parsing (#492)
Browse files Browse the repository at this point in the history
* fix: trim tokens when parsing
  • Loading branch information
chriswk authored Jul 19, 2024
1 parent 9795c7c commit 5281287
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 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
54 changes: 40 additions & 14 deletions server/src/tokens.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -167,6 +168,19 @@ impl FromStr for EdgeToken {
type Err = EdgeError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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<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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 5281287

Please sign in to comment.