auth
auth/memstore.go
Add more tests for ClientStores. Test that deleting a client that doesn't exist throws an ErrClientNotFound. Test that adding a client twice returns an ErrClientAlreadyExists. Test that clients retrieve have properties that all match the client that was stored. Remove the unused error return from the internal MemoryStore helper for getting a list of clients by their OwnerID. If a profile doesn't exist, return an empty list of clients, as we're technically returning any client that has that OwnerID. There's no guarantee that that OwnerID is actually valid.
| paddy@28 | 1 package auth |
| paddy@28 | 2 |
| paddy@31 | 3 import ( |
| paddy@31 | 4 "sync" |
| paddy@31 | 5 |
| paddy@31 | 6 "secondbit.org/uuid" |
| paddy@31 | 7 ) |
| paddy@28 | 8 |
| paddy@28 | 9 type Memstore struct { |
| paddy@28 | 10 tokens map[string]Token |
| paddy@28 | 11 refreshTokenLookup map[string]string |
| paddy@28 | 12 profileTokenLookup map[string][]string |
| paddy@28 | 13 tokenLock sync.RWMutex |
| paddy@29 | 14 |
| paddy@29 | 15 grants map[string]Grant |
| paddy@29 | 16 grantLock sync.RWMutex |
| paddy@31 | 17 |
| paddy@31 | 18 clients map[string]Client |
| paddy@31 | 19 profileClientLookup map[string][]uuid.ID |
| paddy@31 | 20 clientLock sync.RWMutex |
| paddy@28 | 21 } |
| paddy@28 | 22 |
| paddy@28 | 23 func NewMemstore() *Memstore { |
| paddy@28 | 24 return &Memstore{ |
| paddy@31 | 25 tokens: map[string]Token{}, |
| paddy@31 | 26 refreshTokenLookup: map[string]string{}, |
| paddy@31 | 27 profileTokenLookup: map[string][]string{}, |
| paddy@31 | 28 grants: map[string]Grant{}, |
| paddy@31 | 29 clients: map[string]Client{}, |
| paddy@31 | 30 profileClientLookup: map[string][]uuid.ID{}, |
| paddy@28 | 31 } |
| paddy@28 | 32 } |
| paddy@28 | 33 |
| paddy@28 | 34 func (m *Memstore) lookupTokenByRefresh(token string) (string, error) { |
| paddy@28 | 35 m.tokenLock.RLock() |
| paddy@28 | 36 defer m.tokenLock.RUnlock() |
| paddy@28 | 37 t, ok := m.refreshTokenLookup[token] |
| paddy@28 | 38 if !ok { |
| paddy@28 | 39 return "", ErrTokenNotFound |
| paddy@28 | 40 } |
| paddy@28 | 41 return t, nil |
| paddy@28 | 42 } |
| paddy@28 | 43 |
| paddy@28 | 44 func (m *Memstore) lookupTokensByProfileID(id string) ([]string, error) { |
| paddy@28 | 45 m.tokenLock.RLock() |
| paddy@28 | 46 defer m.tokenLock.RUnlock() |
| paddy@28 | 47 return m.profileTokenLookup[id], nil |
| paddy@28 | 48 } |
| paddy@31 | 49 |
| paddy@33 | 50 func (m *Memstore) lookupClientsByProfileID(id string) []uuid.ID { |
| paddy@31 | 51 m.clientLock.RLock() |
| paddy@31 | 52 defer m.clientLock.RUnlock() |
| paddy@33 | 53 c, ok := m.profileClientLookup[id] |
| paddy@33 | 54 if !ok { |
| paddy@33 | 55 return []uuid.ID{} |
| paddy@33 | 56 } |
| paddy@33 | 57 return c |
| paddy@31 | 58 } |