in src/Modules/Profile/Commands.Profile/ConnectPowerBIServiceAccount.cs [79:175]
public override void ExecuteCmdlet()
{
IPowerBIEnvironment environment = null;
// Populate custom environments from discovery url if it is present
// otherwise get environment from existing settings
if (!string.IsNullOrEmpty(this.DiscoveryUrl))
{
if (string.IsNullOrEmpty(this.CustomEnvironment))
{
throw new Exception($"{nameof(this.CustomEnvironment)} is required when using a discovery url");
}
var settings = new PowerBISettings();
CustomEnvironments = new Dictionary<string, IPowerBIEnvironment>();
var customCloudEnvironments = GetServiceConfig(this.DiscoveryUrl).Result;
foreach (GSEnvironment customEnvironment in customCloudEnvironments.Environments)
{
var backendService = customEnvironment.Services.First(s => s.Name.Equals("powerbi-backend", StringComparison.OrdinalIgnoreCase));
var redirectApp = settings.Environments[PowerBIEnvironmentType.Public];
var env = new PowerBIEnvironment()
{
Name = PowerBIEnvironmentType.Custom,
AzureADAuthority = customEnvironment.Services.First(s => s.Name.Equals("aad", StringComparison.OrdinalIgnoreCase)).Endpoint,
AzureADClientId = redirectApp.AzureADClientId,
AzureADRedirectAddress = redirectApp.AzureADRedirectAddress,
AzureADResource = backendService.ResourceId,
GlobalServiceEndpoint = backendService.Endpoint
};
this.CustomEnvironments.Add(customEnvironment.CloudName, env);
}
if (!this.CustomEnvironments.ContainsKey(this.CustomEnvironment))
{
this.Logger.ThrowTerminatingError($"Discovery URL {this.DiscoveryUrl} did not return environment {this.CustomEnvironment}");
}
environment = this.CustomEnvironments[this.CustomEnvironment];
}
else
{
var settings = new PowerBISettings(targetEnvironmentType: this.Environment, refreshGlobalServiceConfig: true);
if (settings.Environments == null)
{
this.Logger.ThrowTerminatingError("Failed to populate environments in settings");
}
environment = settings.Environments[this.Environment];
}
if(!string.IsNullOrEmpty(this.Tenant))
{
var tempEnvironment = (PowerBIEnvironment) environment;
tempEnvironment.AzureADAuthority = tempEnvironment.AzureADAuthority.ToLowerInvariant().Replace("/common", $"/{this.Tenant}");
this.Logger.WriteVerbose($"Updated Azure AD authority with -Tenant specified, new value: {tempEnvironment.AzureADAuthority}");
environment = tempEnvironment;
}
else
{
var tempEnvironment = (PowerBIEnvironment)environment;
tempEnvironment.AzureADAuthority = tempEnvironment.AzureADAuthority.ToLowerInvariant().Replace("/common", "/organizations");
this.Logger.WriteVerbose($"Updated Azure AD authority with /organizations endpoint, new value: {tempEnvironment.AzureADAuthority}");
environment = tempEnvironment;
}
this.Authenticator.Challenge(); // revoke any previous login
IAccessToken token = null;
PowerBIProfile profile = null;
switch (this.ParameterSet)
{
case UserParameterSet:
token = this.Authenticator.Authenticate(environment, this.Logger, this.Settings, new Dictionary<string, string>()
{
{ "msafed", "0" }
}
).Result;
profile = new PowerBIProfile(environment, token);
break;
case UserAndCredentialPasswordParameterSet:
token = this.Authenticator.Authenticate(environment, this.Logger, this.Settings, this.Credential.UserName, this.Credential.Password).Result;
profile = new PowerBIProfile(environment, this.Credential.UserName, this.Credential.Password, token, servicePrincipal: false);
break;
case ServicePrincipalCertificateParameterSet:
token = this.Authenticator.Authenticate(this.ApplicationId, this.CertificateThumbprint, environment, this.Logger, this.Settings).Result;
profile = new PowerBIProfile(environment, this.ApplicationId, this.CertificateThumbprint, token);
break;
case ServicePrincipalParameterSet:
token = this.Authenticator.Authenticate(this.Credential.UserName, this.Credential.Password, environment, this.Logger, this.Settings).Result;
profile = new PowerBIProfile(environment, this.Credential.UserName, this.Credential.Password, token);
break;
default:
throw new NotImplementedException($"Parameter set {this.ParameterSet} was not implemented");
}
this.Storage.SetItem("profile", profile);
this.Logger.WriteObject(profile);
}