auth

Paddy 2014-09-19 Parent:113ccb15b919 Child:3a6a65ed380c

44:fb827644bfd8 Go to Latest

auth/memstore.go

Add support for bulk changes and for logins. Logins now get stored, listed, removed, and updated. You can select a profile by the login associated with it. Also added support for bulk changing profiles, because it may be necesary to set many profiles to compromised at the same time, and there's no sense in requiring a statement per profile.

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
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
31 }
33 func NewMemstore() *Memstore {
34 return &Memstore{
35 tokens: map[string]Token{},
36 refreshTokenLookup: map[string]string{},
37 profileTokenLookup: map[string][]string{},
38 grants: map[string]Grant{},
39 clients: map[string]Client{},
40 profileClientLookup: map[string][]uuid.ID{},
41 endpoints: map[string][]Endpoint{},
42 profiles: map[string]Profile{},
43 logins: map[string]Login{},
44 profileLoginLookup: map[string][]string{},
45 }
46 }
48 func (m *Memstore) lookupTokenByRefresh(token string) (string, error) {
49 m.tokenLock.RLock()
50 defer m.tokenLock.RUnlock()
51 t, ok := m.refreshTokenLookup[token]
52 if !ok {
53 return "", ErrTokenNotFound
54 }
55 return t, nil
56 }
58 func (m *Memstore) lookupTokensByProfileID(id string) ([]string, error) {
59 m.tokenLock.RLock()
60 defer m.tokenLock.RUnlock()
61 return m.profileTokenLookup[id], nil
62 }
64 func (m *Memstore) lookupClientsByProfileID(id string) []uuid.ID {
65 m.clientLock.RLock()
66 defer m.clientLock.RUnlock()
67 c, ok := m.profileClientLookup[id]
68 if !ok {
69 return []uuid.ID{}
70 }
71 return c
72 }