Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: trim tokens when parsing #492

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}