Skip to content

Commit

Permalink
Fix the config authority type to acquire the type from AuthorityInfo (#…
Browse files Browse the repository at this point in the history
…4943)

* Fix the config authority type to acquire the type using the authority info

* Add tests specific to bug
  • Loading branch information
neha-bhargava authored Sep 25, 2024
1 parent 54e1a94 commit e8baa99
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ public T WithAuthority(
cloudInstanceUri,
tenant,
validateAuthority);
Config.Authority = new AadAuthority(authorityInfo);

Config.Authority = authorityInfo.CreateAuthority();

return this as T;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Microsoft.Identity.Test.Common/TestConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public static HashSet<string> 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";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

1 comment on commit e8baa99

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'AcquireTokenNoCache'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: e8baa99 Previous: 54e1a94 Ratio
Microsoft.Identity.Test.Performance.AcquireTokenNoCacheTests.AcquireTokenOnBehalfOf_TestAsync 525523.5 ns (± 129538.9830515943) 381126.43137254904 ns (± 13726.25909453104) 1.38

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.