|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use crate::{credentials::cache::TokenCache, federated_credentials_flow, TokenCredentialOptions}; |
| 5 | +use azure_core::{ |
| 6 | + credentials::{AccessToken, TokenCredential}, |
| 7 | + error::{ErrorKind, ResultExt}, |
| 8 | + HttpClient, Url, |
| 9 | +}; |
| 10 | +use std::{fmt::Debug, str, sync::Arc, time::Duration}; |
| 11 | +use time::OffsetDateTime; |
| 12 | + |
| 13 | +const AZURE_TENANT_ID_ENV_KEY: &str = "AZURE_TENANT_ID"; |
| 14 | +const AZURE_CLIENT_ID_ENV_KEY: &str = "AZURE_CLIENT_ID"; |
| 15 | + |
| 16 | +/// Enables authentication of a Microsoft Entra service principal using a signed client assertion. |
| 17 | +#[derive(Debug)] |
| 18 | +pub struct ClientAssertionCredential<C> { |
| 19 | + http_client: Arc<dyn HttpClient>, |
| 20 | + authority_host: Url, |
| 21 | + tenant_id: String, |
| 22 | + client_id: String, |
| 23 | + assertion: C, |
| 24 | + cache: TokenCache, |
| 25 | +} |
| 26 | + |
| 27 | +#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] |
| 28 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] |
| 29 | +/// Represents an entity capable of supplying a client assertion. |
| 30 | +pub trait ClientAssertion: Send + Sync + Debug { |
| 31 | + /// Supply the client assertion secret. |
| 32 | + async fn secret(&self) -> azure_core::Result<String>; |
| 33 | +} |
| 34 | + |
| 35 | +impl<C: ClientAssertion> ClientAssertionCredential<C> { |
| 36 | + /// Create a new `ClientAssertionCredential`. |
| 37 | + pub fn new( |
| 38 | + http_client: Arc<dyn HttpClient>, |
| 39 | + authority_host: Url, |
| 40 | + tenant_id: String, |
| 41 | + client_id: String, |
| 42 | + assertion: C, |
| 43 | + ) -> azure_core::Result<Arc<Self>> { |
| 44 | + Ok(Arc::new(Self::new_exclusive( |
| 45 | + http_client, |
| 46 | + authority_host, |
| 47 | + tenant_id, |
| 48 | + client_id, |
| 49 | + assertion, |
| 50 | + )?)) |
| 51 | + } |
| 52 | + |
| 53 | + /// Create a new `ClientAssertionCredential` without wrapping it in an |
| 54 | + /// `Arc`. Intended for use by other credentials in the crate that will |
| 55 | + /// themselves be protected by an `Arc`. |
| 56 | + pub(crate) fn new_exclusive( |
| 57 | + http_client: Arc<dyn HttpClient>, |
| 58 | + authority_host: Url, |
| 59 | + tenant_id: String, |
| 60 | + client_id: String, |
| 61 | + assertion: C, |
| 62 | + ) -> azure_core::Result<Self> { |
| 63 | + Ok(Self { |
| 64 | + http_client, |
| 65 | + authority_host, |
| 66 | + tenant_id, |
| 67 | + client_id, |
| 68 | + assertion, |
| 69 | + cache: TokenCache::new(), |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + /// Create a new `ClientAssertionCredential` from environment variables. |
| 74 | + /// |
| 75 | + /// # Variables |
| 76 | + /// |
| 77 | + /// * `AZURE_TENANT_ID` |
| 78 | + /// * `AZURE_CLIENT_ID` |
| 79 | + pub fn from_env( |
| 80 | + options: impl Into<TokenCredentialOptions>, |
| 81 | + assertion: C, |
| 82 | + ) -> azure_core::Result<Arc<Self>> { |
| 83 | + Ok(Arc::new(Self::from_env_exclusive(options, assertion)?)) |
| 84 | + } |
| 85 | + |
| 86 | + /// Create a new `ClientAssertionCredential` from environment variables, |
| 87 | + /// without wrapping it in an `Arc`. Intended for use by other credentials |
| 88 | + /// in the crate that will themselves be protected by an `Arc`. |
| 89 | + /// |
| 90 | + /// # Variables |
| 91 | + /// |
| 92 | + /// * `AZURE_TENANT_ID` |
| 93 | + /// * `AZURE_CLIENT_ID` |
| 94 | + pub(crate) fn from_env_exclusive( |
| 95 | + options: impl Into<TokenCredentialOptions>, |
| 96 | + assertion: C, |
| 97 | + ) -> azure_core::Result<Self> { |
| 98 | + let options = options.into(); |
| 99 | + let http_client = options.http_client(); |
| 100 | + let authority_host = options.authority_host()?; |
| 101 | + let env = options.env(); |
| 102 | + let tenant_id = |
| 103 | + env.var(AZURE_TENANT_ID_ENV_KEY) |
| 104 | + .with_context(ErrorKind::Credential, || { |
| 105 | + format!( |
| 106 | + "working identity credential requires {} environment variable", |
| 107 | + AZURE_TENANT_ID_ENV_KEY |
| 108 | + ) |
| 109 | + })?; |
| 110 | + let client_id = |
| 111 | + env.var(AZURE_CLIENT_ID_ENV_KEY) |
| 112 | + .with_context(ErrorKind::Credential, || { |
| 113 | + format!( |
| 114 | + "working identity credential requires {} environment variable", |
| 115 | + AZURE_CLIENT_ID_ENV_KEY |
| 116 | + ) |
| 117 | + })?; |
| 118 | + |
| 119 | + ClientAssertionCredential::new_exclusive( |
| 120 | + http_client, |
| 121 | + authority_host, |
| 122 | + tenant_id, |
| 123 | + client_id, |
| 124 | + assertion, |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + async fn get_token(&self, scopes: &[&str]) -> azure_core::Result<AccessToken> { |
| 129 | + let token = self.assertion.secret().await?; |
| 130 | + let res: AccessToken = federated_credentials_flow::authorize( |
| 131 | + self.http_client.clone(), |
| 132 | + &self.client_id, |
| 133 | + &token, |
| 134 | + scopes, |
| 135 | + &self.tenant_id, |
| 136 | + &self.authority_host, |
| 137 | + ) |
| 138 | + .await |
| 139 | + .map(|r| { |
| 140 | + AccessToken::new( |
| 141 | + r.access_token().clone(), |
| 142 | + OffsetDateTime::now_utc() + Duration::from_secs(r.expires_in), |
| 143 | + ) |
| 144 | + }) |
| 145 | + .context(ErrorKind::Credential, "request token error")?; |
| 146 | + Ok(res) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] |
| 151 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] |
| 152 | +impl<C: ClientAssertion> TokenCredential for ClientAssertionCredential<C> { |
| 153 | + async fn get_token(&self, scopes: &[&str]) -> azure_core::Result<AccessToken> { |
| 154 | + self.cache.get_token(scopes, self.get_token(scopes)).await |
| 155 | + } |
| 156 | + |
| 157 | + async fn clear_cache(&self) -> azure_core::Result<()> { |
| 158 | + self.cache.clear().await |
| 159 | + } |
| 160 | +} |
0 commit comments