aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--account.go49
-rw-r--r--client.go9
2 files changed, 25 insertions, 33 deletions
diff --git a/account.go b/account.go
index e0d24b0..68dae50 100644
--- a/account.go
+++ b/account.go
@@ -2,6 +2,7 @@ package acme
import (
"crypto/rsa"
+ "errors"
"io"
"net/mail"
"strings"
@@ -20,52 +21,46 @@ type Account struct {
nonce chan string
}
-func newMail(email string) (Mail, error) {
- m, err := mail.ParseAddress(email)
- if err != nil {
- return "", err
- }
- mm := Mail(m.Address)
- return mm, nil
-}
-
-func newPhone(phone string) (Phone, error) {
- p := strings.TrimSpace(phone)
- return Phone(p), nil
-}
-
// NewAccount ...
func NewAccount(key *rsa.PrivateKey) (*Account, error) {
return &Account{PrivKey: key}, nil
}
-func (a *Account) AddMail(mail string) error {
- if m, _ := newMail(mail); m != "" {
- a.Contact = append(a.Contact, m)
+func (a *Account) AddMail(email string) error {
+ m, err := mail.ParseAddress(email)
+ if err != nil {
+ return err
}
+ a.Contact = append(a.Contact, Mail(m.Address))
return nil
}
func (a *Account) AddPhone(phone string) error {
- if ph, _ := newPhone(phone); ph != "" {
- a.Contact = append(a.Contact, ph)
+ if ph := strings.TrimSpace(phone); ph != "" {
+ a.Contact = append(a.Contact, Phone(ph))
}
return nil
}
// Signer describes a signing interface
type Signer interface {
- Sign([]byte, jose.NonceSource) (io.Reader, error)
+ Init(jose.NonceSource) error
+ Sign([]byte) (io.Reader, error)
+}
+
+func (a *Account) Init(n jose.NonceSource) error {
+ var err error
+ a.signer, err = jose.NewSigner(jose.RS256, a.PrivKey)
+ if err != nil {
+ return err
+ }
+ a.signer.SetNonceSource(n)
+ return nil
}
-func (a *Account) Sign(msg []byte, n jose.NonceSource) (io.Reader, error) {
+func (a *Account) Sign(msg []byte) (io.Reader, error) {
if a.signer == nil {
- var err error
- a.signer, err = jose.NewSigner(jose.RS256, a.PrivKey)
- if err != nil {
- return nil, err
- }
- a.signer.SetNonceSource(n)
+ return nil, errors.New("init first")
}
obj, err := a.signer.Sign(msg)
if err != nil {
diff --git a/client.go b/client.go
index 50ed236..f8c5aab 100644
--- a/client.go
+++ b/client.go
@@ -43,11 +43,7 @@ func NewClient(directory string) (*Client, error) {
}
defer resp.Body.Close()
defer c.replyNonce(resp)
- err = json.NewDecoder(resp.Body).Decode(&c.Directory)
- if err != nil {
- return nil, err
- }
- return c, nil
+ return c, json.NewDecoder(resp.Body).Decode(&c.Directory)
}
var errNoNonces = errors.New("out of nonces")
@@ -88,7 +84,8 @@ func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, erro
}
log.Println(ansi.Color("POST", "red+b"), uri, string(body))
- signed, err := s.Sign(body, c)
+ s.Init(c)
+ signed, err := s.Sign(body)
if err != nil {
return nil, err
}