Skip to content

Commit b9f809a

Browse files
authored
[Identity] BrokeredAuthentication adding SharedTokenCacheCredentialBrokerOptions (Azure#27917)
* Azure.Identity.BrokeredAuthentication adding SharedTokenCacheCredentialBrokerOptions * updating core ref to project ref * updating api spec * refactoring to use a shared internal interface * fix tests * move extension test back to avoid dups
1 parent 3124de4 commit b9f809a

14 files changed

+135
-28
lines changed

sdk/identity/Azure.Identity.BrokeredAuthentication/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 1.0.0-beta.2 (Unreleased)
44

55
### Features Added
6+
- Added `SharedTokenCacheCredentialBrokerOptions` to enable `SharedTokenCacheCredential` to use the authentication broker for silent authentication calls when this specicialized options type is used to construct the credential.
67

78
### Breaking Changes
89

sdk/identity/Azure.Identity.BrokeredAuthentication/api/Azure.Identity.BrokeredAuthentication.net461.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ public partial class InteractiveBrowserCredentialBrokerOptions : Azure.Identity.
44
{
55
public InteractiveBrowserCredentialBrokerOptions() { }
66
}
7+
public partial class SharedTokenCacheCredentialBrokerOptions : Azure.Identity.SharedTokenCacheCredentialOptions
8+
{
9+
public SharedTokenCacheCredentialBrokerOptions() { }
10+
}
711
}

sdk/identity/Azure.Identity.BrokeredAuthentication/api/Azure.Identity.BrokeredAuthentication.netstandard2.0.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ public partial class InteractiveBrowserCredentialBrokerOptions : Azure.Identity.
44
{
55
public InteractiveBrowserCredentialBrokerOptions() { }
66
}
7+
public partial class SharedTokenCacheCredentialBrokerOptions : Azure.Identity.SharedTokenCacheCredentialOptions
8+
{
9+
public SharedTokenCacheCredentialBrokerOptions() { }
10+
}
711
}

sdk/identity/Azure.Identity.BrokeredAuthentication/src/Azure.Identity.BrokeredAuthentication.csproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@
1111
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1212
</PropertyGroup>
1313
<ItemGroup>
14-
<PackageReference Include="Azure.Core" />
15-
<PackageReference Include="Azure.Identity" VersionOverride="1.6.0-beta.1" />
14+
<!--TODO: Switch back to package reference once Core is released prior to Azure.Identity release
15+
<PackageReference Include="Azure.Core" />
16+
-->
17+
<ProjectReference Include="../../../core/Azure.Core/src/Azure.Core.csproj" />
18+
<!--
19+
Update this project reference back to package refefence after next Azure.Identity
20+
release, before relasing Azure.Identity.BrokeredAuthentication 1.0.0-beta.2.
21+
-->
22+
<!--<PackageReference Include="Azure.Identity" VersionOverride="1.6.0-beta.1" />-->
23+
<ProjectReference Include="../../Azure.Identity/src/Azure.Identity.csproj"/>
1624
<PackageReference Include="System.Memory" />
1725
<PackageReference Include="System.Text.Json" />
1826
<PackageReference Include="System.Threading.Tasks.Extensions" />

sdk/identity/Azure.Identity.BrokeredAuthentication/src/InteractiveBrowserCredentialBrokerOptions.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System;
45
using Microsoft.Identity.Client;
56
#if (NETFRAMEWORK)
67
using Microsoft.Identity.Client.Desktop;
@@ -11,15 +12,9 @@ namespace Azure.Identity.BrokeredAuthentication
1112
/// <summary>
1213
/// Options to configure the <see cref="InteractiveBrowserCredential"/> to use the system authentication broker in lieu of the system browser if available.
1314
/// </summary>
14-
public class InteractiveBrowserCredentialBrokerOptions : InteractiveBrowserCredentialOptions
15+
public class InteractiveBrowserCredentialBrokerOptions : InteractiveBrowserCredentialOptions, IMsalPublicClientInitializerOptions
1516
{
16-
/// <summary>
17-
/// Creates a new instance of <see cref="InteractiveBrowserCredentialBrokerOptions"/>.
18-
/// </summary>
19-
public InteractiveBrowserCredentialBrokerOptions()
20-
{
21-
this.BeforeBuildClient = AddBroker;
22-
}
17+
Action<PublicClientApplicationBuilder> IMsalPublicClientInitializerOptions.BeforeBuildClient => AddBroker;
2318

2419
private void AddBroker(PublicClientApplicationBuilder builder)
2520
{
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;
5+
using Microsoft.Identity.Client;
6+
#if (NETFRAMEWORK)
7+
using Microsoft.Identity.Client.Desktop;
8+
#endif
9+
10+
namespace Azure.Identity.BrokeredAuthentication
11+
{
12+
/// <summary>
13+
/// Options to configure the <see cref="SharedTokenCacheCredential"/> to use the system authentication broker for silent authentication if available.
14+
/// </summary>
15+
public class SharedTokenCacheCredentialBrokerOptions : SharedTokenCacheCredentialOptions, IMsalPublicClientInitializerOptions
16+
{
17+
Action<PublicClientApplicationBuilder> IMsalPublicClientInitializerOptions.BeforeBuildClient => AddBroker;
18+
19+
private void AddBroker(PublicClientApplicationBuilder builder)
20+
{
21+
#if (NETFRAMEWORK)
22+
builder.WithWindowsBroker();
23+
#else
24+
builder.WithBroker();
25+
#endif
26+
}
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Security.AccessControl;
5+
using System.Threading.Tasks;
6+
using Azure.Core;
7+
using NUnit.Framework;
8+
9+
namespace Azure.Identity.BrokeredAuthentication.Tests
10+
{
11+
public class ManualSharedTokenCacheCredentialBrokerTests
12+
{
13+
[Test]
14+
[Ignore("This test is an integration test which can only be run with user interaction")]
15+
public async Task SilentAuthenticateWithBrokerAsync()
16+
{
17+
TokenCachePersistenceOptions persistenceOptions = new TokenCachePersistenceOptions();
18+
19+
// to fully manually verify the InteractiveBrowserCredential this test should be run both authenticating with a
20+
// school / organization account as well as a personal live account, i.e. a @outlook.com, @live.com, or @hotmail.com
21+
var cred = new InteractiveBrowserCredential(new InteractiveBrowserCredentialBrokerOptions { TokenCachePersistenceOptions = persistenceOptions});
22+
23+
AccessToken token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" })).ConfigureAwait(false);
24+
25+
Assert.NotNull(token.Token);
26+
27+
var silentCred = new SharedTokenCacheCredential(new SharedTokenCacheCredentialBrokerOptions());
28+
29+
// The calls below this should be silent and not require user interaction
30+
token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" })).ConfigureAwait(false);
31+
32+
Assert.NotNull(token.Token);
33+
34+
token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://management.core.windows.net//.default" })).ConfigureAwait(false);
35+
36+
Assert.NotNull(token.Token);
37+
}
38+
}
39+
}

sdk/identity/Azure.Identity/src/Credentials/InteractiveBrowserCredential.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ internal InteractiveBrowserCredential(string tenantId, string clientId, TokenCre
8080
Pipeline = pipeline ?? CredentialPipeline.GetInstance(options);
8181
LoginHint = (options as InteractiveBrowserCredentialOptions)?.LoginHint;
8282
var redirectUrl = (options as InteractiveBrowserCredentialOptions)?.RedirectUri?.AbsoluteUri ?? Constants.DefaultRedirectUrl;
83-
var beforeBuildClient = (options as InteractiveBrowserCredentialOptions)?.BeforeBuildClient;
84-
Client = client ?? new MsalPublicClient(Pipeline, tenantId, clientId, redirectUrl, options, beforeBuildClient);
83+
Client = client ?? new MsalPublicClient(Pipeline, tenantId, clientId, redirectUrl, options);
8584
}
8685

8786
/// <summary>

sdk/identity/Azure.Identity/src/Credentials/InteractiveBrowserCredentialOptions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.Threading;
6-
using Microsoft.Identity.Client;
76

87
namespace Azure.Identity
98
{
@@ -54,7 +53,5 @@ public string TenantId
5453
/// Avoids the account prompt and pre-populates the username of the account to login.
5554
/// </summary>
5655
public string LoginHint { get; set; }
57-
58-
internal Action<PublicClientApplicationBuilder> BeforeBuildClient { get; set; }
5956
}
6057
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
using Microsoft.Identity.Client;
8+
9+
namespace Azure.Identity
10+
{
11+
internal interface IMsalPublicClientInitializerOptions
12+
{
13+
Action<PublicClientApplicationBuilder> BeforeBuildClient { get; }
14+
}
15+
}

0 commit comments

Comments
 (0)