Esempio n. 1
0
        private async Task <HttpResponseMessage> DoRequestAsync(HttpRequestMessage request)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                var authToken = signatureGenerator.GenerateSignature();
                httpClient.DefaultRequestHeaders.Add("Authorization", authToken);
                httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

                var result = await httpClient.SendAsync(request).ConfigureAwait(false);

                if (result.IsSuccessStatusCode)
                {
                    return(result);
                }

                var content = await result.Content.ReadAsStringAsync();

                try
                {
                    var errors = serialization.Deserialize <ErrorResult>(content);
                    throw new PaySimpleEndpointException(errors, result.StatusCode);
                }
                catch (Exception e) when(!(e is PaySimpleEndpointException))
                {
                    throw new PaySimpleEndpointException($"Error deserializing response: {content}", e);
                }
            }
        }
Esempio n. 2
0
        private async Task <HttpResponseMessage> DoRequestAsync(HttpRequestMessage request)
        {
            var authToken = signatureGenerator.GenerateSignature();

            request.Headers.Add("Authorization", authToken);
            request.Headers.Add("Accept", "application/json");
            // Unfortunately the PS Api is incorrectly using the 204 (NO CONTENT) status code. It is returning content with a 204 which causes protocol violations
            // Closing the connection allows this to work, but no more connection reuse for now
            request.Headers.Add("Connection", "close");


            var result = await httpClient.SendAsync(request).ConfigureAwait(false);

            if (result.IsSuccessStatusCode)
            {
                return(result);
            }

            using (var content = await result.Content.ReadAsStreamAsync())
            {
                try
                {
                    var errors = serialization.Deserialize <ErrorResult>(content);
                    throw new PaySimpleEndpointException(errors, result.StatusCode);
                }
                catch (Exception e) when(!(e is PaySimpleEndpointException))
                {
                    throw new PaySimpleEndpointException($"Error deserializing response: {GetResponseString(content)}", e);
                }
            }
        }
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var signature = _signatureGenerator.GenerateSignature(Request.FromHttpRequest(request), _signatureSpecification);

            if (_signatureSpecification.UseSignatureAsAuthroizationHeader)
            {
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Signature", signature);
            }
            else
            {
                request.Headers.Add("signature", signature);
            }
            return(base.SendAsync(request, cancellationToken));
        }
Esempio n. 4
0
        /// <summary>
        /// Verifies the signature of this response matches the expected signature using the <see cref="ISignatureGenerator"/> provided.
        /// </summary>
        /// <remarks>
        /// <para>The <see cref="HummClient"/> automatically calls this method when a reponse is received, application code does not need to call this method explicitly unless performing it's own calls without using a <see cref="HummClient"/> instance.</para>
        /// <para>The <paramref name="signatureGenerator"/> must have been created/initialised with the same device key as the request that generated this response.</para>
        /// <para>If the <see cref="Signature"/> property is null or empty, this call is a no-op and will return without throwing an exception.</para>
        /// </remarks>
        /// <param name="signatureGenerator">The signature generator to use to confirm the response signature.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="signatureGenerator"/> is null.</exception>
        /// <exception cref="HummResponseSignatureException">Thrown if the signature of the response does not match the expected signature.</exception>
        public void VerifySignature(ISignatureGenerator signatureGenerator)
        {
            if (String.IsNullOrEmpty(Signature))
            {
                return;
            }

            signatureGenerator.GuardNull(nameof(signatureGenerator));

            var calcSig = signatureGenerator.GenerateSignature(HummModelPropertyReader.GetPropertyValues(this));

            if (calcSig != this.Signature)
            {
                throw new HummResponseSignatureException();
            }
        }