in src/Common/Common.Authentication/WindowsAuthenticationFactory.cs [37:90]
private async Task<IAccessToken> HandleAuthentication(
IPowerBIEnvironment environment,
IPowerBILogger logger,
IPowerBISettings settings,
IDictionary<string, string> queryParameters,
string userName = null,
SecureString password = null)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
throw new NotSupportedException("Authenticator only works on Windows");
}
IEnumerable<string> scopes = new[] { $"{environment.AzureADResource}/.default" };
BuildAuthApplication(environment, queryParameters, logger);
AuthenticationResult result = null;
try
{
var accounts = await this.AuthApplication.GetAccountsAsync();
if (accounts != null && accounts.Any())
{
// This indicates there's token in cache
result = await this.AuthApplication.AcquireTokenSilent(scopes, accounts.First()).ExecuteAsync();
}
else
{
// auth application is auto cleared when there's no account
BuildAuthApplication(environment, queryParameters, logger);
if (!string.IsNullOrEmpty(userName) && password != null && password.Length > 0)
{
result = await this.AuthApplication.AcquireTokenByUsernamePassword(scopes, userName, password).ExecuteAsync();
}
else
{
result = await this.AuthApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
}
}
}
catch (Exception ex)
{
throw new AuthenticationException($"Error Acquiring Token:{System.Environment.NewLine}{ex.Message}");
}
if (result != null)
{
return result.ToIAccessToken();
// Use the token
}
else
{
throw new AuthenticationException("Failed to acquire token");
}
}