auth

Paddy 2015-12-14 Parent:4b68bac597b7

181:b7e685839a1b Go to Latest

auth/client/client.go

Break out scopes and events. This repo has gotten unwieldy, and there are portions of it that need to be imported by a large number of other packages. For example, scopes will be used in almost every API we write. Rather than importing the entirety of this codebase into every API we write, I've opted to move the scope logic out into a scopes package, with a subpackage for the defined types, which is all most projects actually want to import. We also define some event type constants, and importing those shouldn't require a project to import all our dependencies, either. So I made an events subpackage that just holds those constants. This package has become a little bit of a red-headed stepchild and is do for a refactor, but I'm trying to put that off as long as I can. The refactoring of our scopes stuff has left a bug wherein a token can be granted for scopes that don't exist. I'm going to need to revisit that, and also how to limit scopes to only be granted to the users that should be able to request them. But that's a battle for another day.

History
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 }