Skip to content

Commit

Permalink
Update MSI implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sw-joelmut committed Aug 24, 2023
1 parent c9d4d08 commit 4a5fb7b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.AppConfig;

namespace Microsoft.Bot.Connector.Authentication
{
Expand All @@ -16,10 +17,9 @@ namespace Microsoft.Bot.Connector.Authentication
/// </summary>
public class ManagedIdentityAuthenticator : IAuthenticator
{
private readonly string _appId;
private readonly string _resource;
private readonly ILogger _logger;
private readonly IConfidentialClientApplication _clientApplication;
private readonly IManagedIdentityApplication _clientApplication;

/// <summary>
/// Initializes a new instance of the <see cref="ManagedIdentityAuthenticator"/> class.
Expand Down Expand Up @@ -54,7 +54,6 @@ public ManagedIdentityAuthenticator(string appId, string resource, HttpClient cu
throw new ArgumentNullException(nameof(resource));
}

_appId = appId;
_resource = resource;
_logger = logger ?? NullLogger.Instance;
_clientApplication = CreateClientApplication(appId, customHttpClient);
Expand All @@ -77,10 +76,8 @@ public async Task<AuthenticatorResult> GetTokenAsync(bool forceRefresh = false)

private async Task<AuthenticatorResult> AcquireTokenAsync(bool forceRefresh)
{
var scopes = new string[] { $"{_resource}/.default" };
var authResult = await _clientApplication
.AcquireTokenForClient(scopes)
.WithManagedIdentity(_appId)
.AcquireTokenForManagedIdentity(_resource)
.WithForceRefresh(forceRefresh)
.ExecuteAsync()
.ConfigureAwait(false);
Expand All @@ -100,10 +97,9 @@ private RetryParams HandleTokenProviderException(Exception e, int retryCount)
: RetryParams.DefaultBackOff(retryCount);
}

private IConfidentialClientApplication CreateClientApplication(string appId, HttpClient customHttpClient = null)
private IManagedIdentityApplication CreateClientApplication(string appId, HttpClient customHttpClient = null)
{
var clientBuilder = ConfidentialClientApplicationBuilder.Create(appId)
.WithExperimentalFeatures();
var clientBuilder = ManagedIdentityApplicationBuilder.Create(ManagedIdentityId.WithUserAssignedClientId(appId));

if (customHttpClient != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="2.1.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.50.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.55.0" />
<PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="5.6.0" />
<PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.24" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
Expand Down

0 comments on commit 4a5fb7b

Please sign in to comment.