auth
auth/memstore.go
Implement a GetProfileHandler. Create a Handler that will allow us to return details about a Profile. Right now, you only get a single Profile at a time, which is problematic, because it will lead to N+1 requests. But we have no reason to retrieve anyone _else_'s Profile, so it's not like you need to be fetching any Profile other than your own. Also, this requires a Token issued for the Profile in question, which means you're limited to one Profile per request, anyways. Future avenues for exploration may be an admin Token granting access to many Profiles, the unspecified service flow for accessing the API, or simply accepting that name, join date, last active date, and ID are "public information".
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 }