public void Authenticate_ShouldAddAuthorizationParameter_IfPreviouslyUnassigned() { // Arrange var mockClient = new Mock <IRestClient>(); var mockRequest = new Mock <IRestRequest>(); mockRequest.SetupGet(x => x.Parameters).Returns(new List <Parameter> { new Parameter { Name = "NotMatching" } }); var expectedToken = $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username, password)))}"; // Act authenticator.Authenticate(mockClient.Object, mockRequest.Object); // Assert mockRequest.Verify(x => x.AddParameter("Authorization", expectedToken, ParameterType.HttpHeader), Times.Once); }
/// <exception cref="GPClientException"></exception> public GPConnector GetAppToken(string scope) { var restRequest = new NewtonsoftJsonRestRequest(@"/oauth2/token", Method.POST); restRequest.RequestFormat = DataFormat.Json; restRequest.AddHeader("Accept", "application/json"); restRequest.JsonSerializer.ContentType = "application/x-www-form-urlencoded"; restRequest.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&scope=" + scope, ParameterType.RequestBody); var authenticator = new HttpBasicAuthenticator(ClientID, ClientSecret); authenticator.Authenticate(Client, restRequest); var response = Client.Execute(restRequest); AccessToken = Deserialize <AccessToken>(response.Content); return(this); }
public void Authenticate_ShouldAddAuthorizationParameter_IfPreviouslyUnassigned() { // Arrange var client = new RestClient(); var request = new RestRequest(); request.AddParameter(new Parameter("NotMatching", null, default)); var expectedToken = $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_username}:{_password}"))}"; // Act _authenticator.Authenticate(client, request); // Assert request.Parameters.Single(x => x.Name == "Authorization").Value.ShouldBe(expectedToken); }
public int LaunchJob(int templateId, object extraVars, string limit) { try { AnsibleTowerLaunchPayload payload = new AnsibleTowerLaunchPayload() { extra_vars = extraVars, limit = limit }; var jsonVars = JsonConvert.SerializeObject(payload); ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; var client = new RestClient(this.TowerURI); var authenticator = new HttpBasicAuthenticator(this.TowerUser, this.TowerPassword); var request = new RestRequest("job_templates/" + templateId + "/launch/", Method.POST); request.AddParameter("application/json", jsonVars, ParameterType.RequestBody); authenticator.Authenticate(client, request); IRestResponse response = client.Execute(request); if (response.StatusCode == HttpStatusCode.Created) { var content = JObject.Parse(response.Content); var jobId = content.Value <int>("id"); return(jobId); } else { return(-1); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(-1); } }
/// <summary> /// HTTP Post request sending to remote application with basic authentication and encryption. /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <param name="tree"></param> /// <returns></returns> public string SendRequest(string username, string password, JObject tree) { //We can delete files again we are done with them. DirectoryDelete(Path); DirectoryDelete(PathTree); var request = new RestRequest(Method.POST); var endpoint = ConfigurationManager.AppSettings["RemoteURL"]; var secret = ConfigurationManager.AppSettings["SecretKey"]; var vector = ConfigurationManager.AppSettings["vector"]; var treeString = JsonConvert.SerializeObject(tree); var authenticator = new HttpBasicAuthenticator(username, password); var treeEncrypted = EncryptAesManaged(treeString, secret, vector); var parameters = new JObject ( new JProperty("id", Guid.NewGuid()), new JProperty("tree", treeEncrypted) ); request.AddParameter("application/json", parameters, RestSharp.ParameterType.RequestBody); request.AddHeader("Content-Type", "application/json"); request.AddHeader("Accept", "application/json"); var client = new RestClient(endpoint + "Data/Add") { Timeout = 30000 }; authenticator.Authenticate(client, request); var response = client.Execute(request); Console.WriteLine(response.Content); Console.WriteLine(response.ErrorMessage); Console.WriteLine(response.ErrorException); return(response.IsSuccessful ? "File read and sent to the database. ID of the record: " + response.Content : "An error occured, please contact to system administrator. ErrorCode: " + response.StatusCode); }
public bool CheckJobCompleted(int jobId) { try { var client = new RestClient(this.TowerURI); client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; var authenticator = new HttpBasicAuthenticator(this.TowerUser, this.TowerPassword); var request = new RestRequest("jobs/" + jobId + "/", Method.GET); authenticator.Authenticate(client, request); IRestResponse response = client.Execute(request); if (response.StatusCode == HttpStatusCode.OK) { var content = JObject.Parse(response.Content); var jobStatus = content.Value <string>("status"); if (new string[] { "pending", "waiting", "running" }.Contains(jobStatus)) { return(false); } else { return(true); } } else { return(false); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } }
public void Authenticate(IRestClient client, IRestRequest request) { _authenticator.Authenticate(client, request); }
public IRestRequest AddRequestAuthentication(IRestRequest request) { _httpBasicAuthenticator.Authenticate(null, request); return(request); }
public static void SetBasicAuthorization(string username, string password) { var authenticator = new HttpBasicAuthenticator(username, password); authenticator.Authenticate(client, request); }