forked from Azure/azure-sdk-for-rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add AppServiceManagedIdentityCredential
- Loading branch information
Showing
3 changed files
with
84 additions
and
24 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
sdk/identity/src/token_credentials/app_service_managed_identity_credential.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::{ImdsManagedIdentityClient, TokenCredentialOptions}; | ||
use azure_core::auth::{AccessToken, TokenCredential}; | ||
use azure_core::headers::HeaderName; | ||
use azure_core::Url; | ||
|
||
const ENDPOINT_ENV: &str = "IDENTITY_ENDPOINT"; | ||
const API_VERSION: &str = "2019-08-01"; | ||
const SECRET_HEADER: HeaderName = HeaderName::from_static("x-identity-header"); | ||
const SECRET_ENV: &str = "IDENTITY_HEADER"; | ||
|
||
#[derive(Debug)] | ||
pub struct AppServiceManagedIdentityCredential { | ||
client: ImdsManagedIdentityClient, | ||
} | ||
|
||
impl AppServiceManagedIdentityCredential { | ||
pub fn create(options: TokenCredentialOptions) -> azure_core::Result<Self> { | ||
let env = options.env(); | ||
let endpoint = Url::parse(&env.var(ENDPOINT_ENV)?)?; | ||
Ok(Self { | ||
client: ImdsManagedIdentityClient::new( | ||
options, | ||
endpoint, | ||
API_VERSION, | ||
SECRET_HEADER, | ||
SECRET_ENV, | ||
), | ||
}) | ||
} | ||
} | ||
|
||
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))] | ||
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)] | ||
impl TokenCredential for AppServiceManagedIdentityCredential { | ||
async fn get_token(&self, scopes: &[&str]) -> azure_core::Result<AccessToken> { | ||
self.client.get_token(scopes).await | ||
} | ||
|
||
async fn clear_cache(&self) -> azure_core::Result<()> { | ||
self.client.clear_cache().await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters