auth
auth/memstore.go
Stub out sessions. Stop using the Login type when getting profile by Login, removing Logins, or recording Login use. The Login value has to be unique, anyways, and we don't actually know the Login type when getting a profile by Login. That's sort of the point. Create the concept of Sessions and a sessionStore type to manage our authentication sessions with the server. As per OWASP, we're basically just going to use a transparent, SHA256-generated random string as an ID, and store it client-side and server-side and just pass it back and forth. Add the ProfileID to the Grant type, because we need to remember who granted access. That's sort of important. Set a defaultGrantExpiration constant to an hour, so we have that one constant when creating new Grants. Create a helper that pulls the session ID out of an auth cookie, checks it against the sessionStore, and returns the Session if it's valid. Create a helper that pulls the username and password out of a basic auth header. Create a helper that authenticates a user's login and passphrase, checking them against the profileStore securely. Stub out how the cookie checking is going to work for getting grant approval. Fix the stored Grant RedirectURI to be the passed in redirect URI, not the RedirectURI that we ultimately redirect to. This is in accordance with the spec. Store the profile ID from our session in the created Grant. Stub out a GetTokenHandler that will allow users to exchange a Grant for a Token. Set a constant for the current passphrase scheme, which we will increment for each revision to the passphrase scheme, for backwards compatibility. Change the Profile iterations property to an int, not an int64, to match the code.secondbit.org/pass library (which is matching the PBKDF2 library).
| paddy@28 | 1 package auth |
| paddy@28 | 2 |
| paddy@31 | 3 import ( |
| paddy@31 | 4 "sync" |
| paddy@31 | 5 |
| paddy@45 | 6 "code.secondbit.org/uuid" |
| paddy@31 | 7 ) |
| paddy@28 | 8 |
| paddy@57 | 9 type memstore struct { |
| paddy@28 | 10 tokens map[string]Token |
| paddy@28 | 11 refreshTokenLookup map[string]string |
| paddy@28 | 12 profileTokenLookup map[string][]string |
| paddy@28 | 13 tokenLock sync.RWMutex |
| paddy@29 | 14 |
| paddy@29 | 15 grants map[string]Grant |
| paddy@29 | 16 grantLock sync.RWMutex |
| paddy@31 | 17 |
| paddy@31 | 18 clients map[string]Client |
| paddy@31 | 19 profileClientLookup map[string][]uuid.ID |
| paddy@31 | 20 clientLock sync.RWMutex |
| paddy@38 | 21 |
| paddy@41 | 22 endpoints map[string][]Endpoint |
| paddy@41 | 23 endpointLock sync.RWMutex |
| paddy@41 | 24 |
| paddy@38 | 25 profiles map[string]Profile |
| paddy@38 | 26 profileLock sync.RWMutex |
| paddy@44 | 27 |
| paddy@44 | 28 logins map[string]Login |
| paddy@44 | 29 profileLoginLookup map[string][]string |
| paddy@44 | 30 loginLock sync.RWMutex |
| paddy@28 | 31 } |
| paddy@28 | 32 |
| paddy@57 | 33 // NewMemstore returns an in-memory version of our datastores, |
| paddy@57 | 34 // which is handy for tests. Though the implementation is concurrency-safe, |
| paddy@57 | 35 // if makes no attempt to persist the data, and therefore it is inadvisable |
| paddy@57 | 36 // to use it in a production setting. |
| paddy@57 | 37 func NewMemstore() *memstore { |
| paddy@57 | 38 return &memstore{ |
| paddy@31 | 39 tokens: map[string]Token{}, |
| paddy@31 | 40 refreshTokenLookup: map[string]string{}, |
| paddy@31 | 41 profileTokenLookup: map[string][]string{}, |
| paddy@31 | 42 grants: map[string]Grant{}, |
| paddy@31 | 43 clients: map[string]Client{}, |
| paddy@31 | 44 profileClientLookup: map[string][]uuid.ID{}, |
| paddy@41 | 45 endpoints: map[string][]Endpoint{}, |
| paddy@38 | 46 profiles: map[string]Profile{}, |
| paddy@44 | 47 logins: map[string]Login{}, |
| paddy@44 | 48 profileLoginLookup: map[string][]string{}, |
| paddy@28 | 49 } |
| paddy@28 | 50 } |
| paddy@28 | 51 |
| paddy@57 | 52 func (m *memstore) lookupTokenByRefresh(token string) (string, error) { |
| paddy@28 | 53 m.tokenLock.RLock() |
| paddy@28 | 54 defer m.tokenLock.RUnlock() |
| paddy@28 | 55 t, ok := m.refreshTokenLookup[token] |
| paddy@28 | 56 if !ok { |
| paddy@28 | 57 return "", ErrTokenNotFound |
| paddy@28 | 58 } |
| paddy@28 | 59 return t, nil |
| paddy@28 | 60 } |
| paddy@28 | 61 |
| paddy@57 | 62 func (m *memstore) lookupTokensByProfileID(id string) ([]string, error) { |
| paddy@28 | 63 m.tokenLock.RLock() |
| paddy@28 | 64 defer m.tokenLock.RUnlock() |
| paddy@28 | 65 return m.profileTokenLookup[id], nil |
| paddy@28 | 66 } |
| paddy@31 | 67 |
| paddy@57 | 68 func (m *memstore) lookupClientsByProfileID(id string) []uuid.ID { |
| paddy@31 | 69 m.clientLock.RLock() |
| paddy@31 | 70 defer m.clientLock.RUnlock() |
| paddy@33 | 71 c, ok := m.profileClientLookup[id] |
| paddy@33 | 72 if !ok { |
| paddy@33 | 73 return []uuid.ID{} |
| paddy@33 | 74 } |
| paddy@33 | 75 return c |
| paddy@31 | 76 } |