2015-10-29 00:42:05 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
2018-05-30 17:53:04 +00:00
|
|
|
"bytes"
|
2015-10-29 00:42:05 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-04-15 01:09:29 +00:00
|
|
|
"io/ioutil"
|
2015-10-29 00:42:05 +00:00
|
|
|
"net/http"
|
2015-12-10 18:58:11 +00:00
|
|
|
"strings"
|
2015-10-29 00:42:05 +00:00
|
|
|
)
|
|
|
|
|
2017-02-19 04:12:14 +00:00
|
|
|
const (
|
2018-05-30 17:53:04 +00:00
|
|
|
tosAgreementError = "Terms of service have changed"
|
|
|
|
invalidNonceError = "urn:ietf:params:acme:error:badNonce"
|
2017-02-19 04:12:14 +00:00
|
|
|
)
|
2015-11-02 00:01:00 +00:00
|
|
|
|
|
|
|
// RemoteError is the base type for all errors specific to the ACME protocol.
|
|
|
|
type RemoteError struct {
|
2015-10-29 00:42:05 +00:00
|
|
|
StatusCode int `json:"status,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Detail string `json:"detail"`
|
|
|
|
}
|
|
|
|
|
2015-11-02 00:01:00 +00:00
|
|
|
func (e RemoteError) Error() string {
|
2015-11-07 06:22:32 +00:00
|
|
|
return fmt.Sprintf("acme: Error %d - %s - %s", e.StatusCode, e.Type, e.Detail)
|
2015-10-29 00:42:05 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 00:01:00 +00:00
|
|
|
// TOSError represents the error which is returned if the user needs to
|
|
|
|
// accept the TOS.
|
|
|
|
// TODO: include the new TOS url if we can somehow obtain it.
|
|
|
|
type TOSError struct {
|
|
|
|
RemoteError
|
|
|
|
}
|
|
|
|
|
2017-02-19 04:12:14 +00:00
|
|
|
// NonceError represents the error which is returned if the
|
|
|
|
// nonce sent by the client was not accepted by the server.
|
|
|
|
type NonceError struct {
|
|
|
|
RemoteError
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:58:11 +00:00
|
|
|
type domainError struct {
|
|
|
|
Domain string
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// ObtainError is returned when there are specific errors available
|
|
|
|
// per domain. For example in ObtainCertificate
|
|
|
|
type ObtainError map[string]error
|
2015-12-10 18:58:11 +00:00
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
func (e ObtainError) Error() string {
|
|
|
|
buffer := bytes.NewBufferString("acme: Error -> One or more domains had a problem:\n")
|
|
|
|
for dom, err := range e {
|
|
|
|
buffer.WriteString(fmt.Sprintf("[%s] %s\n", dom, err))
|
2015-12-10 18:58:11 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
return buffer.String()
|
2015-12-10 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 00:42:05 +00:00
|
|
|
func handleHTTPError(resp *http.Response) error {
|
2015-11-02 00:01:00 +00:00
|
|
|
var errorDetail RemoteError
|
2016-04-04 21:15:49 +00:00
|
|
|
|
2017-01-15 15:54:49 +00:00
|
|
|
contentType := resp.Header.Get("Content-Type")
|
2018-05-30 17:53:04 +00:00
|
|
|
if contentType == "application/json" || strings.HasPrefix(contentType, "application/problem+json") {
|
2017-01-15 15:54:49 +00:00
|
|
|
err := json.NewDecoder(resp.Body).Decode(&errorDetail)
|
2016-04-04 21:15:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-15 01:09:29 +00:00
|
|
|
} else {
|
2017-01-15 15:54:49 +00:00
|
|
|
detailBytes, err := ioutil.ReadAll(limitReader(resp.Body, maxBodySize))
|
2016-04-15 01:09:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
errorDetail.Detail = string(detailBytes)
|
2015-10-29 00:42:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errorDetail.StatusCode = resp.StatusCode
|
2015-11-02 00:01:00 +00:00
|
|
|
|
|
|
|
// Check for errors we handle specifically
|
|
|
|
if errorDetail.StatusCode == http.StatusForbidden && errorDetail.Detail == tosAgreementError {
|
|
|
|
return TOSError{errorDetail}
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
if errorDetail.StatusCode == http.StatusBadRequest && errorDetail.Type == invalidNonceError {
|
2017-02-19 04:12:14 +00:00
|
|
|
return NonceError{errorDetail}
|
|
|
|
}
|
|
|
|
|
2015-10-29 00:42:05 +00:00
|
|
|
return errorDetail
|
|
|
|
}
|
2015-11-02 00:01:00 +00:00
|
|
|
|
2015-12-10 18:58:11 +00:00
|
|
|
func handleChallengeError(chlng challenge) error {
|
2018-05-30 17:53:04 +00:00
|
|
|
return chlng.Error
|
2015-11-02 00:01:00 +00:00
|
|
|
}
|