Skip to content

Commit

Permalink
Bringing token exchange url commit into rel/v2 (#2774)
Browse files Browse the repository at this point in the history
* Allow token exchange URL configuration (#2767)

* initial commit adding configurable token exchange url

* Apply suggestions from code review

Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>

* use abstractions 5.2.0

* separate ctor overload

* add msi fic support to changelog

---------

Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>

* bring back abstractions 5.2.0!

---------

Co-authored-by: Jean-Marc Prieur <jmprieur@microsoft.com>
  • Loading branch information
kellyyangsong and jmprieur committed Apr 18, 2024
1 parent c99f631 commit 9331934
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
<MicrosoftGraphVersion>4.36.0</MicrosoftGraphVersion>
<MicrosoftGraphBetaVersion>4.57.0-preview</MicrosoftGraphBetaVersion>
<MicrosoftExtensionsHttpVersion>3.1.3</MicrosoftExtensionsHttpVersion>
<MicrosoftIdentityAbstractions>5.1.0</MicrosoftIdentityAbstractions>
<MicrosoftIdentityAbstractions>5.2.0</MicrosoftIdentityAbstractions>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
Expand Down
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Pending Next Release
=========
- Update to Microsoft.Identity.Abstractions 5.2.0

### New features
- Added support for Managed Identity Federated Identity Credential. See issue [2749](https://github.com/AzureAD/microsoft-identity-web/issues/2749) for details.

2.17.5
=========
- Updated to MSAL 4.59.1.
Expand All @@ -12,7 +19,7 @@
2.17.3
=========
- Updated to Microsoft.IdentityModel.* 7.5.0

2.17.2
=========

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using Azure.Identity;
using System.Threading;
using Microsoft.Identity.Abstractions;
using System.Threading.Tasks;
using Azure.Identity;
using Microsoft.Identity.Abstractions;

namespace Microsoft.Identity.Web
{
Expand All @@ -23,7 +19,7 @@ public async Task LoadIfNeededAsync(CredentialDescription credentialDescription,
ManagedIdentityClientAssertion? managedIdentityClientAssertion = credentialDescription.CachedValue as ManagedIdentityClientAssertion;
if (credentialDescription.CachedValue == null)
{
managedIdentityClientAssertion = new ManagedIdentityClientAssertion(credentialDescription.ManagedIdentityClientId);
managedIdentityClientAssertion = new ManagedIdentityClientAssertion(credentialDescription.ManagedIdentityClientId, credentialDescription.TokenExchangeUrl);
}
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

namespace Microsoft.Identity.Web.Certificateless
{
internal class CertificatelessConstants
{
// Managed Identity Federated Identity Credential
internal const string DefaultTokenExchangeUrl = "api://AzureADTokenExchange";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Microsoft.Identity.Web.Certificateless;

namespace Microsoft.Identity.Web
{
Expand All @@ -14,6 +15,7 @@ namespace Microsoft.Identity.Web
public class ManagedIdentityClientAssertion : ClientAssertionProviderBase
{
private readonly TokenCredential _credential;
private readonly string _tokenExchangeUrl;

/// <summary>
/// See https://aka.ms/ms-id-web/certificateless.
Expand All @@ -34,6 +36,17 @@ public ManagedIdentityClientAssertion(string? managedIdentityClientId)
ExcludeVisualStudioCodeCredential = true,
ExcludeVisualStudioCredential = true
});
_tokenExchangeUrl = CertificatelessConstants.DefaultTokenExchangeUrl;
}

/// <summary>
/// See https://aka.ms/ms-id-web/certificateless.
/// </summary>
/// <param name="managedIdentityClientId">Optional ClientId of the Managed Identity or Workload Identity</param>
/// <param name="tokenExchangeUrl">Optional token exchange resource url. Default value is "api://AzureADTokenExchange/.default".</param>
public ManagedIdentityClientAssertion(string? managedIdentityClientId, string? tokenExchangeUrl) : this (managedIdentityClientId)
{
_tokenExchangeUrl = tokenExchangeUrl ?? CertificatelessConstants.DefaultTokenExchangeUrl;
}

/// <summary>
Expand All @@ -44,7 +57,7 @@ public ManagedIdentityClientAssertion(string? managedIdentityClientId)
protected override async Task<ClientAssertion> GetClientAssertion(CancellationToken cancellationToken)
{
var result = await _credential.GetTokenAsync(
new TokenRequestContext(["api://AzureADTokenExchange/.default"], null),
new TokenRequestContext([_tokenExchangeUrl+"./default"], null),
cancellationToken).ConfigureAwait(false);
return new ClientAssertion(result.Token, result.ExpiresOn);
}
Expand Down

0 comments on commit 9331934

Please sign in to comment.