Skip to content

Commit

Permalink
fb
Browse files Browse the repository at this point in the history
  • Loading branch information
christothes committed Sep 7, 2021
1 parent d7da697 commit a6d39f7
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 24 deletions.
2 changes: 1 addition & 1 deletion sdk/identity/Azure.Identity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Features Added

- `DefaultAzureCredentialOptions` now has a `ClientId` property which allows passing a ClientId value to the `SharedTokenCredential` and `InteractiveBrowserCredential` when constructing a `DefaultAzureCredential`.
- `DefaultAzureCredentialOptions` now has a `InteractiveBrowserClientId` property which allows passing a ClientId value to the InteractiveBrowserCredential` when constructing a `DefaultAzureCredential`.
- Implement `OnBehalfOfCredential` which enables authentication to Azure Active Directory using an On-Behalf-Of flow.

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ public DefaultAzureCredential(bool includeInteractiveCredentials = false) { }
public partial class DefaultAzureCredentialOptions : Azure.Identity.TokenCredentialOptions
{
public DefaultAzureCredentialOptions() { }
public string ClientId { get { throw null; } set { } }
public bool ExcludeAzureCliCredential { get { throw null; } set { } }
public bool ExcludeAzurePowerShellCredential { get { throw null; } set { } }
public bool ExcludeEnvironmentCredential { get { throw null; } set { } }
Expand All @@ -148,6 +147,7 @@ public DefaultAzureCredentialOptions() { }
public bool ExcludeSharedTokenCacheCredential { get { throw null; } set { } }
public bool ExcludeVisualStudioCodeCredential { get { throw null; } set { } }
public bool ExcludeVisualStudioCredential { get { throw null; } set { } }
public string InteractiveBrowserCredentialClientId { get { throw null; } set { } }
public string InteractiveBrowserTenantId { get { throw null; } set { } }
public string ManagedIdentityClientId { get { throw null; } set { } }
public string SharedTokenCacheTenantId { get { throw null; } set { } }
Expand Down
4 changes: 2 additions & 2 deletions sdk/identity/Azure.Identity/src/DefaultAzureCredential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static TokenCredential[] GetDefaultAzureCredentialChain(DefaultAzureCred

if (!options.ExcludeSharedTokenCacheCredential)
{
chain[i++] = factory.CreateSharedTokenCacheCredential(options.SharedTokenCacheTenantId, options.SharedTokenCacheUsername, options.ClientId);
chain[i++] = factory.CreateSharedTokenCacheCredential(options.SharedTokenCacheTenantId, options.SharedTokenCacheUsername);
}

if (!options.ExcludeVisualStudioCredential)
Expand All @@ -229,7 +229,7 @@ private static TokenCredential[] GetDefaultAzureCredentialChain(DefaultAzureCred

if (!options.ExcludeInteractiveBrowserCredential)
{
chain[i++] = factory.CreateInteractiveBrowserCredential(options.InteractiveBrowserTenantId, options.ClientId);
chain[i++] = factory.CreateInteractiveBrowserCredential(options.InteractiveBrowserTenantId, options.InteractiveBrowserCredentialClientId);
}

if (i == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public virtual TokenCredential CreateManagedIdentityCredential(string clientId)
return new ManagedIdentityCredential(clientId, Pipeline);
}

public virtual TokenCredential CreateSharedTokenCacheCredential(string tenantId, string username, string cliendId)
public virtual TokenCredential CreateSharedTokenCacheCredential(string tenantId, string username)
{
return new SharedTokenCacheCredential(tenantId, username, new SharedTokenCacheCredentialOptions{ ClientId = cliendId }, Pipeline);
return new SharedTokenCacheCredential(tenantId, username, null, Pipeline);
}

public virtual TokenCredential CreateInteractiveBrowserCredential(string tenantId, string clientId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class DefaultAzureCredentialOptions : TokenCredentialOptions
/// <summary>
/// Specifies the client id of the selected credential
/// </summary>
public string ClientId { get; set; } = GetNonEmptyStringOrNull(EnvironmentVariables.ClientId);
public string InteractiveBrowserCredentialClientId { get; set; }

/// <summary>
/// Specifies the client id of the azure ManagedIdentity in the case of user assigned identity.
Expand Down
20 changes: 8 additions & 12 deletions sdk/identity/Azure.Identity/tests/DefaultAzureCredentialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void ValidateCtorOptionsPassedToCredentials()
string expVsTenantId = Guid.NewGuid().ToString();
string expCodeTenantId = Guid.NewGuid().ToString();
string actClientId_ManagedIdentity = null;
string actClientId_SharedToken = null;
string actClientId_InteractiveBrowser = null;
string actUsername = null;
string actCacheTenantId = null;
Expand All @@ -87,15 +86,15 @@ public void ValidateCtorOptionsPassedToCredentials()
var credFactory = new MockDefaultAzureCredentialFactory(CredentialPipeline.GetInstance(null));

credFactory.OnCreateManagedIdentityCredential = (clientId, _) => actClientId_ManagedIdentity = clientId;
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, clientId, _) => { actCacheTenantId = tenantId; actUsername = username; actClientId_SharedToken = clientId; };
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _) => { actCacheTenantId = tenantId; actUsername = username; };
credFactory.OnCreateInteractiveBrowserCredential = (tenantId, clientId, _) => { actBrowserTenantId = tenantId; actClientId_InteractiveBrowser = clientId; };
credFactory.OnCreateVisualStudioCredential = (tenantId, _) => { actVsTenantId = tenantId; };
credFactory.OnCreateVisualStudioCodeCredential = (tenantId, _) => { actCodeTenantId = tenantId; };
credFactory.OnCreateAzurePowerShellCredential = _ => {};

var options = new DefaultAzureCredentialOptions
{
ClientId = expClientId,
InteractiveBrowserCredentialClientId = expClientId,
ManagedIdentityClientId = expClientId,
SharedTokenCacheUsername = expUsername,
ExcludeSharedTokenCacheCredential = false,
Expand All @@ -109,7 +108,6 @@ public void ValidateCtorOptionsPassedToCredentials()
new DefaultAzureCredential(credFactory, options);

Assert.AreEqual(expClientId, actClientId_ManagedIdentity);
Assert.AreEqual(expClientId, actClientId_SharedToken);
Assert.AreEqual(expClientId, actClientId_InteractiveBrowser);
Assert.AreEqual(expUsername, actUsername);
Assert.AreEqual(expCacheTenantId, actCacheTenantId);
Expand Down Expand Up @@ -146,19 +144,17 @@ public void ValidateEnvironmentBasedOptionsPassedToCredentials([Values] bool cli
Assert.AreEqual(expClientId, clientId);
};

credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, clientId, _) =>
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _) =>
{
onCreateSharedCalled = true;
Assert.AreEqual(expTenantId, tenantId);
Assert.AreEqual(expUsername, username);
Assert.AreEqual(expClientId, clientId);
};

credFactory.OnCreateInteractiveBrowserCredential = (tenantId, clientId, _) =>
{
onCreateInteractiveCalled = true;
Assert.AreEqual(expTenantId, tenantId);
Assert.AreEqual(expClientId, clientId);
};

credFactory.OnCreateVisualStudioCredential = (tenantId, _) =>
Expand Down Expand Up @@ -221,7 +217,7 @@ public void ValidateEmptyEnvironmentBasedOptionsNotPassedToCredentials([Values]
Assert.IsNull(clientId);
};

credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _, _) =>
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _) =>
{
onCreateSharedCalled = true;
Assert.IsNull(tenantId);
Expand Down Expand Up @@ -297,7 +293,7 @@ public void ValidateCtorWithExcludeOptions([Values(true, false)]bool excludeEnvi
{
managedIdentityCredentialIncluded = true;
};
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _, _) =>
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _) =>
{
sharedTokenCacheCredentialIncluded = true;
};
Expand Down Expand Up @@ -360,7 +356,7 @@ void SetupMockForException<T>(Mock<T> mock) where T : TokenCredential =>
SetupMockForException(c);
credFactory.OnCreateManagedIdentityCredential = (_, c) =>
SetupMockForException(c);
credFactory.OnCreateSharedTokenCacheCredential = (_, _, _, c) =>
credFactory.OnCreateSharedTokenCacheCredential = (_, _, c) =>
SetupMockForException(c);
credFactory.OnCreateAzureCliCredential = c =>
SetupMockForException(c);
Expand Down Expand Up @@ -446,7 +442,7 @@ void SetupMockForException<T>(Mock<T> mock) where T : TokenCredential
SetupMockForException(c);
credFactory.OnCreateManagedIdentityCredential = (_, c) =>
SetupMockForException(c);
credFactory.OnCreateSharedTokenCacheCredential = (_, _, _, c) =>
credFactory.OnCreateSharedTokenCacheCredential = (_, _, c) =>
SetupMockForException(c);
credFactory.OnCreateVisualStudioCredential = (_, c) =>
SetupMockForException(c);
Expand Down Expand Up @@ -576,7 +572,7 @@ void SetupMockForException<T>(Mock<T> mock) where T : TokenCredential =>
SetupMockForException(c);
credFactory.OnCreateManagedIdentityCredential = (clientId, c) =>
SetupMockForException(c);
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, _, c) =>
credFactory.OnCreateSharedTokenCacheCredential = (tenantId, username, c) =>
SetupMockForException(c);
credFactory.OnCreateAzureCliCredential = c =>
SetupMockForException(c);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public MockDefaultAzureCredentialFactory(CredentialPipeline pipeline) : base(pip
private Mock<AzureCliCredential> mockAzureCliCredential = new();
public Action<string, Mock<ManagedIdentityCredential>> OnCreateManagedIdentityCredential { get; set; }
private Mock<ManagedIdentityCredential> mockManagedIdentityCredential = new();
public Action<string, string, string, Mock<SharedTokenCacheCredential>> OnCreateSharedTokenCacheCredential { get; set; }
public Action<string, string, Mock<SharedTokenCacheCredential>> OnCreateSharedTokenCacheCredential { get; set; }
private Mock<SharedTokenCacheCredential> mockSharedTokenCacheCredential = new();
public Action<string, string, Mock<InteractiveBrowserCredential>> OnCreateInteractiveBrowserCredential { get; set; }
private Mock<InteractiveBrowserCredential> mockInteractiveBrowserCredential = new();
Expand All @@ -40,9 +40,9 @@ public override TokenCredential CreateManagedIdentityCredential(string clientId)
return mockManagedIdentityCredential.Object;
}

public override TokenCredential CreateSharedTokenCacheCredential(string tenantId, string username, string clientId)
public override TokenCredential CreateSharedTokenCacheCredential(string tenantId, string username)
{
OnCreateSharedTokenCacheCredential?.Invoke(tenantId, username, clientId, mockSharedTokenCacheCredential);
OnCreateSharedTokenCacheCredential?.Invoke(tenantId, username, mockSharedTokenCacheCredential);
return mockSharedTokenCacheCredential.Object;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public override TokenCredential CreateEnvironmentCredential()
public override TokenCredential CreateManagedIdentityCredential(string clientId)
=> new ManagedIdentityCredential(new ManagedIdentityClient(Pipeline, clientId));

public override TokenCredential CreateSharedTokenCacheCredential(string tenantId, string username, string clientId)
=> new SharedTokenCacheCredential(tenantId, username, new SharedTokenCacheCredentialOptions { ClientId = clientId }, Pipeline);
public override TokenCredential CreateSharedTokenCacheCredential(string tenantId, string username)
=> new SharedTokenCacheCredential(tenantId, username, null, Pipeline);

public override TokenCredential CreateInteractiveBrowserCredential(string tenantId, string clientId)
=> new InteractiveBrowserCredential(tenantId, clientId ?? Constants.DeveloperSignOnClientId, new InteractiveBrowserCredentialOptions(), Pipeline);
Expand Down

0 comments on commit a6d39f7

Please sign in to comment.