Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/Common/Commands.Common/AuthenticationFactorySelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.PowerBI.Common.Abstractions;
using Microsoft.PowerBI.Common.Abstractions.Interfaces;
Expand All @@ -19,7 +18,6 @@ public class AuthenticationFactorySelector : IAuthenticationFactory
{
private static IAuthenticationUserFactory UserAuthFactory;
private static IAuthenticationServicePrincipalFactory ServicePrincipalAuthFactory;
private static IAuthenticationBaseFactory BaseAuthFactory;

private void InitializeUserAuthenticationFactory(IPowerBILogger logger, IPowerBISettings settings)
{
Expand All @@ -35,8 +33,6 @@ private void InitializeUserAuthenticationFactory(IPowerBILogger logger, IPowerBI
UserAuthFactory = new DeviceCodeAuthenticationFactory();
}
}

BaseAuthFactory = UserAuthFactory;
}

private void InitializeServicePrincpalAuthenticationFactory(IPowerBILogger logger, IPowerBISettings settings)
Expand All @@ -48,8 +44,6 @@ private void InitializeServicePrincpalAuthenticationFactory(IPowerBILogger logge
ServicePrincipalAuthFactory = new ServicePrincipalAuthenticationFactory();
}
}

BaseAuthFactory = ServicePrincipalAuthFactory;
}

public async Task<IAccessToken> Authenticate(IPowerBIEnvironment environment, IPowerBILogger logger, IPowerBISettings settings, IDictionary<string, string> queryParameters = null)
Expand Down
23 changes: 11 additions & 12 deletions src/Common/Common.Authentication/WindowsAuthenticationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Authentication;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using Microsoft.Identity.Client.Broker;
using Microsoft.PowerBI.Common.Abstractions.Interfaces;
using Microsoft.PowerBI.Common.Abstractions.Utilities;

namespace Microsoft.PowerBI.Common.Authentication
{
Expand Down Expand Up @@ -89,8 +84,10 @@ private async Task<IAccessToken> HandleAuthentication(
throw new NotSupportedException("Authenticator only works on Windows");
}

var passwordBasedAuth = !string.IsNullOrEmpty(userName) && password != null && password.Length > 0;

IEnumerable<string> scopes = new[] { $"{environment.AzureADResource}/.default" };
BuildAuthApplication(environment, queryParameters, logger);
BuildAuthApplication(environment, queryParameters, logger, passwordBasedAuth);
AuthenticationResult result = null;

try
Expand All @@ -104,10 +101,12 @@ private async Task<IAccessToken> HandleAuthentication(
else
{
// auth application is auto cleared when there's no account
BuildAuthApplication(environment, queryParameters, logger);
if (!string.IsNullOrEmpty(userName) && password != null && password.Length > 0)
BuildAuthApplication(environment, queryParameters, logger, passwordBasedAuth);
if (passwordBasedAuth)
{
result = await this.AuthApplication.AcquireTokenByUsernamePassword(scopes, userName, password).ExecuteAsync();
var passwordString = password.SecureStringToString();
result = await this.AuthApplication.AcquireTokenByUsernamePassword(scopes, userName, passwordString)
.ExecuteAsync();
}
else
{
Expand Down Expand Up @@ -150,14 +149,14 @@ public async Task Challenge()
}
}

private void BuildAuthApplication(IPowerBIEnvironment environment, IDictionary<string, string> queryParameters, IPowerBILogger logger)
private void BuildAuthApplication(IPowerBIEnvironment environment, IDictionary<string, string> queryParameters, IPowerBILogger logger, bool passwordBasedAuth = false)
{
// auth application is auto cleared when there's no account
if (this.AuthApplication == null)
{
PublicClientApplicationBuilder authApplicationBuilder = null;
// WAM is only supported on Windows
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
// WAM is only supported on Windows; don't use WAM on passwordBasedAuth
if (!passwordBasedAuth && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
authApplicationBuilder = PublicClientApplicationBuilder
.Create(environment.AzureADClientId)
Expand Down
Loading