コード例 #1
0
        public void InValidEnvironmentInJson()
        {
            var testJson = @"
{
  ""Environments"": [
    {
      ""name"": ""Public"",
      ""authority"": ""https://login.windows-ppe.net/common/oauth2/authorize"",
      ""clientId"": ""ea0616ba-638b-4df5-95b9-636659ae5121"",
      ""redirect"": ""https://login.microsoftonline.com/common/oauth2/nativeclient"",
      ""resource"": ""https://analysis.windows-int.net/powerbi/api"",
      ""globalService"": ""https://api.powerbi.com""
    },
{
      ""name"": ""NotValid"",
      ""authority"": ""https://login.windows-ppe.net/common/oauth2/authorize"",
      ""clientId"": ""ea0616ba-638b-4df5-95b9-636659ae5121"",
      ""redirect"": ""https://login.microsoftonline.com/common/oauth2/nativeclient"",
      ""resource"": ""https://analysis.windows-int.net/powerbi/api"",
      ""globalService"": ""https://api.powerbi.com""
    }
  ],
  ""Settings"": {
    ""ForceDeviceCodeAuthentication"": true
  }
}
";

            var testDir = Directory.GetCurrentDirectory();

            File.WriteAllText(Path.Combine(testDir, "testSettings.json"), testJson);

            var settings = new PowerBISettings(Path.Combine(testDir, "testSettings.json"));

            Assert.IsNotNull(settings.Environments);
            Assert.IsNotNull(settings.Settings);
            Assert.IsTrue(settings.Environments.Count == 1);
            Assert.AreEqual(true, settings.Settings.ForceDeviceCodeAuthentication);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
 public ReportController(IOptions <PowerBISettings> settings)
 {
     _settings = settings.Value;
 }