aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--client.go46
-rw-r--r--cmd/x/main.go7
-rw-r--r--contact.go1
-rw-r--r--messages.go49
4 files changed, 53 insertions, 50 deletions
diff --git a/client.go b/client.go
index e2d8492..a97279b 100644
--- a/client.go
+++ b/client.go
@@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"errors"
+ "fmt"
+ "io/ioutil"
"log"
"net/http"
"net/textproto"
@@ -68,8 +70,8 @@ func (c Client) Nonce() (string, error) {
// request is used for
// new-reg, new-authz, challenge, new-cert
-func (c *Client) request(url string, s Signer, rq, re interface{}) error {
- body, err := json.Marshal(rq)
+func (c *Client) post(url string, s Signer, v interface{}) error {
+ body, err := json.Marshal(v)
if err != nil {
return err
}
@@ -95,7 +97,14 @@ func (c *Client) request(url string, s Signer, rq, re interface{}) error {
c.Link = links(resp)
c.Location = location(resp)
- return json.NewDecoder(resp.Body).Decode(re)
+ body, err = ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return err
+ }
+ // DEBUG
+ log.Println("RESPONSE", string(body))
+ return json.Unmarshal(body, v)
+ //return json.NewDecoder(resp.Body).Decode(v)
}
func location(r *http.Response) string {
@@ -146,37 +155,38 @@ func replyNonce(r *http.Response) string {
challenge cert-chain
*/
-func (c *Client) Register(a *Account) (RegistrationResp, error) {
- rq := Registration{
+func (c *Client) Register(a *Account) (*Registration, error) {
+ r := &Registration{
Resource: ResNewReg,
Contact: a.Contact,
}
- re := RegistrationResp{}
- err := c.request(c.Dir.NewReg, a, rq, &re)
- return re, err
+ err := c.post(c.Dir.NewReg, a, r)
+ return r, err
}
// Agree to TOS
-func (c *Client) Agree(a *Account) (RegistrationResp, error) {
- rq := Registration{
+func (c *Client) Agree(a *Account) (*Registration, error) {
+ r := &Registration{
Resource: ResRegister,
Contact: a.Contact,
Agreement: c.Link["terms-of-service"],
}
- re := RegistrationResp{}
- err := c.request(c.Location, a, rq, &re)
- return re, err
+ err := c.post(c.Location, a, r)
+ return r, err
}
-func (c *Client) Authorize(a *Account, domain string) (AuthorizationResp, error) {
- rq := Authorization{
+func (c *Client) Authorize(a *Account, domain string) (*Authorization, error) {
+ r := &Authorization{
Resource: ResNewAuthz,
Identifier: Identifier{
Type: IdentDNS,
Value: domain,
},
}
- re := AuthorizationResp{}
- err := c.request(c.Dir.NewAuthz, a, rq, &re)
- return re, err
+ err := c.post(c.Dir.NewAuthz, a, r)
+ return r, err
+}
+
+func (c Client) String() string {
+ return fmt.Sprintf("Link: %v, Location: %v", c.Link, c.Location)
}
diff --git a/cmd/x/main.go b/cmd/x/main.go
index 1d7a813..89fc3d6 100644
--- a/cmd/x/main.go
+++ b/cmd/x/main.go
@@ -24,7 +24,6 @@ func main() {
if err != nil {
log.Fatal(err)
}
- log.Printf("%+v\n", c)
err = a.SaveAccount(".acme/account.json")
if err != nil {
@@ -35,20 +34,20 @@ func main() {
if err != nil {
log.Fatal(err)
}
- log.Printf("%+v\n", c)
+ log.Println(c)
log.Printf("%+v\n", re)
re, err = c.Agree(a)
if err != nil {
log.Fatal(err)
}
- log.Printf("%+v\n", c)
+ log.Println(c)
log.Printf("%+v\n", re)
ar, err := c.Authorize(a, "example.com")
if err != nil {
log.Fatal(err)
}
- log.Printf("%+v\n", c)
+ log.Println(c)
log.Printf("%+v\n", ar)
}
diff --git a/contact.go b/contact.go
index dcd5826..8b0b054 100644
--- a/contact.go
+++ b/contact.go
@@ -33,6 +33,7 @@ func (c *Contacts) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}
+ *c = nil // invalidate old value
for _, s := range tmp {
switch {
case strings.HasPrefix(s, "mailto:"):
diff --git a/messages.go b/messages.go
index c4bf8e5..444e4d8 100644
--- a/messages.go
+++ b/messages.go
@@ -24,20 +24,15 @@ type Directory struct {
// Registration Objects
type Registration struct {
- Resource Resource `json:"resource"` // new-reg
- Contact Contacts `json:"contact,omitempty"`
- Agreement string `json:"agreement,omitempty"`
- Authorizations string `json:"authorizations,omitempty"`
- Certificates string `json:"certificates,omitempty"`
-}
-
-// RegistrationResp ...
-type RegistrationResp struct {
- ID int `json:"id"`
- Key Key `json:"key"`
- Contact Contacts `json:"contact"`
- InitialIP net.IP `json:"initialIp"` // not in draft
- CreatedAt time.Time `json:"createdAt"` // 2006-01-02T15:04:05.999999999Z
+ Resource Resource `json:"resource"` // new-reg
+ Contact Contacts `json:"contact,omitempty"`
+ Agreement string `json:"agreement,omitempty"`
+ Authorizations string `json:"authorizations,omitempty"`
+ Certificates string `json:"certificates,omitempty"`
+ ID int `json:"id,omitempty"`
+ Key *Key `json:"key,omitempty"`
+ InitialIP net.IP `json:"initialIp,omitempty"` // not in draft
+ CreatedAt *time.Time `json:"createdAt,omitempty"` // 2006-01-02T15:04:05.999999999Z
}
// Key contains public part of jose.JsonWebKey
@@ -49,17 +44,12 @@ type Key struct {
// Authorization request
type Authorization struct {
- Resource Resource `json:"resource"` // new-authz
- Identifier Identifier `json:"identifier"`
-}
-
-// AuthorizationResp Objects
-type AuthorizationResp struct {
+ Resource Resource `json:"resource"` // new-authz
Identifier Identifier `json:"identifier"`
- Status Status `json:"status,omitemtpy"` // e.g. valid
- Expires time.Time `json:"expires,omitempty"` // 2006-01-02
- Challenges []Challenge `json:"challenges"`
- Combinations [][]int `json:"combinations,omitemtpy"`
+ Status Status `json:"status,omitempty"` // e.g. valid
+ Expires *time.Time `json:"expires,omitempty"` // 2006-01-02
+ Challenges []Challenge `json:"challenges,omitempty"`
+ Combinations [][]int `json:"combinations,omitempty"`
}
// Identifier ...
@@ -70,10 +60,13 @@ type Identifier struct {
// Challege ...
type Challenge struct {
- Type ChallengeType `json:"type"` // http-01
- Status Status `json:"status"` // e.g. valid
- Validated time.Time `json:"validated"` // 2006-01-02T15:04Z
- KeyAuthorization string `json:"keyAuthorization"`
+ Resource Resource `json:"resource"` // challenge
+ Type ChallengeType `json:"type"`
+ Token string `json:"token"`
+ Status Status `json:"status,omitempty"` // e.g. valid
+ URI string `json:"uri,omitempty"`
+ Validated *time.Time `json:"validated,omitempty"` // 2006-01-02T15:04Z
+ KeyAuthorization string `json:"keyAuthorization,omitempty"`
}
// Problem description