Skip to content

Commit c52e764

Browse files
authored
[Identity] Throw CredentialUnavailableException from credentials not supporting ADFS (#14763)
* [Identity] Throw CredentialUnavailableException from credentials not supporting ADFS * moving tenantId check to after it's read from settings * fix check to use local variable
1 parent f40bbbe commit c52e764

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Fixes and improvements
1111
- Fixed issue with non GUID Client Ids (Issue [#14585](https://github.com/Azure/azure-sdk-for-net/issues/14585))
12+
- Update `VisualStudioCredential` and `VisualStudioCodeCredential` to throw `CredentialUnavailableException` for ADFS tenant (Issue [#14639](https://github.com/Azure/azure-sdk-for-net/issues/14639))
1213

1314

1415
## 1.2.2 (2020-08-20)

sdk/identity/Azure.Identity/src/Constants.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ internal class Constants
1111
{
1212
public const string OrganizationsTenantId = "organizations";
1313

14+
public const string AdfsTenantId = "adfs";
15+
1416
// TODO: Currently this is piggybacking off the Azure CLI client ID, but needs to be switched once the Developer Sign On application is available
1517
public const string DeveloperSignOnClientId = "04b07795-8ddb-461a-bbee-02f9e1bf7b46";
1618

sdk/identity/Azure.Identity/src/VisualStudioCodeCredential.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ private async ValueTask<AccessToken> GetTokenImplAsync(TokenRequestContext reque
6363
{
6464
GetUserSettings(out var tenant, out var environmentName);
6565

66+
if (string.Equals(tenant, Constants.AdfsTenantId, StringComparison.Ordinal))
67+
{
68+
throw new CredentialUnavailableException("VisualStudioCodeCredential authentication unavailable. ADFS tenant / authorities are not supported.");
69+
}
70+
6671
var cloudInstance = GetAzureCloudInstance(environmentName);
6772
var storedCredentials = _vscAdapter.GetCredentials(CredentialsSection, environmentName);
6873

sdk/identity/Azure.Identity/src/VisualStudioCredential.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ private async ValueTask<AccessToken> GetTokenImplAsync(TokenRequestContext reque
6464

6565
try
6666
{
67+
if (string.Equals(_tenantId, Constants.AdfsTenantId, StringComparison.Ordinal))
68+
{
69+
throw new CredentialUnavailableException("VisualStudioCredential authentication unavailable. ADFS tenant/authorities are not supported.");
70+
}
71+
6772
var tokenProviderPath = GetTokenProviderPath();
6873
var tokenProviders = GetTokenProviders(tokenProviderPath);
6974

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Threading;
5+
using Azure.Core;
6+
using Azure.Core.TestFramework;
7+
using NUnit.Framework;
8+
9+
namespace Azure.Identity.Tests
10+
{
11+
public class VisualStudioCodeCredentialTests : ClientTestBase
12+
{
13+
public VisualStudioCodeCredentialTests(bool isAsync) : base(isAsync)
14+
{
15+
16+
}
17+
18+
[Test]
19+
public void AdfsTenantThrowsCredentialUnavailable()
20+
{
21+
var options = new VisualStudioCodeCredentialOptions { TenantId = "adfs", Transport = new MockTransport() };
22+
23+
VisualStudioCodeCredential credential = InstrumentClient(new VisualStudioCodeCredential(options));
24+
25+
Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/.default" }), CancellationToken.None));
26+
}
27+
}
28+
}

sdk/identity/Azure.Identity/tests/VisualStudioCredentialTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,15 @@ public void AuthenticateWithVsCredential_CredentialUnavailableExceptionPassThrou
178178
var credential = InstrumentClient(new VisualStudioCredential(default, default, fileSystem, testProcessFactory));
179179
Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(new[]{"https://vault.azure.net/"}), CancellationToken.None));
180180
}
181+
182+
[Test]
183+
public void AdfsTenantThrowsCredentialUnavailable()
184+
{
185+
var options = new VisualStudioCredentialOptions { TenantId = "adfs", Transport = new MockTransport() };
186+
187+
VisualStudioCredential credential = InstrumentClient(new VisualStudioCredential(options));
188+
189+
Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/.default" }), CancellationToken.None));
190+
}
181191
}
182192
}

0 commit comments

Comments
 (0)