コード例 #1
0
        public void CanCreateExceptionWithInnerException()
        {
            var inner = new AcmeException();
            var ex    = new CertesCliException("certes", inner);

            Assert.Equal("certes", ex.Message);
            Assert.Equal(inner, ex.InnerException);
        }
コード例 #2
0
        /// <summary>
        /// Updates existing Account information registered with the ACME CA.
        /// </summary>
        /// <see cref="https://tools.ietf.org/html/draft-ietf-acme-acme-12#section-7.3.2"/>
        /// <see cref="https://tools.ietf.org/html/draft-ietf-acme-acme-12#section-7.3.3"/>
        /// <see cref="https://tools.ietf.org/html/draft-ietf-acme-acme-12#section-7.3.4"/>
        public async Task <AcmeResponse <Protocol.Account> > AccountUpdateAsync(Protocol.Messages.NewAccount account)
        {
            Logger.Info("Updating an ACME account. Params:{@params}", account);

            var response = await Request(GetType(typeof(Protocol.Account)), Directory.NewAccount,
                                         new RequestParams
            {
                Method  = HttpMethod.Post,
                Payload = account
            });

            if (string.IsNullOrEmpty(response.Headers.Location))
            {
                var ex = new AcmeException(Protocol.ErrorType.IncorrectResponse, "Account updating response does not include Location header.");

                Logger.Error(ex, $"{nameof(AcmeClient)} request error.");

                throw ex;
            }

            Location = response.Headers.Location;

            return(response);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: vincent-deng/acme.net
 private static void PrintError(AcmeException ex)
 {
     Log.Error("error:");
     Log.Error(ex.Problem.Type);
     Log.Error(ex.Problem.Detail);
 }
コード例 #4
0
        protected async Task <AcmeResponse> Request(
            string url,
            RequestParams @params = null
            )
        {
            @params = @params ?? new RequestParams();

            string content = null;

            var request = new HttpRequestMessage(@params.Method, url);

            if (@params.Payload != null)
            {
                var jws = new JsonWebSignature
                {
                    Payload = "",
                };

                jws.SetProtected(new JsonWebSignatureProtected
                {
                    Algorithm = AlgorithmsEnum.RS256,
                    Nonce     = Nonce,
                    Url       = url,
                    KeyID     = @params.IncludePublicKey ? null : Location,
                    Key       = @params.IncludePublicKey ? new JsonWebKey(Key) : null
                });

                if (!(@params.Payload is string))
                {
                    jws.SetPayload(@params.Payload);
                }

                jws.Sign(Key);

                content = JsonConvert.SerializeObject(jws, Formatting.Indented);
            }

            if (!string.IsNullOrEmpty(content))
            {
                request.Content = new StringContent(content);
                request.Content.Headers.ContentType = MediaTypeHeader.JsonContentTypeHeaderValue;
            }

            // Copy headers to HTTP request from ACME request
            foreach (var key in @params.Headers.AllKeys)
            {
                request.Headers.Add(key, @params.Headers.Get(key));
            }

            Logger.Debug("Request {method} {path} {token}", @params.Method, url, @params.Headers.ToString(), content);

            var response = _http.SendAsync(request).Result;

            // Get Replay-Nonce from response
            IEnumerable <string> nonceList;

            response.Headers.TryGetValues(HeaderCollection.ReplayNonceHeader, out nonceList);
            Nonce = nonceList?.FirstOrDefault();

            try
            {
                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException)
            {
                string         message   = null;
                string         errorJson = null;
                Protocol.Error error     = null;
                try
                {
                    errorJson = await response.Content.ReadAsStringAsync();

                    try
                    {
                        error = JsonConvert.DeserializeObject <Protocol.Error>(errorJson);

                        message = $"{error.Type}: {error.Detail}";
                        Logger.Debug("ACME Error {@error}", error);
                    }
                    catch
                    {
                        Logger.Error("Cannot parse ACME Error from Client response");
                        Logger.Debug(errorJson);
                    }
                }
                catch (System.Exception e)
                {
                    Logger.Error("Cannot parse content from Client response");
                    Logger.Debug(e);
                }

                AcmeException ex;
                if (string.IsNullOrEmpty(message))
                {
                    message = $"Unexpected response status code [{response.StatusCode}] for [{@params.Method}] request to [{url}]";
                    ex      = new AcmeException(Protocol.ErrorType.ServerInternal, message, response.StatusCode);
                }
                else
                {
                    ex = new AcmeException(error.Type, error.Detail, response.StatusCode);
                }

                Logger.Error(ex);

                throw ex;
            }

            // Copy headers to ACME Response
            var headers = new HeaderCollection();

            foreach (var header in response.Headers)
            {
                headers.Add(header.Key, string.Join(", ", header.Value));
            }

            var acmeResponse = new AcmeResponse
            {
                StatusCode = (int)response.StatusCode,
                Headers    = headers,
                Content    = response.Content == null
                    ? new MediaTypeContent("", new byte[0])
                    : new MediaTypeContent(
                    response.Content.Headers.ContentType?.MediaType ?? "",
                    await response.Content.ReadAsByteArrayAsync()),
            };

            Logger.Debug("Response {@response}", acmeResponse);

            return(acmeResponse);
        }