aboutsummaryrefslogtreecommitdiff
path: root/client.go
blob: b144e6f5d2dff0afdc163299116b57bb562588ea (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package acme

import (
	"bytes"
	"encoding/json"
	"errors"
	"log"
	"net/http"
	"net/textproto"
	"regexp"
	"time"

	"github.com/square/go-jose"
)

type Solver interface {
	Solve()
}

type Signer interface {
	Sign([]byte, jose.NonceSource) ([]byte, error)
}

type Client struct {
	Dir   Directory
	nonce chan string
}

// NewClient fetches directory and initializes nonce
func NewClient(uri string) (*Client, error) {
	resp, err := http.Get(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	c := &Client{nonce: make(chan string, 10)}
	c.nonce <- replyNonce(resp)
	err = json.NewDecoder(resp.Body).Decode(&c.Dir)
	if err != nil {
		return nil, err
	}
	return c, nil
}

var errNoNonces = errors.New("No nonces available")

// Nonce implements jose nonce provider
func (c Client) Nonce() (string, error) {
	select {
	case nonce := <-c.nonce:
		return nonce, nil
	default:
		return "", errNoNonces
	}
}

// Important header fields
//
// Replay-Nonce		each response, required for next request
// Link			links to next stage
// Retry-After		polling interval

// Action		Request		Response
//
// Register		POST new-reg	201 -> reg
// Request challenges	POST new-authz	201 -> authz
// Answer challenges	POST challenge	200
// Poll for status	GET  authz	200
// Request issuance	POST new-cert	201 -> cert
// Check for new cert	GET  cert	200

// Post is used for
// new-reg, new-authz, challenge, new-cert
func (c Client) Post(s Signer, uri string, v interface{}) (*http.Response, error) {
	body, err := json.Marshal(v)
	if err != nil {
		return nil, err
	}

	signed, err := s.Sign(body, c)
	if err != nil {
		return nil, err
	}

	resp, err := http.Post(uri, "application/jose+json",
		bytes.NewReader(signed))
	if err != nil {
		return nil, err
	}

	c.nonce <- replyNonce(resp)

	if resp.StatusCode >= http.StatusBadRequest {
		return nil, handleError(resp)
	}

	log.Printf("%+v\n", link(resp))

	return resp, nil
}

type Links map[string]string

func link(r *http.Response) Links {
	links := make(Links)
	key := textproto.CanonicalMIMEHeaderKey("Link")
	reg := regexp.MustCompile(`^<(.*)>;rel="(.*)"`)
	for _, l := range r.Header[key] {
		re := reg.FindStringSubmatch(l)
		if len(re) == 3 {
			links[re[2]] = re[1]
		}
	}
	return links
}

func retryAfter(r *http.Response) time.Duration {
	ra := r.Header.Get("Retry-After")
	if d, err := time.ParseDuration(ra + "s"); err == nil {
		return d
	}
	return time.Second
}

func replyNonce(r *http.Response) string {
	return r.Header.Get("Replay-Nonce")
}