aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-10 13:15:55 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-10 13:15:55 +0100
commit54f6c23a9670700e8a7d14858173e19221e16550 (patch)
tree61a971cdcb551c2b666cf35519fce2692adeb18d
parentf576e74ce6891ce05882d536428f9bf9979910a2 (diff)
Move nonce into client
-rw-r--r--account.go3
-rw-r--r--client.go34
-rw-r--r--cmd/x/main.go22
3 files changed, 56 insertions, 3 deletions
diff --git a/account.go b/account.go
index a17d931..38121f6 100644
--- a/account.go
+++ b/account.go
@@ -3,7 +3,6 @@ package acme
import (
"crypto/rand"
"crypto/rsa"
- "errors"
"fmt"
"net/http"
"net/mail"
@@ -63,8 +62,6 @@ func (a *Account) ParseSigned(msg []byte) ([]byte, error) {
return obj.Verify(&a.PrivKey.PublicKey)
}
-var errNoNonces = errors.New("No nonces available")
-
// Nonce implements jose nonce provider
func (a Account) Nonce() (string, error) {
select {
diff --git a/client.go b/client.go
index 8e92127..5b178a8 100644
--- a/client.go
+++ b/client.go
@@ -3,6 +3,7 @@ package acme
import (
"bytes"
"encoding/json"
+ "errors"
"log"
"net/http"
"net/textproto"
@@ -19,6 +20,39 @@ type NonceSigner interface {
parseNonce(*http.Response)
}
+type Client struct {
+ directory Directory
+ nonce chan string
+}
+
+// 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.directory)
+ 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
+ }
+}
+
// Get is used for
// directory, authz, cert
func Get(s NonceSigner, uri string, v interface{}) error {
diff --git a/cmd/x/main.go b/cmd/x/main.go
new file mode 100644
index 0000000..51ebbc4
--- /dev/null
+++ b/cmd/x/main.go
@@ -0,0 +1,22 @@
+package main
+
+import (
+ "log"
+
+ "dim13.org/acme"
+)
+
+func main() {
+ c, err := acme.NewClient(acme.LEStaging)
+ if err != nil {
+ log.Fatal(err)
+ }
+ log.Printf("%+v\n", c)
+ for {
+ n, err := c.Nonce()
+ if err != nil {
+ break
+ }
+ log.Printf("%+v\n", n)
+ }
+}