auth

Paddy 2014-09-07 Parent:88523dab00a5 Child:1f7b44b130a0

33:607708cd8829 Go to Latest

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.

History
1 package auth
3 import (
4 "sync"
6 "secondbit.org/uuid"
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 grants map[string]Grant
16 grantLock sync.RWMutex
18 clients map[string]Client
19 profileClientLookup map[string][]uuid.ID
20 clientLock sync.RWMutex
21 }
23 func NewMemstore() *Memstore {
24 return &Memstore{
25 tokens: map[string]Token{},
26 refreshTokenLookup: map[string]string{},
27 profileTokenLookup: map[string][]string{},
28 grants: map[string]Grant{},
29 clients: map[string]Client{},
30 profileClientLookup: map[string][]uuid.ID{},
31 }
32 }
34 func (m *Memstore) lookupTokenByRefresh(token string) (string, error) {
35 m.tokenLock.RLock()
36 defer m.tokenLock.RUnlock()
37 t, ok := m.refreshTokenLookup[token]
38 if !ok {
39 return "", ErrTokenNotFound
40 }
41 return t, nil
42 }
44 func (m *Memstore) lookupTokensByProfileID(id string) ([]string, error) {
45 m.tokenLock.RLock()
46 defer m.tokenLock.RUnlock()
47 return m.profileTokenLookup[id], nil
48 }
50 func (m *Memstore) lookupClientsByProfileID(id string) []uuid.ID {
51 m.clientLock.RLock()
52 defer m.clientLock.RUnlock()
53 c, ok := m.profileClientLookup[id]
54 if !ok {
55 return []uuid.ID{}
56 }
57 return c
58 }