azure_identity/token_credentials/
options.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use crate::env::Env;
use azure_core::error::{ErrorKind, ResultExt};
use std::sync::Arc;
use url::Url;

const AZURE_AUTHORITY_HOST_ENV_KEY: &str = "AZURE_AUTHORITY_HOST";
const AZURE_PUBLIC_CLOUD: &str = "https://login.microsoftonline.com";

/// Provides options to configure how the Identity library makes authentication
/// requests to Azure Active Directory.
#[derive(Debug, Clone)]
pub struct TokenCredentialOptions {
    env: Env,
    http_client: Arc<dyn azure_core::HttpClient>,
    authority_host: String,
}

/// The default token credential options.
/// The authority host is taken from the `AZURE_AUTHORITY_HOST` environment variable if set and a valid URL.
/// If not, the default authority host is `https://login.microsoftonline.com` for the Azure public cloud.
impl Default for TokenCredentialOptions {
    fn default() -> Self {
        let env = Env::default();
        let authority_host = env
            .var(AZURE_AUTHORITY_HOST_ENV_KEY)
            .unwrap_or_else(|_| AZURE_PUBLIC_CLOUD.to_owned());
        Self {
            env: Env::default(),
            http_client: azure_core::new_http_client(),
            authority_host,
        }
    }
}

impl TokenCredentialOptions {
    #[cfg(test)]
    pub(crate) fn new(env: Env, http_client: Arc<dyn azure_core::HttpClient>) -> Self {
        Self {
            env,
            http_client,
            authority_host: AZURE_PUBLIC_CLOUD.to_owned(),
        }
    }
    /// Set the authority host for authentication requests.
    pub fn set_authority_host(&mut self, authority_host: String) {
        self.authority_host = authority_host;
    }

    /// The authority host to use for authentication requests.  The default is
    /// `https://login.microsoftonline.com`.
    pub fn authority_host(&self) -> azure_core::Result<Url> {
        Url::parse(&self.authority_host).with_context(ErrorKind::DataConversion, || {
            format!("invalid authority host URL {}", &self.authority_host)
        })
    }

    pub fn http_client(&self) -> Arc<dyn azure_core::HttpClient> {
        self.http_client.clone()
    }

    pub(crate) fn env(&self) -> &Env {
        &self.env
    }
}

impl From<Arc<dyn azure_core::HttpClient>> for TokenCredentialOptions {
    fn from(http_client: Arc<dyn azure_core::HttpClient>) -> Self {
        Self {
            http_client,
            ..Default::default()
        }
    }
}