aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-10 17:01:14 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-10 17:01:14 +0100
commit54975553c4b67d2b2d9d6edcbf18dbcbdc508fec (patch)
tree368e07db912904c7982358ad60e76a35404121b5
parentf8176a30d5193f77fd7a76492f8245620035ffb7 (diff)
Reimplement registration
-rw-r--r--client.go19
-rw-r--r--cmd/x/main.go9
-rw-r--r--messages.go10
-rw-r--r--registration.go8
-rw-r--r--resource.go49
5 files changed, 51 insertions, 44 deletions
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
+}