Skip to content

Commit 003fc62

Browse files
authored
Wrap WAM in config check (#20981)
1 parent 2bb87f7 commit 003fc62

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed the issue that errors related to WAM are thrown when it is not enabled. [#20871] [#20824]
2223
* Updated Azure.Core library to 1.28.0.
2324
* Fixed an issue that the helper message about missing modules shows up at the wrong time. [#19228]
2425

src/Accounts/Authentication/Authentication/TokenCache/PowerShellTokenCacheProvider.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
using Hyak.Common;
2222

2323
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
24+
using Microsoft.Azure.Commands.Shared.Config;
2425
using Microsoft.Azure.Internal.Subscriptions;
2526
using Microsoft.Azure.Internal.Subscriptions.Models;
27+
using Microsoft.Azure.PowerShell.Common.Config;
2628
using Microsoft.Identity.Client;
2729
using Microsoft.Identity.Client.Broker;
2830
using Microsoft.Rest;
@@ -162,11 +164,19 @@ private SubscriptionClient GetSubscriptionClient(IAccessToken token, IAzureEnvir
162164

163165
protected abstract void RegisterCache(IPublicClientApplication client);
164166

167+
/// <summary>
168+
/// Creates a public client app.
169+
/// This method is not meant for authentication purpose. Use APIs from Azure.Identity instead.
170+
/// </summary>
165171
public virtual IPublicClientApplication CreatePublicClient(string authority = null)
166172
{
167-
var builder = PublicClientApplicationBuilder.Create(Constants.PowerShellClientId).WithBrokerPreview();
168-
169-
if(!string.IsNullOrEmpty(authority))
173+
var builder = PublicClientApplicationBuilder.Create(Constants.PowerShellClientId);
174+
if (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var config)
175+
&& config.GetConfigValue<bool>(ConfigKeys.EnableLoginByWam))
176+
{
177+
builder = builder.WithBrokerPreview();
178+
}
179+
if (!string.IsNullOrEmpty(authority))
170180
{
171181
builder.WithAuthority(authority);
172182
}

src/Accounts/Authenticators/SilentAuthenticator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ public override Task<IAccessToken> Authenticate(AuthenticationParameters paramet
5858

5959
private static SharedTokenCacheCredentialOptions GetTokenCredentialOptions(SilentParameters silentParameters, string tenantId, string authority, PowerShellTokenCacheProvider tokenCacheProvider)
6060
{
61-
SharedTokenCacheCredentialOptions options =
62-
new SharedTokenCacheCredentialBrokerOptions(tokenCacheProvider.GetTokenCachePersistenceOptions());
61+
bool isWamEnabled = AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var config)
62+
&& config.GetConfigValue<bool>(ConfigKeys.EnableLoginByWam);
63+
SharedTokenCacheCredentialOptions options = isWamEnabled
64+
? new SharedTokenCacheCredentialBrokerOptions(tokenCacheProvider.GetTokenCachePersistenceOptions())
65+
: new SharedTokenCacheCredentialOptions(tokenCacheProvider.GetTokenCachePersistenceOptions());
6366
options.EnableGuestTenantAuthentication = true;
6467
options.ClientId = Constants.PowerShellClientId;
6568
options.Username = silentParameters.UserId;

0 commit comments

Comments
 (0)