auth
auth/memstore.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 auth
3 import (
4 "sync"
6 "code.secondbit.org/uuid.hg"
7 )
9 type memstore struct {
10 tokens map[string]Token
11 refreshTokenLookup map[string]string
12 profileTokenLookup map[string][]string
13 tokenLock sync.RWMutex
15 authCodes map[string]AuthorizationCode
16 authCodeLock sync.RWMutex
18 clients map[string]Client
19 profileClientLookup map[string][]uuid.ID
20 clientLock sync.RWMutex
22 endpoints map[string][]Endpoint
23 endpointLock sync.RWMutex
25 profiles map[string]Profile
26 profileLock sync.RWMutex
28 logins map[string]Login
29 profileLoginLookup map[string][]string
30 loginLock sync.RWMutex
32 sessions map[string]Session
33 sessionLock sync.RWMutex
35 scopes map[string]Scope
36 scopeLock sync.RWMutex
37 }
39 // NewMemstore returns an in-memory version of our datastores,
40 // which is handy for tests. Though the implementation is concurrency-safe,
41 // if makes no attempt to persist the data, and therefore it is inadvisable
42 // to use it in a production setting.
43 func NewMemstore() *memstore {
44 return &memstore{
45 tokens: map[string]Token{},
46 refreshTokenLookup: map[string]string{},
47 profileTokenLookup: map[string][]string{},
48 authCodes: map[string]AuthorizationCode{},
49 clients: map[string]Client{},
50 profileClientLookup: map[string][]uuid.ID{},
51 endpoints: map[string][]Endpoint{},
52 profiles: map[string]Profile{},
53 logins: map[string]Login{},
54 profileLoginLookup: map[string][]string{},
55 sessions: map[string]Session{},
56 scopes: map[string]Scope{},
57 }
58 }
60 func (m *memstore) lookupTokenByRefresh(token string) (string, error) {
61 m.tokenLock.RLock()
62 defer m.tokenLock.RUnlock()
63 t, ok := m.refreshTokenLookup[token]
64 if !ok {
65 return "", ErrTokenNotFound
66 }
67 return t, nil
68 }
70 func (m *memstore) lookupTokensByProfileID(id string) ([]string, error) {
71 m.tokenLock.RLock()
72 defer m.tokenLock.RUnlock()
73 return m.profileTokenLookup[id], nil
74 }
76 func (m *memstore) lookupClientsByProfileID(id string) []uuid.ID {
77 m.clientLock.RLock()
78 defer m.clientLock.RUnlock()
79 c, ok := m.profileClientLookup[id]
80 if !ok {
81 return []uuid.ID{}
82 }
83 return c
84 }