Skip to content

fix: treat empty strings as providing no auth #10

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

Merged
merged 1 commit into from
Jan 16, 2025
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
16 changes: 10 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,23 @@ pub struct Config {
// Definition of the fhir server and credentials used for communicating data requests to the dic
#[clap(long, env)]
pub fhir_request_url: Url,
#[clap(long, env)]
pub fhir_request_credentials: Option<Auth>,
#[clap(long, env, default_value = "")]
pub fhir_request_credentials: Auth,
// Definition of the fhir server and credentials used for reading data from the dic
#[clap(long, env)]
pub fhir_input_url: Url,
#[clap(long, env)]
pub fhir_input_credentials: Option<Auth>,
#[clap(long, env, default_value = "")]
pub fhir_input_credentials: Auth,
// Definition of the fhir server and credentials used for adding data to the project data
#[clap(long, env)]
pub fhir_output_url: Url,
#[clap(long, env)]
pub fhir_output_credentials: Option<Auth>,
#[clap(long, env, default_value = "")]
pub fhir_output_credentials: Auth,
}

#[derive(Debug, Clone)]
pub enum Auth {
None,
Basic {
user: String,
pw: String,
Expand All @@ -46,6 +47,9 @@ impl FromStr for Auth {
type Err = &'static str;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.is_empty() {
return Ok(Self::None);
}
let (user, pw) = s.split_once(":").ok_or("Credentials should be in the form of '<user>:<pw>'")?;
Ok(Self::Basic { user: user.to_owned(), pw: pw.to_owned() })
}
Expand Down
12 changes: 5 additions & 7 deletions src/fhir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ use crate::{config::Auth, requests::DataRequestPayload, CONFIG};
#[derive(Clone, Debug)]
pub struct FhirServer {
url: Url,
auth: Option<Auth>,
auth: Auth,
client: Client,
}

trait ClientBuilderExt {
fn add_auth(self, auth: &Option<Auth>) -> Self;
fn add_auth(self, auth: &Auth) -> Self;
}

impl ClientBuilderExt for reqwest::RequestBuilder {
fn add_auth(self, auth: &Option<Auth>) -> Self {
let Some(auth) = auth else {
return self
};
fn add_auth(self, auth: &Auth) -> Self {
match auth {
Auth::Basic { user, pw } => self.basic_auth(user, Some(pw)),
Auth::None => self,
}
}
}

impl FhirServer {
pub fn new(url: Url, auth: Option<Auth>) -> Self {
pub fn new(url: Url, auth: Auth) -> Self {
Self { url, auth, client: Client::new() }
}

Expand Down
Loading