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.
13 "code.secondbit.org/auth.hg"
14 "code.secondbit.org/uuid.hg"
18 ErrNilClient = errors.New("nil client wrapper")
19 ErrNilHTTPClient = errors.New("nil client")
28 func New(address string, id uuid.ID) *Client {
29 address = strings.TrimRight(address, "/")
32 client: &http.Client{},
37 func (c *Client) do(method, url string, request interface{}, scopes []string, subject uuid.ID) (auth.Response, error) {
39 return auth.Response{}, ErrNilClient
42 return auth.Response{}, ErrNilHTTPClient
44 var response auth.Response
45 if !strings.HasPrefix(url, "http") {
46 url = strings.TrimLeft(url, "/")
52 data, err := json.Marshal(request)
56 body = bytes.NewBuffer(data)
58 req, err := http.NewRequest(method, url, body)
62 req.Header.Set("Ducky-Scope", strings.Join(scopes, " "))
63 req.Header.Set("Ducky-Issuer", c.ID.String())
65 req.Header.Set("Ducky-Subject", subject.String())
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)
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)
83 dec := json.NewDecoder(resp.Body)
84 err = dec.Decode(&response)
89 if len(response.Errors) > 0 {
90 return response, httpErrors(response.Errors)
95 type httpErrors []auth.RequestError
97 func (h httpErrors) Error() string {
98 return fmt.Sprintf("%+#v", h)
101 func (c *Client) Get(url string, scopes []string, subject uuid.ID) (auth.Response, error) {
102 return c.do("GET", url, nil, scopes, subject)