Skip to content

Commit

Permalink
Dont modify the merged options when building the confidential client
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan Caruso committed Nov 27, 2024
1 parent 3936c29 commit 273581a
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
16 changes: 12 additions & 4 deletions src/Microsoft.Identity.Web.TokenAcquisition/MergedOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.Identity.Abstractions;
#if !NETSTANDARD2_0 && !NET462 && !NET472

#if !NETSTANDARD2_0 && !NETFRAMEWORK
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
#endif
using Microsoft.Identity.Client;

Expand Down Expand Up @@ -53,6 +55,12 @@ public ConfidentialClientApplicationOptions ConfidentialClientApplicationOptions
// This is for supporting for CIAM authorities including custom url domains, see https://github.com/AzureAD/microsoft-identity-web/issues/2690
internal bool PreserveAuthority { get; set; }

/// <summary>
/// Id Web will modify the instance so that it can be used by MSAL.
/// This modifies this property so that the original value is not changed.
/// </summary>
internal string? PreparedInstance { get; set; }

internal static void UpdateMergedOptionsFromMicrosoftIdentityOptions(MicrosoftIdentityOptions microsoftIdentityOptions, MergedOptions mergedOptions)
{

Expand Down Expand Up @@ -466,14 +474,14 @@ public void PrepareAuthorityInstanceForMsal()
if (IsB2C && Instance.EndsWith("/tfp/", StringComparison.OrdinalIgnoreCase))
{
#if !NETSTANDARD2_0 && !NET462 && !NET472
Instance = Instance.Replace("/tfp/", string.Empty, StringComparison.OrdinalIgnoreCase).TrimEnd('/') + "/";
PreparedInstance = Instance.Replace("/tfp/", string.Empty, StringComparison.OrdinalIgnoreCase).TrimEnd('/') + "/";
#else
Instance = Instance.Replace("/tfp/", string.Empty).TrimEnd('/') + "/";
PreparedInstance = Instance.Replace("/tfp/", string.Empty).TrimEnd('/') + "/";
#endif
}
else
{
Instance = Instance.TrimEnd('/') + "/";
PreparedInstance = Instance.TrimEnd('/') + "/";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#nullable enable
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.get -> string?
Microsoft.Identity.Web.MergedOptions.AppHomeTenantId.set -> void
Microsoft.Identity.Web.MergedOptions.MergedOptions(Microsoft.Identity.Web.MergedOptions! other) -> void
Microsoft.Identity.Web.MergedOptions.PreparedInstance.get -> string?
Microsoft.Identity.Web.MergedOptions.PreparedInstance.set -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForApp(Microsoft.Identity.Client.AcquireTokenForClientParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions) -> void
Microsoft.Identity.Web.TokenAcquisitionExtensionOptions.InvokeOnBeforeTokenAcquisitionForTestUser(Microsoft.Identity.Client.AcquireTokenByUsernameAndPasswordConfidentialParameterBuilder! builder, Microsoft.Identity.Abstractions.AcquireTokenOptions? acquireTokenOptions, System.Security.Claims.ClaimsPrincipal! user) -> void
readonly Microsoft.Identity.Web.TokenAcquisition.tokenAcquisitionExtensionOptionsMonitor -> Microsoft.Extensions.Options.IOptionsMonitor<Microsoft.Identity.Web.TokenAcquisitionExtensionOptions!>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public async Task<AcquireTokenResult> AddAccountToCacheFromAuthorizationCodeAsyn
if (mergedOptions.IsB2C)
{

var authority = $"{mergedOptions.Instance}{ClaimConstants.Tfp}/{mergedOptions.Domain}/{authCodeRedemptionParameters.UserFlow ?? mergedOptions.DefaultUserFlow}";
var authority = $"{mergedOptions.PreparedInstance}{ClaimConstants.Tfp}/{mergedOptions.Domain}/{authCodeRedemptionParameters.UserFlow ?? mergedOptions.DefaultUserFlow}";
builder.WithB2CAuthority(authority);
}

Expand Down Expand Up @@ -754,7 +754,6 @@ private bool IsInvalidClientCertificateOrSignedAssertionError(MsalServiceExcepti
/// </summary>
private async Task<IConfidentialClientApplication> BuildConfidentialClientApplicationAsync(MergedOptions mergedOptions)
{
string? currentUri = _tokenAcquisitionHost.GetCurrentRedirectUri(mergedOptions);
mergedOptions.PrepareAuthorityInstanceForMsal();

try
Expand All @@ -773,6 +772,8 @@ private async Task<IConfidentialClientApplication> BuildConfidentialClientApplic
builder.WithCacheOptions(CacheOptions.EnableSharedCacheOptions);
}

string? currentUri = _tokenAcquisitionHost.GetCurrentRedirectUri(mergedOptions);

// The redirect URI is not needed for OBO
if (!string.IsNullOrEmpty(currentUri))
{
Expand All @@ -788,12 +789,12 @@ private async Task<IConfidentialClientApplication> BuildConfidentialClientApplic
}
else if (mergedOptions.IsB2C)
{
authority = $"{mergedOptions.Instance}{ClaimConstants.Tfp}/{mergedOptions.Domain}/{mergedOptions.DefaultUserFlow}";
authority = $"{mergedOptions.PreparedInstance}{ClaimConstants.Tfp}/{mergedOptions.Domain}/{mergedOptions.DefaultUserFlow}";
builder.WithB2CAuthority(authority);
}
else
{
authority = $"{mergedOptions.Instance}{mergedOptions.TenantId}/";
authority = $"{mergedOptions.PreparedInstance}{mergedOptions.TenantId}/";
builder.WithAuthority(authority);
}

Expand Down

0 comments on commit 273581a

Please sign in to comment.