use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;
use crate::core::{
CommonConfig, CommonConfigBuilder, EnvReader, Hooks, Result, RetryPolicy, Secret, TlsConfig,
VendorEnv,
};
pub const DEFAULT_URL: &str = "http://127.0.0.1:8000";
const VENDOR_ENV: VendorEnv<'static> = VendorEnv::NONE;
const VENDOR_TOKEN_VARS: &[&str] = &[];
#[derive(Debug, Clone)]
pub struct OidcConfig {
common: CommonConfig,
token: Option<Secret>,
}
impl OidcConfig {
pub fn builder() -> OidcConfigBuilder {
OidcConfigBuilder::default()
}
pub fn from_env() -> Result<Self> {
Self::builder().build_from_env()
}
pub fn common(&self) -> &CommonConfig {
&self.common
}
pub fn url(&self) -> &url::Url {
self.common.url()
}
pub fn token(&self) -> Option<&Secret> {
self.token.as_ref()
}
pub fn validate(&self) -> Result<()> {
Ok(())
}
}
#[derive(Debug, Clone, Default)]
pub struct OidcConfigBuilder {
common: CommonConfigBuilder,
token: Option<Secret>,
}
impl OidcConfigBuilder {
pub fn url(mut self, url: impl Into<String>) -> Self {
self.common = self.common.url(url);
self
}
pub fn token(mut self, token: impl Into<Secret>) -> Self {
self.token = Some(token.into());
self
}
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.common = self.common.connect_timeout(timeout);
self
}
pub fn request_timeout(mut self, timeout: Duration) -> Self {
self.common = self.common.request_timeout(timeout);
self
}
pub fn total_timeout(mut self, timeout: Duration) -> Self {
self.common = self.common.total_timeout(timeout);
self
}
pub fn retry(mut self, retry: RetryPolicy) -> Self {
self.common = self.common.retry(retry);
self
}
pub fn max_retries(mut self, max_retries: u32) -> Self {
self.common = self.common.max_retries(max_retries);
self
}
pub fn tls(mut self, tls: TlsConfig) -> Self {
self.common = self.common.tls(tls);
self
}
pub fn ca_cert_path(mut self, path: impl Into<PathBuf>) -> Self {
self.common = self.common.ca_cert_path(path);
self
}
pub fn insecure_skip_verify(mut self, skip: bool) -> Self {
self.common = self.common.insecure_skip_verify(skip);
self
}
pub fn proxy(mut self, proxy: impl Into<String>) -> Self {
self.common = self.common.proxy(proxy);
self
}
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.common = self.common.header(name, value);
self
}
pub fn user_agent_suffix(mut self, suffix: impl Into<String>) -> Self {
self.common = self.common.user_agent_suffix(suffix);
self
}
pub fn hooks(mut self, hooks: Arc<dyn Hooks>) -> Self {
self.common = self.common.hooks(hooks);
self
}
pub fn common(mut self, f: impl FnOnce(CommonConfigBuilder) -> CommonConfigBuilder) -> Self {
self.common = f(self.common);
self
}
pub fn build(self) -> Result<OidcConfig> {
let config = OidcConfig {
common: self.common.build(DEFAULT_URL)?,
token: self.token,
};
config.validate()?;
Ok(config)
}
pub fn build_from_env(self) -> Result<OidcConfig> {
self.build_with_env(&EnvReader::new())
}
pub fn build_with_env(mut self, env: &EnvReader) -> Result<OidcConfig> {
self.common = self.common.apply_env_with(env, &VENDOR_ENV)?;
if self.token.is_none() {
self.token = env.secret("TOKEN", VENDOR_TOKEN_VARS);
}
self.build()
}
}