Skip to content

Commit

Permalink
[Identity] Throw CredentialUnavailableException from credentials not …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
schaabs authored Sep 2, 2020
1 parent f40bbbe commit c52e764
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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


## 1.2.2 (2020-08-20)
Expand Down
2 changes: 2 additions & 0 deletions sdk/identity/Azure.Identity/src/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal class Constants
{
public const string OrganizationsTenantId = "organizations";

public const string AdfsTenantId = "adfs";

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

Expand Down
5 changes: 5 additions & 0 deletions sdk/identity/Azure.Identity/src/VisualStudioCodeCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ private async ValueTask<AccessToken> GetTokenImplAsync(TokenRequestContext reque
{
GetUserSettings(out var tenant, out var environmentName);

if (string.Equals(tenant, Constants.AdfsTenantId, StringComparison.Ordinal))
{
throw new CredentialUnavailableException("VisualStudioCodeCredential authentication unavailable. ADFS tenant / authorities are not supported.");
}

var cloudInstance = GetAzureCloudInstance(environmentName);
var storedCredentials = _vscAdapter.GetCredentials(CredentialsSection, environmentName);

Expand Down
5 changes: 5 additions & 0 deletions sdk/identity/Azure.Identity/src/VisualStudioCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ private async ValueTask<AccessToken> GetTokenImplAsync(TokenRequestContext reque

try
{
if (string.Equals(_tenantId, Constants.AdfsTenantId, StringComparison.Ordinal))
{
throw new CredentialUnavailableException("VisualStudioCredential authentication unavailable. ADFS tenant/authorities are not supported.");
}

var tokenProviderPath = GetTokenProviderPath();
var tokenProviders = GetTokenProviders(tokenProviderPath);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading;
using Azure.Core;
using Azure.Core.TestFramework;
using NUnit.Framework;

namespace Azure.Identity.Tests
{
public class VisualStudioCodeCredentialTests : ClientTestBase
{
public VisualStudioCodeCredentialTests(bool isAsync) : base(isAsync)
{

}

[Test]
public void AdfsTenantThrowsCredentialUnavailable()
{
var options = new VisualStudioCodeCredentialOptions { TenantId = "adfs", Transport = new MockTransport() };

VisualStudioCodeCredential credential = InstrumentClient(new VisualStudioCodeCredential(options));

Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/.default" }), CancellationToken.None));
}
}
}
10 changes: 10 additions & 0 deletions sdk/identity/Azure.Identity/tests/VisualStudioCredentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,15 @@ public void AuthenticateWithVsCredential_CredentialUnavailableExceptionPassThrou
var credential = InstrumentClient(new VisualStudioCredential(default, default, fileSystem, testProcessFactory));
Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(new[]{"https://vault.azure.net/"}), CancellationToken.None));
}

[Test]
public void AdfsTenantThrowsCredentialUnavailable()
{
var options = new VisualStudioCredentialOptions { TenantId = "adfs", Transport = new MockTransport() };

VisualStudioCredential credential = InstrumentClient(new VisualStudioCredential(options));

Assert.ThrowsAsync<CredentialUnavailableException>(async () => await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://vault.azure.net/.default" }), CancellationToken.None));
}
}
}

0 comments on commit c52e764

Please sign in to comment.