This commit is contained in:
Lars Hoogestraat 2020-12-07 17:30:26 +01:00
parent aacba2dd95
commit 36fd4af4cc
4 changed files with 14 additions and 15 deletions

View File

@ -22,7 +22,7 @@ func (s *Session) GetLastTouchTime() time.Time {
return s.lastTouched return s.lastTouched
} }
//GetSessionID gets the sessionID //SessionID gets the sessionID
func (s *Session) SessionID() string { func (s *Session) SessionID() string {
return s.sid return s.sid
} }

View File

@ -59,13 +59,13 @@ func (imp *InMemoryProvider) Get(sid string) (*Session, error) {
return nil, fmt.Errorf("no session with id %s found", sid) return nil, fmt.Errorf("no session with id %s found", sid)
} }
//Get receives the session from the map by the session identifier //SessionIDsFromValues receives all session ids from the map found with the key and value
func (imp *InMemoryProvider) SessionIDsFromValues(key string, value interface{}) []string { func (imp *InMemoryProvider) SessionIDsFromValues(key string, value interface{}) []string {
imp.mutex.RLock() imp.mutex.RLock()
defer imp.mutex.RUnlock() defer imp.mutex.RUnlock()
ids := []string{} var ids []string
for _, s := range imp.sessions { for _, s := range imp.sessions {
if s.values[key] == value { if s.values[key] == value {
@ -84,8 +84,7 @@ func (imp *InMemoryProvider) Remove(sid string) {
delete(imp.sessions, sid) delete(imp.sessions, sid)
} }
//Clean clean sessions after a specified timeout //Clean clean sessions after the specified timeout
//Checks every x durations defined in config
func (imp *InMemoryProvider) Clean(ticker *time.Ticker, timeoutAfter time.Duration) { func (imp *InMemoryProvider) Clean(ticker *time.Ticker, timeoutAfter time.Duration) {
go func() { go func() {
for range ticker.C { for range ticker.C {
@ -98,4 +97,4 @@ func (imp *InMemoryProvider) Clean(ticker *time.Ticker, timeoutAfter time.Durati
imp.mutex.Unlock() imp.mutex.Unlock()
} }
}() }()
} }

View File

@ -12,7 +12,7 @@ import (
"time" "time"
) )
//SessionConfig contains settings for the session //SessionService contains settings for the session
type SessionService struct { type SessionService struct {
Path string Path string
HTTPOnly bool HTTPOnly bool
@ -68,7 +68,7 @@ func (sc SessionService) Get(rw http.ResponseWriter, r *http.Request) (*Session,
return sess, nil return sess, nil
} }
//Renew renews the session from //Renew renews the session
func (sc SessionService) Renew(rw http.ResponseWriter, r *http.Request) (*Session, error) { func (sc SessionService) Renew(rw http.ResponseWriter, r *http.Request) (*Session, error) {
cookie, err := r.Cookie(sc.Name) cookie, err := r.Cookie(sc.Name)

View File

@ -55,11 +55,11 @@ func TestSessionLifeCycle(t *testing.T) {
} }
if getSess.SessionID() != session.SessionID() { if getSess.SessionID() != session.SessionID() {
t.Fatalf("got an invalid session id. Initial %s, after get %s", session.SessionID(), getSess.SessionID()) t.Fatalf("invalid session id, want %s, got %s", session.SessionID(), getSess.SessionID())
} }
if getSess.GetValue("userid") != 3 { if getSess.GetValue("userid") != 3 {
t.Fatalf("the session does not contain the expected userid %d in the session values, got userid %v", 3, session.GetValue("userid")) t.Fatalf("the session does not contain the expected user id %d in the session values, got userid %v", 3, session.GetValue("userid"))
} }
removeSession(t, cookie.Raw, sessionService) removeSession(t, cookie.Raw, sessionService)
@ -142,18 +142,18 @@ func removeSession(t *testing.T, rawCookieValue string, cs session.SessionServic
func checkCookie(t *testing.T, cookie, expectedCookie *http.Cookie) { func checkCookie(t *testing.T, cookie, expectedCookie *http.Cookie) {
if cookie.Name != expectedCookie.Name { if cookie.Name != expectedCookie.Name {
t.Errorf("got an unexpected cookie name. Expected %s, bot got %s", expectedCookie.Name, cookie.Name) t.Errorf("unexpected cookie name, want: %s, got: %s", expectedCookie.Name, cookie.Name)
} }
if cookie.Path != expectedCookie.Path { if cookie.Path != expectedCookie.Path {
t.Errorf("got an unexpected cookie path. Expected %s, bot got %s", expectedCookie.Path, cookie.Path) t.Errorf("unexpected cookie path, want: %s, got %s", expectedCookie.Path, cookie.Path)
} }
if cookie.Value != expectedCookie.Value { if cookie.Value != expectedCookie.Value {
t.Errorf("got an unexpected cookie value. Expected %s, bot got %s", expectedCookie.Value, cookie.Value) t.Errorf("unexpected cookie value, want %s, got %s", expectedCookie.Value, cookie.Value)
} }
if cookie.HttpOnly != expectedCookie.HttpOnly { if cookie.HttpOnly != expectedCookie.HttpOnly {
t.Errorf("got an unexpected cookie http only f;ag. Expected %t, bot got %t", expectedCookie.HttpOnly, cookie.HttpOnly) t.Errorf("unexpected cookie http only flag, want %t, got %t", expectedCookie.HttpOnly, cookie.HttpOnly)
} }
if cookie.Secure != expectedCookie.Secure { if cookie.Secure != expectedCookie.Secure {
t.Errorf("got an unexpected cookie secure flag. Expected %t, bot got %t", expectedCookie.Secure, cookie.Secure) t.Errorf("unexpected cookie secure flag, want %t, got %t", expectedCookie.Secure, cookie.Secure)
} }
} }