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 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
25 changes: 7 additions & 18 deletions flipt-rust/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
use crate::error::ClientError;
use crate::evaluation::Evaluation;
use crate::{AuthScheme, Config};
use reqwest::header::HeaderMap;
use crate::{AuthenticationStrategy, Config, NoneAuthentication};
use std::time::Duration;

pub struct FliptClient {
pub evaluation: Evaluation,
}

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 = config.auth_strategy.authenticate();

let client = match reqwest::Client::builder()
.timeout(Duration::from_secs(config.timeout))
Expand All @@ -44,6 +33,6 @@ impl FliptClient {

impl Default for FliptClient {
fn default() -> Self {
Self::new(Config::default()).unwrap()
Self::new::<NoneAuthentication>(Config::default()).unwrap()
}
}
91 changes: 84 additions & 7 deletions flipt-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,112 @@ 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: T,
timeout: u64,
}

impl Default for Config {
impl Default for Config<NoneAuthentication> {
fn default() -> Self {
Self {
endpoint: Url::parse("http://localhost:8080").unwrap(),
auth_scheme: AuthScheme::None,
auth_strategy: NoneAuthentication::default(),
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,
timeout,
}
}
}

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

pub struct NoneAuthentication {}

impl NoneAuthentication {
pub fn new() -> Self {
Self {}
}
}

impl Default for NoneAuthentication {
fn default() -> Self {
Self::new()
}
}

impl AuthenticationStrategy for NoneAuthentication {
fn authenticate(self) -> HeaderMap {
HeaderMap::new()
}
}

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