private HttpAuthentication SetHttpAuthentication(PSCreateJobParams jobRequest, JobCreateOrUpdateParameters jobUpdateParams) { HttpAuthentication httpAuthentication = null; if (!string.IsNullOrEmpty(jobRequest.HttpAuthType)) { switch (jobRequest.HttpAuthType.ToLower()) { case "clientcertificate": if (jobRequest.ClientCertPfx != null && jobRequest.ClientCertPassword != null) { httpAuthentication = new ClientCertAuthentication { Type = HttpAuthenticationType.ClientCertificate, Password = jobRequest.ClientCertPassword, Pfx = jobRequest.ClientCertPfx }; } else { throw new InvalidOperationException(Resources.SchedulerInvalidClientCertAuthRequest); } break; case "activedirectoryoauth": if (jobRequest.Tenant != null && jobRequest.Audience != null && jobRequest.ClientId != null && jobRequest.Secret != null) { httpAuthentication = new AADOAuthAuthentication { Type = HttpAuthenticationType.ActiveDirectoryOAuth, Tenant = jobRequest.Tenant, Audience = jobRequest.Audience, ClientId = jobRequest.ClientId, Secret = jobRequest.Secret }; } else { throw new InvalidOperationException(Resources.SchedulerInvalidAADOAuthRequest); } break; case "basic": if (jobRequest.Username != null && jobRequest.Password != null) { httpAuthentication = new BasicAuthentication { Type = HttpAuthenticationType.Basic, Username = jobRequest.Username, Password = jobRequest.Password }; } else { throw new InvalidOperationException(Resources.SchedulerInvalidBasicAuthRequest); } break; case "none": if (!string.IsNullOrEmpty(jobRequest.ClientCertPfx) || !string.IsNullOrEmpty(jobRequest.ClientCertPassword) || !string.IsNullOrEmpty(jobRequest.Tenant) || !string.IsNullOrEmpty(jobRequest.Secret) || !string.IsNullOrEmpty(jobRequest.ClientId) || !string.IsNullOrEmpty(jobRequest.Audience) || !string.IsNullOrEmpty(jobRequest.Username) || !string.IsNullOrEmpty(jobRequest.Password)) { throw new InvalidOperationException(Resources.SchedulerInvalidNoneAuthRequest); } break; } } return(httpAuthentication); }
/// <summary> /// Get Http job authentication. /// </summary> /// <param name="authenticationParams">Http authentication properties specified via PowerShell.</param> /// <returns>HttpAuthentication object.</returns> private HttpAuthentication PopulateHttpAuthentication(PSHttpJobAuthenticationParams authenticationParams) { if (authenticationParams == null || authenticationParams.HttpAuthType == null || authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationNone, StringComparison.InvariantCultureIgnoreCase)) { return(null); } else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationClientCertificate, StringComparison.InvariantCultureIgnoreCase)) { if (string.IsNullOrWhiteSpace(authenticationParams.ClientCertPfx) || string.IsNullOrWhiteSpace(authenticationParams.ClientCertPassword)) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidClientCertAuthRequest); } var clientCert = new ClientCertAuthentication() { Type = HttpAuthenticationType.ClientCertificate, Pfx = authenticationParams.ClientCertPfx, Password = authenticationParams.ClientCertPassword }; return(clientCert); } else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationActiveDirectoryOAuth, StringComparison.InvariantCultureIgnoreCase)) { if (string.IsNullOrWhiteSpace(authenticationParams.Tenant) || string.IsNullOrWhiteSpace(authenticationParams.ClientId) || string.IsNullOrWhiteSpace(authenticationParams.Secret) || string.IsNullOrWhiteSpace(authenticationParams.Audience)) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidActiveDirectoryOAuthRequest); } var adOAuth = new OAuthAuthentication() { Type = HttpAuthenticationType.ActiveDirectoryOAuth, Audience = authenticationParams.Audience, ClientId = authenticationParams.ClientId, Secret = authenticationParams.Secret, Tenant = authenticationParams.Tenant }; return(adOAuth); } else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationBasic, StringComparison.InvariantCultureIgnoreCase)) { if (string.IsNullOrWhiteSpace(authenticationParams.Username) || string.IsNullOrWhiteSpace(authenticationParams.Password)) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidBasicRequest); } var basic = new BasicAuthentication() { Type = HttpAuthenticationType.Basic, Username = authenticationParams.Username, Password = authenticationParams.Password }; return(basic); } else { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidAuthenticationType); } }
/// <summary> /// Get Http job authentication. /// </summary> /// <param name="authenticationParams">Http authentication properties specified via PowerShell.</param> /// <returns>HttpAuthentication object.</returns> private HttpAuthentication PopulateHttpAuthentication(PSHttpJobAuthenticationParams authenticationParams) { if (authenticationParams == null || authenticationParams.HttpAuthType == null || authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationNone, StringComparison.InvariantCultureIgnoreCase)) { return null; } else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationClientCertificate, StringComparison.InvariantCultureIgnoreCase)) { if(string.IsNullOrWhiteSpace(authenticationParams.ClientCertPfx) || string.IsNullOrWhiteSpace(authenticationParams.ClientCertPassword)) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidClientCertAuthRequest); } var clientCert = new ClientCertAuthentication() { Type = HttpAuthenticationType.ClientCertificate, Pfx = authenticationParams.ClientCertPfx, Password = authenticationParams.ClientCertPassword }; return clientCert; } else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationActiveDirectoryOAuth, StringComparison.InvariantCultureIgnoreCase)) { if (string.IsNullOrWhiteSpace(authenticationParams.Tenant) || string.IsNullOrWhiteSpace(authenticationParams.ClientId) || string.IsNullOrWhiteSpace(authenticationParams.Secret) || string.IsNullOrWhiteSpace(authenticationParams.Audience)) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidActiveDirectoryOAuthRequest); } var adOAuth = new OAuthAuthentication() { Type = HttpAuthenticationType.ActiveDirectoryOAuth, Audience = authenticationParams.Audience, ClientId = authenticationParams.ClientId, Secret = authenticationParams.Secret, Tenant = authenticationParams.Tenant }; return adOAuth; } else if (authenticationParams.HttpAuthType.Equals(Constants.HttpAuthenticationBasic, StringComparison.InvariantCultureIgnoreCase)) { if(string.IsNullOrWhiteSpace(authenticationParams.Username) || string.IsNullOrWhiteSpace(authenticationParams.Password)) { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidBasicRequest); } var basic = new BasicAuthentication() { Type = HttpAuthenticationType.Basic, Username = authenticationParams.Username, Password = authenticationParams.Password }; return basic; } else { throw new PSManagement.PSArgumentException(Resources.SchedulerInvalidAuthenticationType); } }