auth
2015-07-18
Parent:b0d1b3e39fc8
auth/client/client.go
Update client to detect errors. The client doesn't treat non-200 responses as errors automatically, so we need to detect when the response.Errors property is set, and use that to return an error. To avoid the boilerplate and an extensive error system, I just wrapped them in an httpErrors type that implements the error interface. That way the errors can be returned, and callers can type-cast and interrogate them. I also updated the GetLogin function to return an auth.ErrLoginNotFound error when the httpErrors response indicates that's the reason the request failed.
1 package client
3 import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io"
9 "net/http"
10 "strings"
11 "time"
13 "code.secondbit.org/auth.hg"
14 "code.secondbit.org/uuid.hg"
15 )
17 var (
18 ErrNilClient = errors.New("nil client wrapper")
19 ErrNilHTTPClient = errors.New("nil client")
20 )
22 type Client struct {
23 client *http.Client
24 address string
25 ID uuid.ID
26 }
28 func New(address string, id uuid.ID) *Client {
29 address = strings.TrimRight(address, "/")
30 return &Client{
31 address: address,
32 client: &http.Client{},
33 ID: id,
34 }
35 }
37 func (c *Client) do(method, url string, request interface{}, scopes []string, subject uuid.ID) (auth.Response, error) {
38 if c == nil {
39 return auth.Response{}, ErrNilClient
40 }
41 if c.client == nil {
42 return auth.Response{}, ErrNilHTTPClient
43 }
44 var response auth.Response
45 if !strings.HasPrefix(url, "http") {
46 url = strings.TrimLeft(url, "/")
47 url = "/" + url
48 url = c.address + url
49 }
50 var body io.Reader
51 if request != nil {
52 data, err := json.Marshal(request)
53 if err != nil {
54 return response, err
55 }
56 body = bytes.NewBuffer(data)
57 }
58 req, err := http.NewRequest(method, url, body)
59 if err != nil {
60 return response, err
61 }
62 req.Header.Set("Ducky-Scope", strings.Join(scopes, " "))
63 req.Header.Set("Ducky-Issuer", c.ID.String())
64 if subject != nil {
65 req.Header.Set("Ducky-Subject", subject.String())
66 }
67 req.Header.Set("Ducky-Expires", time.Now().Add(time.Hour).String())
68 req.Header.Set("Ducky-Issued-At", time.Now().String())
69 req.Header.Set("Ducky-Not-Before", time.Now().Add(-5*time.Minute).String())
70 resp, err := c.client.Do(req)
71 if err != nil {
72 return response, err
73 }
74 defer resp.Body.Close()
75 switch resp.Header.Get("Content-Type") {
76 case "application/json":
77 dec := json.NewDecoder(resp.Body)
78 err = dec.Decode(&response)
79 if err != nil {
80 return response, err
81 }
82 default:
83 dec := json.NewDecoder(resp.Body)
84 err = dec.Decode(&response)
85 if err != nil {
86 return response, err
87 }
88 }
89 if len(response.Errors) > 0 {
90 return response, httpErrors(response.Errors)
91 }
92 return response, nil
93 }
95 type httpErrors []auth.RequestError
97 func (h httpErrors) Error() string {
98 return fmt.Sprintf("%+#v", h)
99 }
101 func (c *Client) Get(url string, scopes []string, subject uuid.ID) (auth.Response, error) {
102 return c.do("GET", url, nil, scopes, subject)
103 }