ids_daps_client/
config.rs

1use std::borrow::Cow;
2
3/// Configuration for the DAPS client.
4#[derive(Debug, derive_builder::Builder)]
5#[builder(setter(into), build_fn(validate = "Self::validate"))]
6#[allow(clippy::module_name_repetitions)]
7pub struct DapsConfig<'a> {
8    /// The URL for the request of a DAPS token.
9    pub(super) token_url: Cow<'a, str>,
10    /// The URL for the request of the certificates for validation.
11    pub(super) certs_url: Cow<'a, str>,
12    /// The local path to the private key file.
13    pub(super) private_key: Cow<'a, std::path::Path>,
14    /// The password for the private key file.
15    pub(super) private_key_password: Option<Cow<'a, str>>,
16    /// The scope for the DAPS token.
17    pub(super) scope: Cow<'a, str>,
18    /// The time-to-live for the certificates cache in seconds.
19    pub(super) certs_cache_ttl: u64,
20}
21
22impl DapsConfigBuilder<'_> {
23    /// Validates the configuration.
24    pub fn validate(&self) -> Result<(), String> {
25        if self.token_url.is_none() {
26            return Err("Token URL is empty".to_string());
27        } else if let Some(token_url) = self.token_url.clone() {
28            token_url
29                .parse::<url::Url>()
30                .map_err(|e| format!("Token URL is invalid: {e}"))?;
31        }
32        if self.certs_url.is_none() {
33            return Err("Certs URL is empty".to_string());
34        } else if let Some(certs_url) = self.certs_url.clone() {
35            certs_url
36                .parse::<url::Url>()
37                .map_err(|e| format!("Certs URL is invalid: {e}"))?;
38        }
39        if self.private_key.is_none() {
40            return Err("Private key path is empty".to_string());
41        }
42        if self.private_key_password.is_none() {
43            return Err("Private key password is empty".to_string());
44        }
45        if self.scope.is_none() {
46            return Err("Scope is empty".to_string());
47        }
48        Ok(())
49    }
50}