-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CDMS-181 provides the TokenCredential by using ConfidentialClientAppl…
…icationBuilder
- Loading branch information
1 parent
a22c435
commit c461f72
Showing
6 changed files
with
54 additions
and
14 deletions.
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
Btms.Azure/ConfidentialClientApplicationTokenCredential.cs
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 @@ | ||
using Azure.Core; | ||
using Btms.Azure.Extensions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Identity.Client; | ||
|
||
namespace Btms.Azure; | ||
|
||
/// <summary> | ||
/// Takes care of retriving a token via ConfidentialClientApplicationBuilder | ||
/// which allows us to inject our CDP_HTTPS_PROXY based http client. | ||
/// | ||
/// It's unclear why this isn't available out of the box! The | ||
/// </summary> | ||
/// <param name="token"></param> | ||
/// <param name="expiresOn"></param> | ||
public class ConfidentialClientApplicationTokenCredential : TokenCredential | ||
{ | ||
private readonly string[] _scopes = { "https://storage.azure.com/.default" }; | ||
|
||
private readonly IConfidentialClientApplication _app; | ||
public ConfidentialClientApplicationTokenCredential(IServiceProvider serviceProvider, IAzureConfig config) | ||
{ | ||
var httpClientFactory = serviceProvider.GetRequiredService<MsalHttpClientFactoryAdapter>(); | ||
|
||
_app = ConfidentialClientApplicationBuilder.Create(config.AzureClientId) | ||
.WithHttpClientFactory(httpClientFactory) | ||
.WithTenantId(config.AzureTenantId) | ||
.WithClientSecret(config.AzureClientSecret) | ||
.Build(); | ||
} | ||
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
var authResult = _app.AcquireTokenForClient(_scopes).ExecuteAsync(cancellationToken).Result; | ||
return ValueTask.FromResult(new AccessToken(authResult.AccessToken, authResult.ExpiresOn)); | ||
} | ||
|
||
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) | ||
{ | ||
var authResult = _app.AcquireTokenForClient(_scopes).ExecuteAsync(cancellationToken).Result; | ||
return new AccessToken(authResult.AccessToken, authResult.ExpiresOn); | ||
} | ||
} |
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
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