auth
auth/memstore.go
Further grant testing. Use the %T format to return the name of the GrantStore that failed the test, rather than just returning the position of the store. Test that storing a grant twice yields an ErrGrantAlreadyExists. Test that retrieving a grant that doesn't exist yields an ErrGrantNotFound. Compare returned grants against expectations.
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 }