From e8baa99af1726fe40b9a0ba302dfb9f3bde12803 Mon Sep 17 00:00:00 2001 From: Neha Bhargava <61847233+neha-bhargava@users.noreply.github.com> Date: Wed, 25 Sep 2024 13:07:21 -0700 Subject: [PATCH] Fix the config authority type to acquire the type from AuthorityInfo (#4943) * Fix the config authority type to acquire the type using the authority info * Add tests specific to bug --- .../AppConfig/AbstractApplicationBuilder.cs | 3 +- .../TestConstants.cs | 4 +-- .../ApiConfigTests/AuthorityTests.cs | 24 +++++++++++++++ .../InstanceTests/DstsAuthorityTests.cs | 30 +++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/client/Microsoft.Identity.Client/AppConfig/AbstractApplicationBuilder.cs b/src/client/Microsoft.Identity.Client/AppConfig/AbstractApplicationBuilder.cs index 11914ac144..c537327a81 100644 --- a/src/client/Microsoft.Identity.Client/AppConfig/AbstractApplicationBuilder.cs +++ b/src/client/Microsoft.Identity.Client/AppConfig/AbstractApplicationBuilder.cs @@ -501,7 +501,8 @@ public T WithAuthority( cloudInstanceUri, tenant, validateAuthority); - Config.Authority = new AadAuthority(authorityInfo); + + Config.Authority = authorityInfo.CreateAuthority(); return this as T; } diff --git a/tests/Microsoft.Identity.Test.Common/TestConstants.cs b/tests/Microsoft.Identity.Test.Common/TestConstants.cs index a02a197c4c..afc6e178bb 100644 --- a/tests/Microsoft.Identity.Test.Common/TestConstants.cs +++ b/tests/Microsoft.Identity.Test.Common/TestConstants.cs @@ -110,8 +110,8 @@ public static HashSet s_scope public const string ADFSAuthority2 = "https://someAdfs.com/adfs/"; public const string DstsAuthorityTenantless = "https://some.url.dsts.core.azure-test.net/dstsv2/"; - public const string DstsAuthorityTenanted = "https://some.url.dsts.core.azure-test.net/dstsv2/" + TenantId + "/"; - public const string DstsAuthorityCommon = "https://some.url.dsts.core.azure-test.net/dstsv2/" + Common + "/"; + public const string DstsAuthorityTenanted = DstsAuthorityTenantless + TenantId + "/"; + public const string DstsAuthorityCommon = DstsAuthorityTenantless + Common + "/"; public const string GenericAuthority = "https://demo.duendesoftware.com"; diff --git a/tests/Microsoft.Identity.Test.Unit/ApiConfigTests/AuthorityTests.cs b/tests/Microsoft.Identity.Test.Unit/ApiConfigTests/AuthorityTests.cs index 2b4113ae1c..522ed06362 100644 --- a/tests/Microsoft.Identity.Test.Unit/ApiConfigTests/AuthorityTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/ApiConfigTests/AuthorityTests.cs @@ -433,6 +433,30 @@ public void IsDefaultAuthorityTest() Assert.IsFalse(s_b2cAuthority.AuthorityInfo.IsDefaultAuthority); } + [DataTestMethod] + [DataRow(TestConstants.AuthorityCommonTenant, typeof(AadAuthority), "Aad")] + [DataRow(TestConstants.AuthorityCommonPpeAuthority, typeof(AadAuthority), "Aad")] + [DataRow(TestConstants.AuthorityConsumersTenant, typeof(AadAuthority), "Aad")] + [DataRow(TestConstants.AuthorityOrganizationsTenant, typeof(AadAuthority), "Aad")] + [DataRow(TestConstants.AuthorityGuidTenant, typeof(AadAuthority), "Aad")] + [DataRow(TestConstants.DstsAuthorityCommon, typeof(DstsAuthority), "Dsts")] + [DataRow(TestConstants.DstsAuthorityTenantless, typeof(DstsAuthority), "Dsts")] + [DataRow(TestConstants.ADFSAuthority, typeof(AdfsAuthority), "Adfs")] + [DataRow(TestConstants.CiamAuthorityMainFormat, typeof(CiamAuthority), "Ciam")] + public void VerifyConfigAuthorityType(string authorityHost, Type authorityTypeInstance, string authorityType) + { + string tenantId = "tenant"; + + var app = ConfidentialClientApplicationBuilder + .Create(TestConstants.ClientId) + .WithAuthority(authorityHost, tenantId) + .WithClientSecret("secret") + .BuildConcrete(); + + Assert.IsInstanceOfType(app.ServiceBundle.Config.Authority, authorityTypeInstance); + Assert.AreEqual(app.AuthorityInfo.AuthorityType.ToString(), authorityType); + } + private static void VerifyAuthority( Authority configAuthority, Authority requestAuthority, diff --git a/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/DstsAuthorityTests.cs b/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/DstsAuthorityTests.cs index d376594b96..5e9b895fb7 100644 --- a/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/DstsAuthorityTests.cs +++ b/tests/Microsoft.Identity.Test.Unit/CoreTests/InstanceTests/DstsAuthorityTests.cs @@ -84,6 +84,36 @@ public async Task DstsClientCredentialSuccessfulTestAsync(string authority) } } + [TestMethod] + public async Task DstsClientCredentialWithTenantIdFromAuthorityTestAsync() + { + using (var httpManager = new MockHttpManager()) + { + IConfidentialClientApplication app = ConfidentialClientApplicationBuilder + .Create(TestConstants.ClientId) + .WithHttpManager(httpManager) + .WithAuthority(TestConstants.DstsAuthorityTenantless, TestConstants.TenantId) + .WithClientSecret(TestConstants.ClientSecret) + .Build(); + + Assert.AreEqual(TestConstants.DstsAuthorityTenanted, app.Authority); + var confidentailClientApp = (ConfidentialClientApplication)app; + Assert.AreEqual(AuthorityType.Dsts, confidentailClientApp.AuthorityInfo.AuthorityType); + + httpManager.AddMockHandler(CreateTokenResponseHttpHandler(TestConstants.DstsAuthorityTenanted)); + + AuthenticationResult result = await app + .AcquireTokenForClient(TestConstants.s_scope) + .WithTenantIdFromAuthority(new Uri(TestConstants.DstsAuthorityTenanted)) + .ExecuteAsync(CancellationToken.None) + .ConfigureAwait(false); + + Assert.IsNotNull(result); + Assert.IsNotNull(result.AccessToken); + Assert.AreEqual(TokenSource.IdentityProvider, result.AuthenticationResultMetadata.TokenSource); + } + } + [TestMethod] public void DstsAuthorityFlags() {