auth
auth/memstore.go
Update profiles and add tests. Add tests for profile storage. Make Memstore implement the profilestore interface. Get the groundwork of profiles laid.
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
22 profiles map[string]Profile
23 profileLock sync.RWMutex
24 }
26 func NewMemstore() *Memstore {
27 return &Memstore{
28 tokens: map[string]Token{},
29 refreshTokenLookup: map[string]string{},
30 profileTokenLookup: map[string][]string{},
31 grants: map[string]Grant{},
32 clients: map[string]Client{},
33 profileClientLookup: map[string][]uuid.ID{},
34 profiles: map[string]Profile{},
35 }
36 }
38 func (m *Memstore) lookupTokenByRefresh(token string) (string, error) {
39 m.tokenLock.RLock()
40 defer m.tokenLock.RUnlock()
41 t, ok := m.refreshTokenLookup[token]
42 if !ok {
43 return "", ErrTokenNotFound
44 }
45 return t, nil
46 }
48 func (m *Memstore) lookupTokensByProfileID(id string) ([]string, error) {
49 m.tokenLock.RLock()
50 defer m.tokenLock.RUnlock()
51 return m.profileTokenLookup[id], nil
52 }
54 func (m *Memstore) lookupClientsByProfileID(id string) []uuid.ID {
55 m.clientLock.RLock()
56 defer m.clientLock.RUnlock()
57 c, ok := m.profileClientLookup[id]
58 if !ok {
59 return []uuid.ID{}
60 }
61 return c
62 }