auth
auth/session.go
Break client verification out, break token returns out. Break client verification out into a helper function to avoid rewriting it for pretty much every grant. Break token returns out into a new function as part of the GrantType, so that implicit grants can redirect with the token value. Split returning the token as JSON into its own exported function, which can be used in multiple grants. Return more relevant information to the template when a user is deciding whether or not to authorize a grant.
| paddy@70 | 1 package auth |
| paddy@70 | 2 |
| paddy@70 | 3 import ( |
| paddy@70 | 4 "errors" |
| paddy@70 | 5 "time" |
| paddy@70 | 6 |
| paddy@70 | 7 "code.secondbit.org/uuid" |
| paddy@70 | 8 ) |
| paddy@70 | 9 |
| paddy@70 | 10 var ( |
| paddy@70 | 11 // ErrNoSessionStore is returned when a Context tries to act on a sessionStore without setting on first. |
| paddy@70 | 12 ErrNoSessionStore = errors.New("no sessionStore was specified for the Context") |
| paddy@70 | 13 // ErrSessionNotFound is returned when a Session is requested but not found in the sessionStore. |
| paddy@70 | 14 ErrSessionNotFound = errors.New("session not found in sessionStore") |
| paddy@70 | 15 // ErrInvalidSession is returned when a Session is specified but is not valid. |
| paddy@70 | 16 ErrInvalidSession = errors.New("session is not valid") |
| paddy@77 | 17 // ErrSessionAlreadyExists is returned when a sessionStore tries to store a Session with an ID that already exists in the sessionStore. |
| paddy@77 | 18 ErrSessionAlreadyExists = errors.New("session already exists") |
| paddy@70 | 19 ) |
| paddy@70 | 20 |
| paddy@70 | 21 // Session represents a user's authenticated session, associating it with a profile |
| paddy@70 | 22 // and some audit data. |
| paddy@70 | 23 type Session struct { |
| paddy@70 | 24 ID string |
| paddy@70 | 25 IP string |
| paddy@70 | 26 UserAgent string |
| paddy@70 | 27 ProfileID uuid.ID |
| paddy@70 | 28 Created time.Time |
| paddy@70 | 29 Login string |
| paddy@70 | 30 Active bool |
| paddy@70 | 31 } |
| paddy@70 | 32 |
| paddy@70 | 33 type sessionStore interface { |
| paddy@70 | 34 createSession(session Session) error |
| paddy@70 | 35 getSession(id string) (Session, error) |
| paddy@70 | 36 removeSession(id string) error |
| paddy@70 | 37 listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) |
| paddy@70 | 38 } |
| paddy@77 | 39 |
| paddy@77 | 40 func (m *memstore) createSession(session Session) error { |
| paddy@77 | 41 m.sessionLock.Lock() |
| paddy@77 | 42 defer m.sessionLock.Unlock() |
| paddy@77 | 43 if _, ok := m.sessions[session.ID]; ok { |
| paddy@77 | 44 return ErrSessionAlreadyExists |
| paddy@77 | 45 } |
| paddy@77 | 46 m.sessions[session.ID] = session |
| paddy@77 | 47 return nil |
| paddy@77 | 48 } |
| paddy@77 | 49 |
| paddy@77 | 50 func (m *memstore) getSession(id string) (Session, error) { |
| paddy@77 | 51 m.sessionLock.RLock() |
| paddy@77 | 52 defer m.sessionLock.RUnlock() |
| paddy@77 | 53 if _, ok := m.sessions[id]; !ok { |
| paddy@77 | 54 return Session{}, ErrSessionNotFound |
| paddy@77 | 55 } |
| paddy@77 | 56 return m.sessions[id], nil |
| paddy@77 | 57 } |
| paddy@77 | 58 |
| paddy@77 | 59 func (m *memstore) removeSession(id string) error { |
| paddy@77 | 60 m.sessionLock.Lock() |
| paddy@77 | 61 defer m.sessionLock.Unlock() |
| paddy@77 | 62 if _, ok := m.sessions[id]; !ok { |
| paddy@77 | 63 return ErrSessionNotFound |
| paddy@77 | 64 } |
| paddy@77 | 65 delete(m.sessions, id) |
| paddy@77 | 66 return nil |
| paddy@77 | 67 } |
| paddy@77 | 68 |
| paddy@77 | 69 func (m *memstore) listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) { |
| paddy@77 | 70 m.sessionLock.RLock() |
| paddy@77 | 71 defer m.sessionLock.RUnlock() |
| paddy@77 | 72 res := []Session{} |
| paddy@77 | 73 for _, session := range m.sessions { |
| paddy@77 | 74 if int64(len(res)) >= num { |
| paddy@77 | 75 break |
| paddy@77 | 76 } |
| paddy@77 | 77 if profile != nil && !profile.Equal(session.ProfileID) { |
| paddy@77 | 78 continue |
| paddy@77 | 79 } |
| paddy@77 | 80 if !before.IsZero() && session.Created.After(before) { |
| paddy@77 | 81 continue |
| paddy@77 | 82 } |
| paddy@77 | 83 res = append(res, session) |
| paddy@77 | 84 } |
| paddy@77 | 85 // BUG(paddy): sessions should return sorted by date created |
| paddy@77 | 86 return res, nil |
| paddy@77 | 87 } |