diff --git a/src/Microsoft.Identity.Web/AuthorityHelpers.cs b/src/Microsoft.Identity.Web/AuthorityHelpers.cs index 38021d7e4..afaa78061 100644 --- a/src/Microsoft.Identity.Web/AuthorityHelpers.cs +++ b/src/Microsoft.Identity.Web/AuthorityHelpers.cs @@ -2,6 +2,8 @@ // Licensed under the MIT License. using System; +using System.Collections.Generic; +using System.Web; using Microsoft.AspNetCore.Http; namespace Microsoft.Identity.Web @@ -51,5 +53,29 @@ internal static string EnsureAuthorityIsV2(string authority) preserveAuthority = true; return authority; } + + internal static string GetAuthorityWithoutQueryIfNeeded(MicrosoftIdentityOptions options) + { + if (!string.IsNullOrEmpty(options.Authority)) + { + int queryIndex = options.Authority.IndexOf('?', StringComparison.Ordinal); + if (queryIndex > -1) + { + options.ExtraQueryParameters ??= new Dictionary(); + var queryParams = HttpUtility.ParseQueryString(options.Authority[queryIndex..].TrimStart('?')); + for (int i = 0; i < queryParams.Count; i++) + { + var key = queryParams.GetKey(i); + var value = queryParams.Get(i); + if (key != null && value != null) + options.ExtraQueryParameters[key] = value; + } + + return options.Authority[..queryIndex]; + } + } + + return options.Authority ?? string.Empty; + } } } diff --git a/src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilderExtensions.cs b/src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilderExtensions.cs index 663cfbae2..2d7b92c00 100644 --- a/src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilderExtensions.cs +++ b/src/Microsoft.Identity.Web/WebApiExtensions/MicrosoftIdentityWebApiAuthenticationBuilderExtensions.cs @@ -176,6 +176,7 @@ private static void AddMicrosoftIdentityWebApiImplementation( // Process OIDC compliant tenants if (mergedOptions.Authority != null) { + mergedOptions.Authority = AuthorityHelpers.GetAuthorityWithoutQueryIfNeeded(mergedOptions); mergedOptions.Authority = AuthorityHelpers.BuildCiamAuthorityIfNeeded(mergedOptions.Authority, out bool preserveAuthority); mergedOptions.PreserveAuthority = preserveAuthority; options.Authority = mergedOptions.Authority; diff --git a/src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityWebAppAuthenticationBuilderExtensions.cs b/src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityWebAppAuthenticationBuilderExtensions.cs index a985be710..e53177ce4 100644 --- a/src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityWebAppAuthenticationBuilderExtensions.cs +++ b/src/Microsoft.Identity.Web/WebAppExtensions/MicrosoftIdentityWebAppAuthenticationBuilderExtensions.cs @@ -312,6 +312,7 @@ s.ServiceKey is null && if (mergedOptions.Authority != null) { + mergedOptions.Authority = AuthorityHelpers.GetAuthorityWithoutQueryIfNeeded(mergedOptions); mergedOptions.Authority = AuthorityHelpers.BuildCiamAuthorityIfNeeded(mergedOptions.Authority, out bool preserveAuthority); mergedOptions.PreserveAuthority = preserveAuthority; if (mergedOptions.ExtraQueryParameters != null) diff --git a/tests/Microsoft.Identity.Web.Test/AuthorityHelpersTests.cs b/tests/Microsoft.Identity.Web.Test/AuthorityHelpersTests.cs index 285c9c4f8..13a909cff 100644 --- a/tests/Microsoft.Identity.Web.Test/AuthorityHelpersTests.cs +++ b/tests/Microsoft.Identity.Web.Test/AuthorityHelpersTests.cs @@ -1,8 +1,8 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using System.Collections.Generic; using Microsoft.AspNetCore.Authentication.OpenIdConnect; -using Microsoft.Extensions.Options; using Microsoft.Identity.Web.Test.Common; using Xunit; @@ -118,5 +118,623 @@ public void EnsureAuthorityIsV2(string initialAuthority, string expectedAuthorit options.Authority = AuthorityHelpers.EnsureAuthorityIsV2(options.Authority); Assert.Equal(expectedAuthority, options.Authority); } + + [Theory] + [MemberData(nameof(GetAuthorityWithoutQueryIfNeededTheoryData))] + [MemberData(nameof(GetAuthorityWithoutQueryIfNeededExistingValuesTheoryData))] + [MemberData(nameof(GetAuthorityWithoutQueryIfNeededOverlappingExistingValuesTheoryData))] + public void GetAuthorityWithoutQueryIfNeeded(AuthorityHelpersTheoryData theoryData) + { + // arrange + MicrosoftIdentityOptions options = new() + { + Authority = theoryData.Authority, + ExtraQueryParameters = theoryData.ExtraQueryParameters + }; + + // act + var authorityWithoutQuery = AuthorityHelpers.GetAuthorityWithoutQueryIfNeeded(options); + + // assert + Assert.Equal(theoryData.ExpectedAuthority, authorityWithoutQuery); + Assert.NotNull(options.ExtraQueryParameters); + Assert.Equal(theoryData.ExpectedExtraQueryParameters.Count, options.ExtraQueryParameters.Count); + foreach (var key in theoryData.ExpectedExtraQueryParameters.Keys) + { + Assert.True(options.ExtraQueryParameters.ContainsKey(key)); + Assert.Equal(theoryData.ExpectedExtraQueryParameters[key], options.ExtraQueryParameters[key]); + } + } + + #region GetAuthorityWithoutQueryIfNeeded TheoryData + public static TheoryData GetAuthorityWithoutQueryIfNeededTheoryData() + { + var singleQuery = "?key1=value1"; + var multipleQueries = "?key1=value1&key2=value2"; + var emptyQuery = "?"; + var queryNoValue = "?key1"; + + var singleExpectedExtraQueryParams = new Dictionary + { + { "key1", "value1" } + }; + + var multipleExpectedExtraQueryParams = new Dictionary + { + { "key1", "value1" }, + { "key2", "value2" } + }; + + var emptyExpectedExtraQueryParams = new Dictionary(); + + var theoryData = new TheoryData + { + new("AuthorityCommonTenant_SingleQuery") + { + Authority = TestConstants.AuthorityCommonTenant + singleQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_MultipleQueries") + { + Authority = TestConstants.AuthorityCommonTenant + multipleQueries, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_EmptyQuery") + { + Authority = TestConstants.AuthorityCommonTenant + emptyQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_QueryNoValue") + { + Authority = TestConstants.AuthorityCommonTenant + queryNoValue, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_SingleQuery") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + singleQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_MultipleQueries") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_EmptyQuery") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_QueryNoValue") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_SingleQuery") + { + Authority = TestConstants.B2CAuthorityWithV2 + singleQuery, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_MultipleQueries") + { + Authority = TestConstants.B2CAuthorityWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_EmptyQuery") + { + Authority = TestConstants.B2CAuthorityWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_QueryNoValue") + { + Authority = TestConstants.B2CAuthorityWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_SingleQuery") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + singleQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_MultipleQueries") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_EmptyQuery") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_QueryNoValue") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthority_SingleQuery") + { + Authority = TestConstants.B2CAuthority + singleQuery, + ExpectedAuthority = TestConstants.B2CAuthority, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CAuthority_MultipleQueries") + { + Authority = TestConstants.B2CAuthority + multipleQueries, + ExpectedAuthority = TestConstants.B2CAuthority, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CAuthority_EmptyQuery") + { + Authority = TestConstants.B2CAuthority + emptyQuery, + ExpectedAuthority = TestConstants.B2CAuthority, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthority_QueryNoValue") + { + Authority = TestConstants.B2CAuthority + queryNoValue, + ExpectedAuthority = TestConstants.B2CAuthority, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_SingleQuery") + { + Authority = TestConstants.B2CCustomDomainAuthority + singleQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_MultipleQueries") + { + Authority = TestConstants.B2CCustomDomainAuthority + multipleQueries, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_EmptyQuery") + { + Authority = TestConstants.B2CCustomDomainAuthority + emptyQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_QueryNoValue") + { + Authority = TestConstants.B2CCustomDomainAuthority + queryNoValue, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + } + }; + + return theoryData; + } + + public static TheoryData GetAuthorityWithoutQueryIfNeededExistingValuesTheoryData() + { + var singleQuery = "?key1=value1"; + var multipleQueries = "?key1=value1&key2=value2"; + var emptyQuery = "?"; + var queryNoValue = "?key1"; + + var singleExpectedExtraQueryParams = new Dictionary + { + { "key1", "value1" }, + { "key3", "value3" }, + { "key4", "value4" } + }; + + var multipleExpectedExtraQueryParams = new Dictionary + { + { "key1", "value1" }, + { "key2", "value2" }, + { "key3", "value3" }, + { "key4", "value4" } + }; + + var emptyExpectedExtraQueryParams = new Dictionary + { + { "key3", "value3" }, + { "key4", "value4" } + }; + + var theoryData = new TheoryData + { + new("AuthorityCommonTenant_SingleQuery") + { + Authority = TestConstants.AuthorityCommonTenant + singleQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_MultipleQueries") + { + Authority = TestConstants.AuthorityCommonTenant + multipleQueries, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_EmptyQuery") + { + Authority = TestConstants.AuthorityCommonTenant + emptyQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_QueryNoValue") + { + Authority = TestConstants.AuthorityCommonTenant + queryNoValue, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_SingleQuery") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + singleQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_MultipleQueries") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_EmptyQuery") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_QueryNoValue") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_SingleQuery") + { + Authority = TestConstants.B2CAuthorityWithV2 + singleQuery, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_MultipleQueries") + { + Authority = TestConstants.B2CAuthorityWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_EmptyQuery") + { + Authority = TestConstants.B2CAuthorityWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_QueryNoValue") + { + Authority = TestConstants.B2CAuthorityWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_SingleQuery") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + singleQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_MultipleQueries") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_EmptyQuery") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_QueryNoValue") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthority_SingleQuery") + { + Authority = TestConstants.B2CAuthority + singleQuery, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CAuthority_MultipleQueries") + { + Authority = TestConstants.B2CAuthority + multipleQueries, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CAuthority_EmptyQuery") + { + Authority = TestConstants.B2CAuthority + emptyQuery, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthority_QueryNoValue") + { + Authority = TestConstants.B2CAuthority + queryNoValue, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_SingleQuery") + { + Authority = TestConstants.B2CCustomDomainAuthority + singleQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_MultipleQueries") + { + Authority = TestConstants.B2CCustomDomainAuthority + multipleQueries, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_EmptyQuery") + { + Authority = TestConstants.B2CCustomDomainAuthority + emptyQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_QueryNoValue") + { + Authority = TestConstants.B2CCustomDomainAuthority + queryNoValue, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key3", "value3" }, { "key4", "value4" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + } + }; + + return theoryData; + } + + public static TheoryData GetAuthorityWithoutQueryIfNeededOverlappingExistingValuesTheoryData() + { + var singleQuery = "?key1=value1"; + var multipleQueries = "?key1=value1&key2=value2"; + var emptyQuery = "?"; + var queryNoValue = "?key1"; + + var singleExpectedExtraQueryParams = new Dictionary + { + { "key1", "value1" }, + { "key2", "existingValue2" } + }; + + var multipleExpectedExtraQueryParams = new Dictionary + { + { "key1", "value1" }, + { "key2", "value2" } + }; + + var emptyExpectedExtraQueryParams = new Dictionary + { + { "key1", "existingValue1" }, + { "key2", "existingValue2" } + }; + + var theoryData = new TheoryData + { + new("AuthorityCommonTenant_SingleQuery") + { + Authority = TestConstants.AuthorityCommonTenant + singleQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_MultipleQueries") + { + Authority = TestConstants.AuthorityCommonTenant + multipleQueries, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_EmptyQuery") + { + Authority = TestConstants.AuthorityCommonTenant + emptyQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenant_QueryNoValue") + { + Authority = TestConstants.AuthorityCommonTenant + queryNoValue, + ExpectedAuthority = TestConstants.AuthorityCommonTenant, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_SingleQuery") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + singleQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_MultipleQueries") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_EmptyQuery") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("AuthorityCommonTenantWithV2_QueryNoValue") + { + Authority = TestConstants.AuthorityCommonTenantWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.AuthorityCommonTenantWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_SingleQuery") + { + Authority = TestConstants.B2CAuthorityWithV2 + singleQuery, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_MultipleQueries") + { + Authority = TestConstants.B2CAuthorityWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_EmptyQuery") + { + Authority = TestConstants.B2CAuthorityWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthorityWithV2_QueryNoValue") + { + Authority = TestConstants.B2CAuthorityWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.B2CAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_SingleQuery") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + singleQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_MultipleQueries") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + multipleQueries, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_EmptyQuery") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + emptyQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthorityWithV2_QueryNoValue") + { + Authority = TestConstants.B2CCustomDomainAuthorityWithV2 + queryNoValue, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthorityWithV2, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthority_SingleQuery") + { + Authority = TestConstants.B2CAuthority + singleQuery, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CAuthority_MultipleQueries") + { + Authority = TestConstants.B2CAuthority + multipleQueries, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CAuthority_EmptyQuery") + { + Authority = TestConstants.B2CAuthority + emptyQuery, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CAuthority_QueryNoValue") + { + Authority = TestConstants.B2CAuthority + queryNoValue, + ExpectedAuthority = TestConstants.B2CAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_SingleQuery") + { + Authority = TestConstants.B2CCustomDomainAuthority + singleQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = singleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_MultipleQueries") + { + Authority = TestConstants.B2CCustomDomainAuthority + multipleQueries, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = multipleExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_EmptyQuery") + { + Authority = TestConstants.B2CCustomDomainAuthority + emptyQuery, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + }, + new("B2CCustomDomainAuthority_QueryNoValue") + { + Authority = TestConstants.B2CCustomDomainAuthority + queryNoValue, + ExpectedAuthority = TestConstants.B2CCustomDomainAuthority, + ExtraQueryParameters = new Dictionary < string, string > { { "key1", "existingValue1" }, { "key2", "existingValue2" } }, + ExpectedExtraQueryParameters = emptyExpectedExtraQueryParams + } + }; + + return theoryData; + } + #endregion + } + + public class AuthorityHelpersTheoryData : TheoryDataBase + { + public AuthorityHelpersTheoryData(string testId) : base(testId) + { + } + + public string Authority { get; set; } = string.Empty; + + public string ExpectedAuthority { get; set; } = string.Empty; + + public IDictionary ExtraQueryParameters { get; set; } = new Dictionary(); + + public IDictionary ExpectedExtraQueryParameters { get; set; } = new Dictionary(); } } diff --git a/tests/Microsoft.Identity.Web.Test/TheoryDataBase.cs b/tests/Microsoft.Identity.Web.Test/TheoryDataBase.cs new file mode 100644 index 000000000..e06227271 --- /dev/null +++ b/tests/Microsoft.Identity.Web.Test/TheoryDataBase.cs @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using Xunit; + +namespace Microsoft.Identity.Web.Test +{ + public class TheoryDataBase : TheoryData + { + public TheoryDataBase(string testId) + { + TestId = testId; + } + + public string TestId { get; set; } = string.Empty; + + public override string ToString() + { + return TestId; + } + } +}