/// <summary> /// /// </summary> /// <param name="account"></param> /// <param name="environment"></param> /// <param name="redirectUri"></param> /// <returns></returns> public IClientApplicationBase GetClient(PartnerAccount account, PartnerEnvironment environment, string redirectUri = null) { IClientApplicationBase app; if (account.IsPropertySet(PartnerAccountPropertyType.CertificateThumbprint) || account.IsPropertySet(PartnerAccountPropertyType.ServicePrincipalSecret)) { app = SharedTokenCacheClientFactory.CreateConfidentialClient( $"{environment.ActiveDirectoryAuthority}{account.Tenant}", account.GetProperty(PartnerAccountPropertyType.ApplicationId), account.GetProperty(PartnerAccountPropertyType.ServicePrincipalSecret), GetCertificate(account.GetProperty(PartnerAccountPropertyType.CertificateThumbprint)), redirectUri, account.Tenant); } else { app = SharedTokenCacheClientFactory.CreatePublicClient( $"{environment.ActiveDirectoryAuthority}{account.Tenant}", account.GetProperty(PartnerAccountPropertyType.ApplicationId), redirectUri, account.Tenant); } return(app); }
/// <summary> /// Gets an aptly configured client. /// </summary> /// <param name="account">The account information to be used when generating the client.</param> /// <param name="environment">The environment where the client is connecting.</param> /// <param name="redirectUri">The redirect URI for the client.</param> /// <returns>An aptly configured client.</returns> public IClientApplicationBase GetClient(PartnerAccount account, PartnerEnvironment environment, string redirectUri = null) { IClientApplicationBase app; if (account.IsPropertySet(PartnerAccountPropertyType.CertificateThumbprint) || account.IsPropertySet(PartnerAccountPropertyType.ServicePrincipalSecret)) { app = CreateConfidentialClient( GetAzureCloudInstance(environment), account.GetProperty(PartnerAccountPropertyType.ApplicationId), account.GetProperty(PartnerAccountPropertyType.ServicePrincipalSecret), GetCertificate(account.GetProperty(PartnerAccountPropertyType.CertificateThumbprint)), redirectUri, account.Tenant); } else { app = CreatePublicClient( GetAzureCloudInstance(environment), account.GetProperty(PartnerAccountPropertyType.ApplicationId), redirectUri, account.Tenant); } return(app); }
/// <summary> /// Performs the operations associated with the command. /// </summary> protected override void ProcessRecord() { IPartner partnerOperations; OrganizationProfile profile; PartnerAccount account = new PartnerAccount(); PartnerEnvironment environment = PartnerEnvironment.PublicEnvironments[Environment]; PartnerService.Instance.EnforceMfa = (EnforceMFA.IsPresent && EnforceMFA.ToBool()); if (!string.IsNullOrEmpty(CertificateThumbprint)) { account.SetProperty(PartnerAccountPropertyType.CertificateThumbprint, CertificateThumbprint); } if (!string.IsNullOrEmpty(RefreshToken)) { account.SetProperty(PartnerAccountPropertyType.RefreshToken, RefreshToken); } account.SetProperty(PartnerAccountPropertyType.ApplicationId, PowerShellApplicationId); if (ParameterSetName.Equals(AccessTokenParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.SetProperty(PartnerAccountPropertyType.AccessToken, AccessToken); account.Type = AccountType.AccessToken; } else if (ParameterSetName.Equals(RefreshTokenParameterSet, StringComparison.InvariantCultureIgnoreCase)) { if (Credential != null) { account.ObjectId = Credential.UserName; account.SetProperty(PartnerAccountPropertyType.ApplicationId, Credential.UserName); account.SetProperty(PartnerAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString()); } } else if (ParameterSetName.Equals(ServicePrincipalCertificateParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.SetProperty(PartnerAccountPropertyType.ApplicationId, ApplicationId); } else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.ObjectId = Credential.UserName; account.Type = AccountType.ServicePrincipal; account.SetProperty(PartnerAccountPropertyType.ApplicationId, Credential.UserName); account.SetProperty(PartnerAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString()); } else { account.Type = AccountType.User; } if (UseDeviceAuthentication.IsPresent) { account.SetProperty("UseDeviceAuth", "true"); } account.SetProperty( PartnerAccountPropertyType.Scope, ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase) ? $"{environment.AzureAdGraphEndpoint}/.default" : $"{environment.PartnerCenterEndpoint}/user_impersonation"); account.Tenant = string.IsNullOrEmpty(Tenant) ? "common" : Tenant; PartnerSession.Instance.AuthenticationFactory.Authenticate( account, environment, new[] { account.GetProperty(PartnerAccountPropertyType.Scope) }, Message); PartnerSession.Instance.Context = new PartnerContext { Account = account, Environment = environment }; try { partnerOperations = PartnerSession.Instance.ClientFactory.CreatePartnerOperations(); profile = partnerOperations.Profiles.OrganizationProfile.GetAsync().GetAwaiter().GetResult(); PartnerSession.Instance.Context.CountryCode = profile.DefaultAddress.Country; PartnerSession.Instance.Context.Locale = profile.Culture; } catch (PartnerException) { /* This error can safely be ignored */ } WriteObject(PartnerSession.Instance.Context); }
/// <summary> /// Executes the operations associated with the cmdlet. /// </summary> public override void ExecuteCmdlet() { Scheduler.RunTask(async() => { IPartner partnerOperations; OrganizationProfile profile; PartnerAccount account = new PartnerAccount(); PartnerEnvironment environment = PartnerEnvironment.PublicEnvironments[Environment]; if (!string.IsNullOrEmpty(CertificateThumbprint)) { account.SetProperty(PartnerAccountPropertyType.CertificateThumbprint, CertificateThumbprint); } if (!string.IsNullOrEmpty(RefreshToken)) { account.SetProperty(PartnerAccountPropertyType.RefreshToken, RefreshToken); } account.SetProperty(PartnerAccountPropertyType.ApplicationId, string.IsNullOrEmpty(ApplicationId) ? PowerShellApplicationId : ApplicationId); if (ParameterSetName.Equals(AccessTokenParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.SetProperty(PartnerAccountPropertyType.AccessToken, AccessToken); account.Type = AccountType.AccessToken; } else if (ParameterSetName.Equals(RefreshTokenParameterSet, StringComparison.InvariantCultureIgnoreCase)) { if (Credential != null) { account.ObjectId = Credential.UserName; account.SetProperty(PartnerAccountPropertyType.ApplicationId, Credential.UserName); account.SetProperty(PartnerAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString()); } } else if (ParameterSetName.Equals(ServicePrincipalCertificateParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.SetProperty(PartnerAccountPropertyType.ApplicationId, ApplicationId); account.Type = AccountType.Certificate; } else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase)) { account.ObjectId = Credential.UserName; account.Type = AccountType.ServicePrincipal; account.SetProperty(PartnerAccountPropertyType.ApplicationId, Credential.UserName); account.SetProperty(PartnerAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString()); } else { account.Type = AccountType.User; } if (UseDeviceAuthentication.IsPresent) { account.SetProperty("UseDeviceAuth", "true"); } account.SetProperty( PartnerAccountPropertyType.Scope, ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase) ? $"{environment.AzureAdGraphEndpoint}/.default" : $"{environment.PartnerCenterEndpoint}/user_impersonation"); account.Tenant = string.IsNullOrEmpty(Tenant) ? "organizations" : Tenant; await PartnerSession.Instance.AuthenticationFactory.AuthenticateAsync( account, environment, new[] { account.GetProperty(PartnerAccountPropertyType.Scope) }, Message, CancellationToken).ConfigureAwait(false); PartnerSession.Instance.Context = new PartnerContext { Account = account, Environment = environment }; try { partnerOperations = await PartnerSession.Instance.ClientFactory.CreatePartnerOperationsAsync(CorrelationId).ConfigureAwait(false); profile = await partnerOperations.Profiles.OrganizationProfile.GetAsync().ConfigureAwait(false); PartnerSession.Instance.Context.CountryCode = profile.DefaultAddress.Country; PartnerSession.Instance.Context.Locale = profile.Culture; } catch (PartnerException) { /* This error can safely be ignored */ } WriteObject(PartnerSession.Instance.Context); }); }