auth

Paddy 2014-11-18 Parent:8398c3e4b3d9 Child:229422395721

77:d43c3fbf00f3 Go to Latest

auth/session.go

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..

History
1 package auth
3 import (
4 "errors"
5 "time"
7 "code.secondbit.org/uuid"
8 )
10 var (
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")
19 )
21 // Session represents a user's authenticated session, associating it with a profile
22 // and some audit data.
23 type Session struct {
24 ID string
25 IP string
26 UserAgent string
27 ProfileID uuid.ID
28 Created time.Time
29 Login string
30 Active bool
31 }
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)
38 }
40 func (m *memstore) createSession(session Session) error {
41 m.sessionLock.Lock()
42 defer m.sessionLock.Unlock()
43 if _, ok := m.sessions[session.ID]; ok {
44 return ErrSessionAlreadyExists
45 }
46 m.sessions[session.ID] = session
47 return nil
48 }
50 func (m *memstore) getSession(id string) (Session, error) {
51 m.sessionLock.RLock()
52 defer m.sessionLock.RUnlock()
53 if _, ok := m.sessions[id]; !ok {
54 return Session{}, ErrSessionNotFound
55 }
56 return m.sessions[id], nil
57 }
59 func (m *memstore) removeSession(id string) error {
60 m.sessionLock.Lock()
61 defer m.sessionLock.Unlock()
62 if _, ok := m.sessions[id]; !ok {
63 return ErrSessionNotFound
64 }
65 delete(m.sessions, id)
66 return nil
67 }
69 func (m *memstore) listSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
70 m.sessionLock.RLock()
71 defer m.sessionLock.RUnlock()
72 res := []Session{}
73 for _, session := range m.sessions {
74 if int64(len(res)) >= num {
75 break
76 }
77 if profile != nil && !profile.Equal(session.ProfileID) {
78 continue
79 }
80 if !before.IsZero() && session.Created.After(before) {
81 continue
82 }
83 res = append(res, session)
84 }
85 // BUG(paddy): sessions should return sorted by date created
86 return res, nil
87 }