auth

Paddy 2015-12-14 Parent:d103a598548c

181:b7e685839a1b Go to Latest

auth/memstore.go

Break out scopes and events. This repo has gotten unwieldy, and there are portions of it that need to be imported by a large number of other packages. For example, scopes will be used in almost every API we write. Rather than importing the entirety of this codebase into every API we write, I've opted to move the scope logic out into a scopes package, with a subpackage for the defined types, which is all most projects actually want to import. We also define some event type constants, and importing those shouldn't require a project to import all our dependencies, either. So I made an events subpackage that just holds those constants. This package has become a little bit of a red-headed stepchild and is do for a refactor, but I'm trying to put that off as long as I can. The refactoring of our scopes stuff has left a bug wherein a token can be granted for scopes that don't exist. I'm going to need to revisit that, and also how to limit scopes to only be granted to the users that should be able to request them. But that's a battle for another day.

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 }