aboutsummaryrefslogtreecommitdiff
path: root/client.go
blob: 6fcdbe0b64c580c23893d43b6212c7e4a93cc270 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package acme

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

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

// Solver decribes a solving interface
type Solver interface {
	Solve()
}

// Links to the next stage
type Links map[string]string

// Client ...
type Client struct {
	Dir   Directory
	nonce chan string
	Links Links
}

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

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

// 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
	}
	log.Println(string(body))

	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)
	c.Links = links(resp)

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

	return resp, nil
}

func links(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")
}

func (c *Client) Register(a *Account) error {
	r := Registration{
		Resource: ResNewReg,
		Contact:  a.Contact,
	}
	resp, err := c.Post(a, c.Dir.NewReg, r)
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return err
	}
	log.Println(string(body))
	return nil
}