From 54975553c4b67d2b2d9d6edcbf18dbcbdc508fec Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 10 Dec 2015 17:01:14 +0100 Subject: Reimplement registration --- client.go | 19 +++++++++++++++++++ cmd/x/main.go | 9 +-------- messages.go | 10 +++++----- registration.go | 8 -------- resource.go | 49 ++++++++++++++++++++++++++----------------------- 5 files changed, 51 insertions(+), 44 deletions(-) delete mode 100644 registration.go diff --git a/client.go b/client.go index e4b208c..6fcdbe0 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "io/ioutil" "log" "net/http" "net/textproto" @@ -130,3 +131,21 @@ func retryAfter(r *http.Response) time.Duration { 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 +} diff --git a/cmd/x/main.go b/cmd/x/main.go index d42d139..c60b8f8 100644 --- a/cmd/x/main.go +++ b/cmd/x/main.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "log" "dim13.org/acme" @@ -17,15 +16,9 @@ func main() { if err != nil { log.Fatal(err) } - resp, err := c.Post(a, c.Dir.NewReg, acme.NewRegistration(a.Contact, acme.NewReg{})) + err = c.Register(a) if err != nil { log.Fatal(err) } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Fatal(err) - } - log.Println(string(body)) log.Println(c) } diff --git a/messages.go b/messages.go index 33f5873..c270f32 100644 --- a/messages.go +++ b/messages.go @@ -24,7 +24,7 @@ type Directory struct { // Registration Objects type Registration struct { - Resource string `json:"resource"` // new-reg + Resource Resource `json:"resource"` // new-reg Contact []Contact `json:"contact,omitempty"` Agreement string `json:"agreement,omitempty"` Authorizations string `json:"authorizations,omitempty"` @@ -49,7 +49,7 @@ type Key struct { // Authorization request type Authorization struct { - Resource string `json:"resource"` // new-authz + Resource Resource `json:"resource"` // new-authz Identifier Identifier `json:"identifier"` } @@ -69,7 +69,7 @@ type Identifier struct { } // Challege ... -type Challege struct { +type Challenge struct { Type string `json:"type"` // http-01 Status Status `json:"status"` // e.g. valid Validated string `json:"validated"` // 2006-01-02T15:04Z @@ -96,8 +96,8 @@ const ( StatusRevoked ) -// UnmarshalJSON implemets json interface for status decoding -func (s *Status) UnmarshalJSON(b []byte) error { +// UnmarshalText implemets json interface for status decoding +func (s *Status) UnmarshalText(b []byte) error { var status = map[string]Status{ "unknown": StatusUnknown, "pending": StatusPending, diff --git a/registration.go b/registration.go deleted file mode 100644 index da492f8..0000000 --- a/registration.go +++ /dev/null @@ -1,8 +0,0 @@ -package acme - -func NewRegistration(contact []Contact, r ResourceValue) Registration { - return Registration{ - Resource: r.Value(), - Contact: contact, - } -} diff --git a/resource.go b/resource.go index b38cbca..ab54a94 100644 --- a/resource.go +++ b/resource.go @@ -1,28 +1,31 @@ package acme -type ResourceValue interface { - Value() string -} +type Resource int -type NewReg struct { - Contact []string `json:"contact"` -} +const ( + ResNewReg Resource = iota + ResRecoverReg + ResNewAuthz + ResNewCert + ResRevoceCert + ResRegister + ResAuthz + ResChallenge + ResCert +) -type RecoverReg struct{} -type NewAuthz struct{} -type NewCert struct{} -type RevokeCert struct{} -type Register struct{} -type Authz struct{} -type Challenge struct{} -type Cert struct{} +var resources = map[Resource]string{ + ResNewReg: "new-reg", + ResRecoverReg: "recover-reg", + ResNewAuthz: "new-authz", + ResNewCert: "new-cert", + ResRevoceCert: "revoke-cert", + ResRegister: "reg", + ResAuthz: "authz", + ResChallenge: "challenge", + ResCert: "cert", +} -func (NewReg) Value() string { return "new-reg" } -func (RecoverReg) Value() string { return "recover-reg" } -func (NewAuthz) Value() string { return "new-authz" } -func (NewCert) Value() string { return "new-cert" } -func (RevokeCert) Value() string { return "revoke-cert" } -func (Register) Value() string { return "reg" } -func (Authz) Value() string { return "authz" } -func (Challenge) Value() string { return "challenge" } -func (Cert) Value() string { return "cert" } +func (r Resource) MarshalText() ([]byte, error) { + return []byte(resources[r]), nil +} -- cgit v1.2.3