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

feat(rust): Add traits for generalizing the token strategy #67

Merged
merged 4 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 9 additions & 16 deletions flipt-rust/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::ClientError;
use crate::evaluation::Evaluation;
use crate::{AuthScheme, Config};
use crate::{AuthenticationStrategy, ClientTokenAuthentication, Config};
use reqwest::header::HeaderMap;
use std::time::Duration;

Expand All @@ -9,20 +9,13 @@ pub struct FliptClient {
}

impl FliptClient {
pub fn new(config: Config) -> Result<Self, ClientError> {
let mut header_map = HeaderMap::new();

match config.auth_scheme {
AuthScheme::BearerToken(bearer) => {
header_map.insert(
"Authorization",
format!("Bearer {}", bearer).parse().unwrap(),
);
}
AuthScheme::JWT(jwt) => {
header_map.insert("Authorization", format!("JWT {}", jwt).parse().unwrap());
}
AuthScheme::None => {}
pub fn new<T>(config: Config<T>) -> Result<Self, ClientError>
where
T: AuthenticationStrategy,
{
let header_map = match config.auth_strategy {
Some(a) => a.authenticate(),
None => HeaderMap::new(),
};

let client = match reqwest::Client::builder()
Expand All @@ -44,6 +37,6 @@ impl FliptClient {

impl Default for FliptClient {
fn default() -> Self {
Self::new(Config::default()).unwrap()
Self::new::<ClientTokenAuthentication>(Config::default()).unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want a None authentication strategy and make that the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markphelps Haha yeah that was what I was thinking. Should have posed that question here. I added it.

}
}
74 changes: 67 additions & 7 deletions flipt-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,95 @@ pub mod error;
pub mod evaluation;
pub mod util;

use reqwest::header::HeaderMap;
use url::Url;

#[derive(Debug, Clone)]
pub struct Config {
pub struct Config<T>
where
T: AuthenticationStrategy,
{
endpoint: Url,
auth_scheme: AuthScheme,
auth_strategy: Option<T>,
timeout: u64,
}

impl Default for Config {
impl<T> Default for Config<T>
where
T: AuthenticationStrategy,
{
fn default() -> Self {
Self {
endpoint: Url::parse("http://localhost:8080").unwrap(),
auth_scheme: AuthScheme::None,
auth_strategy: None,
timeout: 60,
}
}
}

impl Config {
pub fn new(endpoint: Url, auth_scheme: AuthScheme, timeout: u64) -> Self {
impl<T> Config<T>
where
T: AuthenticationStrategy,
{
pub fn new(endpoint: Url, auth_strategy: T, timeout: u64) -> Self {
Self {
endpoint,
auth_scheme,
auth_strategy: Some(auth_strategy),
timeout,
}
}
}

pub trait AuthenticationStrategy {
fn authenticate(self) -> HeaderMap;
}

pub struct JWTAuthentication {
jwt_token: String,
}

impl JWTAuthentication {
pub fn new(jwt_token: String) -> Self {
Self { jwt_token }
}
}

impl AuthenticationStrategy for JWTAuthentication {
fn authenticate(self) -> HeaderMap {
let mut header_map = HeaderMap::new();

header_map.insert(
"Authorization",
format!("JWT {}", self.jwt_token).parse().unwrap(),
);

header_map
}
}

pub struct ClientTokenAuthentication {
client_token: String,
}

impl ClientTokenAuthentication {
pub fn new(client_token: String) -> Self {
Self { client_token }
}
}

impl AuthenticationStrategy for ClientTokenAuthentication {
fn authenticate(self) -> HeaderMap {
let mut header_map = HeaderMap::new();

header_map.insert(
"Authorization",
format!("Bearer {}", self.client_token).parse().unwrap(),
);

header_map
}
}

#[derive(Debug, Clone)]
pub enum AuthScheme {
None,
Expand Down
4 changes: 2 additions & 2 deletions flipt-rust/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use flipt::evaluation::models::{
BatchEvaluationRequest, ErrorEvaluationReason, EvaluationReason, EvaluationRequest,
EvaluationResponseType,
};
use flipt::{AuthScheme, Config};
use flipt::{ClientTokenAuthentication, Config};
use std::{collections::HashMap, env};
use url::Url;

Expand All @@ -14,7 +14,7 @@ async fn tests() {

let flipt_client = FliptClient::new(Config::new(
Url::parse(&url).unwrap(),
AuthScheme::BearerToken(token),
ClientTokenAuthentication::new(token),
60,
))
.unwrap();
Expand Down
Loading