Пример #1
0
        private string GetResponseContent(string endpoint, object requestBody)
        {
            Logger.Verbose("Making request to " + endpoint + " endpoint...");

            var restClient  = new RestClient(_apiUrl);
            var restRequest = new RestRequest(endpoint, Method.POST)
            {
                RequestFormat = DataFormat.Json
            };
            var requestBodyString = JsonConvert.SerializeObject(requestBody);

            restRequest.AddParameter("application/json", requestBodyString, ParameterType.RequestBody);

            if (_isAuthorizedRequest)
            {
                if (string.IsNullOrEmpty(_clientId))
                {
                    throw new MissingFieldException("Client ID not provided for authenticated request.");
                }

                if (string.IsNullOrEmpty(_privateKeyPem))
                {
                    throw new MissingFieldException("Public key not provided");
                }

                var signature = AsymmetricCryptoUtil.CreateSignature(requestBodyString, _privateKeyPem);
                restRequest.AddHeader("Authorization", "token " + _clientId + ":" + signature);
            }

            var restResponse = restClient.Execute(restRequest);

            if (restResponse.StatusCode == 0)
            {
                throw new NetworkErrorException("Network error. Could not connect to server.");
            }

            if (restResponse.StatusCode != HttpStatusCode.OK)
            {
                ErrorResponse errorResponse;
                try
                {
                    errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(restResponse.Content);
                }
                catch (Exception)
                {
                    throw new RequestException("Unknown error whilst contacting server");
                }

                switch (restResponse.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                    throw new BadRequestException(errorResponse.Error);

                case HttpStatusCode.Unauthorized:
                    throw new UnauthorizedException(errorResponse.Error);

                case HttpStatusCode.NotFound:
                    throw new NotFoundException(errorResponse.Error);

                case HttpStatusCode.Conflict:
                    throw new ConflictException(errorResponse.ErrorCode, errorResponse.Error);

                default:
                    throw new RequestException(errorResponse.ErrorCode, errorResponse.Error);
                }
            }

            Logger.Verbose("Received response: " + restResponse.Content);

            return(restResponse.Content);
        }
Пример #2
0
 public void CreateVerifySignatureUnsupported()
 {
     Assert.Throws <ArgumentException>(() => AsymmetricCryptoUtil.CreateSignature(TestString, DesPrivateKeyPem));
     Assert.Throws <ArgumentException>(() => AsymmetricCryptoUtil.VerifySignature(TestString, "", DesPublicKeyPem));
 }
Пример #3
0
        private bool VerifyKeyPairSignatures(string message, string privateKeyPem, string publicKeyPem)
        {
            var signature = AsymmetricCryptoUtil.CreateSignature(message, privateKeyPem);

            return(AsymmetricCryptoUtil.VerifySignature(message, signature, publicKeyPem));
        }
Пример #4
0
        private async Task <string> GetResponseContent(string endpoint, string requestBody)
        {
            Logger.Verbose("Making request to " + endpoint + " endpoint...");

            var restClient  = new RestClient(ApiClient.ApiUrl);
            var restRequest = new RestRequest(endpoint, Method.POST)
            {
                RequestFormat = DataFormat.Json
            };

            restRequest.AddParameter("application/json", requestBody, ParameterType.RequestBody);

            if (IsAuthenticatedRequest)
            {
                if (string.IsNullOrEmpty(ApiClient.ApiKey))
                {
                    throw new MissingFieldException("API key not provided for authenticated request.");
                }

                if (string.IsNullOrEmpty(ApiClient.PrivateKeyPem))
                {
                    throw new MissingFieldException("Private key not provided");
                }

                var signature = AsymmetricCryptoUtil.CreateSignature(requestBody, ApiClient.PrivateKeyPem);
                restRequest.AddHeader("Authorization", "token " + ApiClient.ApiKey + ":" + signature);
            }

            IRestResponse restResponse = new RestResponse();

            if (InterruptHandleSet)
            {
                // Perform the request asynchronously, but block on the interrupt handle.
                var responseReceived = false;
                var asyncHandle      = restClient.ExecuteAsync(restRequest, response =>
                {
                    responseReceived = true;
                    restResponse     = response;
                    InterruptHandle.Set();
                });

                InterruptHandle.WaitOne();

                if (!responseReceived)
                {
                    asyncHandle.Abort();
                    throw new RequestException("Network error. Request was interrupted.");
                }
            }
            else
            {
                var cancellationTokenSource = new CancellationTokenSource();
                restResponse = await restClient.ExecuteTaskAsync(restRequest, cancellationTokenSource.Token);
            }

            if (restResponse.StatusCode == 0)
            {
                throw new RequestException("Network error. Could not connect to server.");
            }

            if (restResponse.StatusCode != HttpStatusCode.OK)
            {
                ErrorResponse errorResponse;
                try
                {
                    errorResponse = JsonConvert.DeserializeObject <ErrorResponse>(restResponse.Content);
                }
                catch (Exception)
                {
                    throw new NetworkErrorException("Unknown error whilst contacting server");
                }

                switch (restResponse.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                    throw new BadRequestException(errorResponse.Error);

                case HttpStatusCode.Unauthorized:
                    throw new UnauthorizedException(errorResponse.Error);

                case HttpStatusCode.NotFound:
                    throw new NotFoundException(errorResponse.Error);

                case HttpStatusCode.Conflict:
                    throw new NotFoundException(errorResponse.Error);

                default:
                    throw new RequestException(errorResponse.Error);
                }
            }

            Logger.Verbose("Received response: " + restResponse.Content);

            return(restResponse.Content);
        }