auth

Paddy 2015-01-29 Parent:c03b5eb3179e Child:d103a598548c

133:bc842183181d Go to Latest

auth/memstore.go

Add Client updating from the API. Add a handler to update Clients using the API. Add a helper that will decode a request for us based on its Content-Type header. Change the ClientChange.Validate function to return as many errors as possible, as opposed to just the first error it encounters. Update the ClientChange.Validate tests to take advantage of the new signature.

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
34 }
36 // NewMemstore returns an in-memory version of our datastores,
37 // which is handy for tests. Though the implementation is concurrency-safe,
38 // if makes no attempt to persist the data, and therefore it is inadvisable
39 // to use it in a production setting.
40 func NewMemstore() *memstore {
41 return &memstore{
42 tokens: map[string]Token{},
43 refreshTokenLookup: map[string]string{},
44 profileTokenLookup: map[string][]string{},
45 authCodes: map[string]AuthorizationCode{},
46 clients: map[string]Client{},
47 profileClientLookup: map[string][]uuid.ID{},
48 endpoints: map[string][]Endpoint{},
49 profiles: map[string]Profile{},
50 logins: map[string]Login{},
51 profileLoginLookup: map[string][]string{},
52 sessions: map[string]Session{},
53 }
54 }
56 func (m *memstore) lookupTokenByRefresh(token string) (string, error) {
57 m.tokenLock.RLock()
58 defer m.tokenLock.RUnlock()
59 t, ok := m.refreshTokenLookup[token]
60 if !ok {
61 return "", ErrTokenNotFound
62 }
63 return t, nil
64 }
66 func (m *memstore) lookupTokensByProfileID(id string) ([]string, error) {
67 m.tokenLock.RLock()
68 defer m.tokenLock.RUnlock()
69 return m.profileTokenLookup[id], nil
70 }
72 func (m *memstore) lookupClientsByProfileID(id string) []uuid.ID {
73 m.clientLock.RLock()
74 defer m.clientLock.RUnlock()
75 c, ok := m.profileClientLookup[id]
76 if !ok {
77 return []uuid.ID{}
78 }
79 return c
80 }