aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-22 02:54:49 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-22 02:54:49 +0100
commit366b6f8736986b663c040744daec2a908a843365 (patch)
tree26c42144858c35d59d4eb37f45618cdc42be4f3c
parentc419e07befe8672085fe35f5b4a0edc6a8c4d244 (diff)
wip
-rw-r--r--client.go50
1 files changed, 34 insertions, 16 deletions
diff --git a/client.go b/client.go
index d207ebb..1b07095 100644
--- a/client.go
+++ b/client.go
@@ -10,6 +10,8 @@ import (
"regexp"
"strconv"
"time"
+
+ "github.com/mgutz/ansi"
)
type Links map[string]string
@@ -75,21 +77,21 @@ func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, erro
if err != nil {
return nil, err
}
- log.Println("POST", string(body))
+ log.Println(ansi.Color("POST", "red+b"), string(body))
signed, err := s.Sign(body, c)
if err != nil {
return nil, err
}
- resp, err := http.Post(uri, "application/jose+json", signed)
+ resp, err := http.Post(uri, "application/json", signed)
if err != nil {
return nil, err
}
defer resp.Body.Close()
c.parseHeader(resp)
- log.Println("STATUS", resp.Status)
- log.Println("HEADER", c)
+ log.Println(ansi.Color("STATUS", "yellow"), resp.Status)
+ log.Println(ansi.Color("HEADER", "green"), c)
if resp.StatusCode >= http.StatusBadRequest {
var p Problem
@@ -108,7 +110,7 @@ func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, erro
if err != nil {
return resp, err
}
- log.Println("RESPONSE", string(body))
+ log.Println(ansi.Color("RESP", "red"), string(body))
return resp, json.Unmarshal(body, v)
//return json.NewDecoder(resp.Body).Decode(v)
}
@@ -168,7 +170,7 @@ func (c *Client) Register(a *Account) error {
switch resp.StatusCode {
case http.StatusConflict:
// Query Location
- r.Resource = ResRegister
+ r = &Registration{Resource: ResRegister}
_, err = c.post(c.Location, a, r)
if err != nil {
return err
@@ -177,8 +179,11 @@ func (c *Client) Register(a *Account) error {
case http.StatusCreated:
// Agree to TOS
if tos := c.Link["terms-of-service"]; tos != "" {
- r.Resource = ResRegister
- r.Agreement = tos
+ r = &Registration{
+ Resource: ResRegister,
+ Contact: a.Contact,
+ Agreement: tos,
+ }
_, err = c.post(c.Location, a, r)
}
}
@@ -186,15 +191,28 @@ func (c *Client) Register(a *Account) error {
}
func (c *Client) Authorize(a *Account, domain []string) error {
+ ident := Identifier{
+ Type: IdentDNS,
+ Value: domain[0],
+ }
r := &Authorization{
- Resource: ResNewAuthz,
- Identifier: Identifier{
- Type: IdentDNS,
- Value: domain[0],
- },
- }
- _, err := c.post(c.Dir.NewAuthz, a, r)
- log.Println(r)
+ Resource: ResNewAuthz,
+ Identifier: ident,
+ }
+ resp, err := c.post(c.Dir.NewAuthz, a, r)
+ switch resp.StatusCode {
+ case http.StatusCreated:
+ for _, ch := range r.Challenges {
+ if ch.Type == ChallengeHTTP {
+ ans := &Challenge{
+ Resource: ResChallenge,
+ Type: ch.Type,
+ Token: ch.Token,
+ }
+ _, err = c.post(ch.URI, a, ans)
+ }
+ }
+ }
return err
}