auth

Paddy 2014-10-15 Parent:3a6a65ed380c Child:e45bfa2abc00

50:b620d32d9903 Go to Latest

auth/memstore.go

Create the Context type and its helpers. Create a Context type that ties together all our Stores and other configuration-specific items. Create helper functions for the Context, to throw errors when something is used without first being set, as all possible Context values _can_ be nil. Basically, it's better to throw an error than panic.

History
1 package auth
3 import (
4 "sync"
6 "code.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 }