Beispiel #1
0
        private async Task <TResult> SendAsync <TResult>(HttpMethod method, Uri uri, object message) where TResult : class
        {
            Verbose($"{method} {uri} {message?.GetType()}");
            var nonceHeader = new AcmeHeader {
                Nonce = nonce
            };

            Verbose($"sending nonce {nonce}");

            var request = new HttpRequestMessage(method, uri);

            if (message != null)
            {
                var encodedMessage = jws.Encode(message, nonceHeader);
                var json           = JsonConvert.SerializeObject(encodedMessage, jsonSettings);

                request.Content = new StringContent(json, Encoding.UTF8, "application/json");
            }

            var response = await client.SendAsync(request).ConfigureAwait(false);

            Verbose($"response status: {(int)response.StatusCode} {response.ReasonPhrase}");

            RememberNonce(response);

            if (response.Content.Headers.ContentType.MediaType == "application/problem+json")
            {
                var problemJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var problem = JsonConvert.DeserializeObject <Problem>(problemJson);
                Verbose($"error response from server: {problem.Type}: {problem.Detail}");
                throw new AcmeException(problem, response);
            }

            if (typeof(TResult) == typeof(CertificateResponse) &&
                response.Content.Headers.ContentType.MediaType == "application/pkix-cert")
            {
                var certificateBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

                var certificateResponse = new CertificateResponse {
                    Certificate = certificateBytes
                };

                GetHeaderValues(response, certificateResponse);

                return(certificateResponse as TResult);
            }

            var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var responseContent = JObject.Parse(responseText).ToObject <TResult>();

            GetHeaderValues(response, responseContent);

            return(responseContent);
        }
Beispiel #2
0
            private async Task <TResult> SendAsync <TResult>(HttpMethod method, Uri uri, object message, CancellationToken token) where TResult : class
            {
                var nonceHeader = new AcmeHeader
                {
                    Nonce = _nonce
                };

                var request = new HttpRequestMessage(method, uri);

                if (message != null)
                {
                    var encodedMessage = _jws.Encode(message, nonceHeader);
                    var json           = JsonConvert.SerializeObject(encodedMessage, jsonSettings);

                    request.Content = new StringContent(json, Encoding.UTF8, "application/json");
                }

                var response = await _client.SendAsync(request, token).ConfigureAwait(false);

                RememberNonce(response);

                if (response.Content.Headers.ContentType.MediaType == "application/problem+json")
                {
                    var problemJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var problem = JsonConvert.DeserializeObject <Problem>(problemJson);
                    throw new AcmeException(problem, response);
                }

                if (typeof(TResult) == typeof(CertificateResponse) &&
                    response.Content.Headers.ContentType.MediaType == "application/pkix-cert")
                {
                    var certificateBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

                    var certificateResponse = new CertificateResponse
                    {
                        Certificate = certificateBytes
                    };

                    GetHeaderValues(response, certificateResponse);

                    return(certificateResponse as TResult);
                }

                var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var responseContent = JObject.Parse(responseText).ToObject <TResult>();

                GetHeaderValues(response, responseContent);

                return(responseContent);
            }
Beispiel #3
0
            private async Task <TResult> SendAsync <TResult>(HttpMethod method, Uri uri, object message, CancellationToken token) where TResult : class
            {
                var hasNonce = _nonce != null;
                HttpResponseMessage response;

                do
                {
                    var nonceHeader = new AcmeHeader
                    {
                        Nonce = _nonce
                    };

                    var request = new HttpRequestMessage(method, uri);

                    if (message != null)
                    {
                        var encodedMessage = _jws.Encode(message, nonceHeader);
                        var json           = JsonConvert.SerializeObject(encodedMessage, jsonSettings);

                        request.Content = new StringContent(json, Encoding.UTF8, "application/json");
                    }

                    response = await _client.SendAsync(request, token).ConfigureAwait(false);

                    if (response.Headers.TryGetValues("Replay-Nonce", out var vals))
                    {
                        _nonce = vals.FirstOrDefault();
                    }
                    else
                    {
                        _nonce = null;
                    }

                    if (response.IsSuccessStatusCode || hasNonce || _nonce == null)
                    {
                        break; // either successful or no point in retry
                    }

                    hasNonce = true; // we only allow it once
                } while (true);

                if (response.Content.Headers.ContentType.MediaType == "application/problem+json")
                {
                    var problemJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    var problem = JsonConvert.DeserializeObject <Problem>(problemJson);
                    throw new AcmeException(problem, response);
                }

                if (typeof(TResult) == typeof(CertificateResponse) &&
                    response.Content.Headers.ContentType.MediaType == "application/pkix-cert")
                {
                    var certificateBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);

                    var certificateResponse = new CertificateResponse
                    {
                        Certificate = certificateBytes
                    };

                    GetHeaderValues(response, certificateResponse);

                    return(certificateResponse as TResult);
                }

                var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var responseContent = JObject.Parse(responseText).ToObject <TResult>();

                GetHeaderValues(response, responseContent);

                return(responseContent);
            }