summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/rsc/letsencrypt/vendor/github.com/xenolf/lego/acme/error.go
blob: b32561a3ac25b542fa867855317e80bed38d7a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package acme

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
)

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 {
	StatusCode int    `json:"status,omitempty"`
	Type       string `json:"type"`
	Detail     string `json:"detail"`
}

func (e RemoteError) Error() string {
	return fmt.Sprintf("acme: Error %d - %s - %s", e.StatusCode, e.Type, e.Detail)
}

// 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
}

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 {
		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)
	}

	return fmt.Sprintf("%s\nError Detail:\n%s", c.RemoteError.Error(), errStr)
}

func handleHTTPError(resp *http.Response) error {
	var errorDetail RemoteError
	decoder := json.NewDecoder(resp.Body)
	err := decoder.Decode(&errorDetail)
	if err != nil {
		return err
	}

	errorDetail.StatusCode = resp.StatusCode

	// Check for errors we handle specifically
	if errorDetail.StatusCode == http.StatusForbidden && errorDetail.Detail == tosAgreementError {
		return TOSError{errorDetail}
	}

	return errorDetail
}

func handleChallengeError(chlng challenge) error {
	return challengeError{chlng.Error, chlng.ValidationRecords}
}