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: Handle both upper and lowercase of apitokentype. #174

Merged
merged 1 commit into from
May 2, 2023
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
9 changes: 5 additions & 4 deletions server/src/http/unleash_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::types::{

use prometheus::{register_int_gauge_vec, IntGaugeVec, Opts};
use reqwest::{header, Client};
use tracing::warn;
use unleash_types::client_metrics::ClientApplication;

use crate::error::FeatureError;
Expand Down Expand Up @@ -239,10 +240,10 @@ impl UnleashClient {
.map_err(|_| EdgeError::EdgeTokenError)?;
match result.status() {
StatusCode::OK => {
let token_response = result
.json::<EdgeTokens>()
.await
.map_err(|_| EdgeError::EdgeTokenParseError)?;
let token_response = result.json::<EdgeTokens>().await.map_err(|e| {
warn!("Failed to parse validation response with error: {e:?}");
EdgeError::EdgeTokenParseError
})?;
Ok(token_response
.tokens
.into_iter()
Expand Down
29 changes: 28 additions & 1 deletion server/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ pub type EdgeResult<T> = Result<T, EdgeError>;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, utoipa::ToSchema)]
#[serde(rename_all = "lowercase")]
pub enum TokenType {
#[serde(alias = "FRONTEND")]
Frontend,
#[serde(alias = "CLIENT")]
Client,
#[serde(alias = "ADMIN")]
Admin,
Invalid,
}
Expand Down Expand Up @@ -274,10 +277,12 @@ pub struct FeatureFilters {
mod tests {
use std::str::FromStr;

use crate::error::EdgeError::EdgeTokenParseError;
use crate::http::unleash_client::EdgeTokens;
use test_case::test_case;
use tracing::warn;

use crate::types::EdgeToken;
use crate::types::{EdgeResult, EdgeToken};

fn test_str(token: &str) -> EdgeToken {
EdgeToken::from_str(
Expand Down Expand Up @@ -360,4 +365,26 @@ mod tests {
assert!(token1.subsumes(&token2));
assert!(!token2.subsumes(&token1));
}

#[test]
fn token_type_should_be_case_insensitive() {
let json = r###"{ "tokens": [{
"token": "chriswk-test:development.notusedsecret",
"type": "CLIENT",
"projects": [
"chriswk-test"
]
},
{
"token": "demo-app:production.notusedsecret",
"type": "client",
"projects": [
"demo-app"
]
}] }"###;
let tokens: EdgeResult<EdgeTokens> =
serde_json::from_str(json).map_err(|_| EdgeTokenParseError);
assert!(tokens.is_ok());
assert_eq!(tokens.unwrap().tokens.len(), 2);
}
}