Write Session tests.
Add loginURI as a property to our Context, to keep track of where users should
be redirected to log in.
Implement the sessionStore in the memstore to let us test with Sessions.
Catch when the HTTP Basic Auth header doesn't include two parts, rather than
panicking. Return an ErrInvalidAuthFormat.
Clean up the error handling for checkCookie to be cleaner. Log unexpected errors
from request.Cookie.
Stop checking for cookie expiration times--those aren't sent to the server, so
we'll never get a valid session if we look for them.
Add a helper to build a login redirect URI--a URI the user can be redirected to
that has a URL-encoded URL to redirect the user back to after a successful
login.
Add a wrapper to wrap our Context into HTTP handlers.
Create a RegisterOAuth2 helper that adds the OAuth2 endpoints to a Gorilla/mux
router.
Redirect users to the login page when they have no session set or an invalid
session.
Return a server error when we can't check our cookie for whatever reason.
Log errors.
Add sessions to our OAuth2 tests so the tests stop failing--the session check
was interfering with them.
Add a test for our getBasicAuth helper to ensure that we're parsing basic auth
correctly.
Add an ErrSessionAlreadyExists error to be returned when a session has an ID
conflict.
Test that our memstore implementation of the sessionStore works as intended..
7 "code.secondbit.org/uuid"
11 // ErrNoSessionStore is returned when a Context tries to act on a sessionStore without setting on first.
12 ErrNoSessionStore = errors.New("no sessionStore was specified for the Context")
13 // ErrSessionNotFound is returned when a Session is requested but not found in the sessionStore.
14 ErrSessionNotFound = errors.New("session not found in sessionStore")
15 // ErrInvalidSession is returned when a Session is specified but is not valid.
16 ErrInvalidSession = errors.New("session is not valid")
17 // ErrSessionAlreadyExists is returned when a sessionStore tries to store a Session with an ID that already exists in the sessionStore.
18 ErrSessionAlreadyExists = errors.New("session already exists")
21 // Session represents a user's authenticated session, associating it with a profile
22 // and some audit data.
33 type sessionStore interface {
34 createSession(session Session) error
35 getSession(id string) (Session, error)
36 removeSession(id string) error
37 listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error)
40 func (m *memstore) createSession(session Session) error {
42 defer m.sessionLock.Unlock()
43 if _, ok := m.sessions[session.ID]; ok {
44 return ErrSessionAlreadyExists
46 m.sessions[session.ID] = session
50 func (m *memstore) getSession(id string) (Session, error) {
52 defer m.sessionLock.RUnlock()
53 if _, ok := m.sessions[id]; !ok {
54 return Session{}, ErrSessionNotFound
56 return m.sessions[id], nil
59 func (m *memstore) removeSession(id string) error {
61 defer m.sessionLock.Unlock()
62 if _, ok := m.sessions[id]; !ok {
63 return ErrSessionNotFound
65 delete(m.sessions, id)
69 func (m *memstore) listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
71 defer m.sessionLock.RUnlock()
73 for _, session := range m.sessions {
74 if int64(len(res)) >= num {
77 if profile != nil && !profile.Equal(session.ProfileID) {
80 if !before.IsZero() && session.Created.After(before) {
83 res = append(res, session)
85 // BUG(paddy): sessions should return sorted by date created