2015-10-29 00:42:05 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-12-10 18:58:11 +00:00
|
|
|
"strings"
|
2015-10-29 00:42:05 +00:00
|
|
|
)
|
|
|
|
|
2015-11-02 00:01:00 +00:00
|
|
|
const (
|
|
|
|
tosAgreementError = "Must agree to subscriber agreement before any further actions"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-12-10 18:58:11 +00:00
|
|
|
type domainError struct {
|
|
|
|
Domain string
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
|
|
|
type challengeError struct {
|
|
|
|
RemoteError
|
|
|
|
records []validationRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c challengeError) Error() string {
|
|
|
|
|
|
|
|
var errStr string
|
|
|
|
for _, validation := range c.records {
|
2015-12-24 08:57:09 +00:00
|
|
|
errStr = errStr + fmt.Sprintf("\tValidation for %s:%s\n\tResolved to:\n\t\t%s\n\tUsed: %s\n\n",
|
|
|
|
validation.Hostname, validation.Port, strings.Join(validation.ResolvedAddresses, "\n\t\t"), validation.UsedAddress)
|
2015-12-10 18:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s\nError Detail:\n%s", c.RemoteError.Error(), errStr)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
// try to decode the content as JSON
|
|
|
|
if resp.Header.Get("Content-Type") == "application/json" {
|
|
|
|
decoder := json.NewDecoder(resp.Body)
|
|
|
|
err := decoder.Decode(&errorDetail)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return challengeError{chlng.Error, chlng.ValidationRecords}
|
2015-11-02 00:01:00 +00:00
|
|
|
}
|