auth

Paddy 2015-03-20 Parent:d103a598548c Child:b7e685839a1b

147:7ae03163f578 Go to Latest

auth/memstore.go

Randomly generate codes. We've been using our IDs for auth codes. But our IDs may at some point be non-random, for the purpose of optimising database performance, or some other perfectly valid reason. Auth codes we always want to be random, and have no relation to IDs, so why conflate them? Instead, we pull 16 random bytes out of crypto/rand.Reader and hex encode them.

History
1 package auth
3 import (
4 "sync"
6 "code.secondbit.org/uuid.hg"
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 authCodes map[string]AuthorizationCode
16 authCodeLock 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
32 sessions map[string]Session
33 sessionLock sync.RWMutex
35 scopes map[string]Scope
36 scopeLock sync.RWMutex
37 }
39 // NewMemstore returns an in-memory version of our datastores,
40 // which is handy for tests. Though the implementation is concurrency-safe,
41 // if makes no attempt to persist the data, and therefore it is inadvisable
42 // to use it in a production setting.
43 func NewMemstore() *memstore {
44 return &memstore{
45 tokens: map[string]Token{},
46 refreshTokenLookup: map[string]string{},
47 profileTokenLookup: map[string][]string{},
48 authCodes: map[string]AuthorizationCode{},
49 clients: map[string]Client{},
50 profileClientLookup: map[string][]uuid.ID{},
51 endpoints: map[string][]Endpoint{},
52 profiles: map[string]Profile{},
53 logins: map[string]Login{},
54 profileLoginLookup: map[string][]string{},
55 sessions: map[string]Session{},
56 scopes: map[string]Scope{},
57 }
58 }
60 func (m *memstore) lookupTokenByRefresh(token string) (string, error) {
61 m.tokenLock.RLock()
62 defer m.tokenLock.RUnlock()
63 t, ok := m.refreshTokenLookup[token]
64 if !ok {
65 return "", ErrTokenNotFound
66 }
67 return t, nil
68 }
70 func (m *memstore) lookupTokensByProfileID(id string) ([]string, error) {
71 m.tokenLock.RLock()
72 defer m.tokenLock.RUnlock()
73 return m.profileTokenLookup[id], nil
74 }
76 func (m *memstore) lookupClientsByProfileID(id string) []uuid.ID {
77 m.clientLock.RLock()
78 defer m.clientLock.RUnlock()
79 c, ok := m.profileClientLookup[id]
80 if !ok {
81 return []uuid.ID{}
82 }
83 return c
84 }