From a1e6d81b498e1cf3df4d41cb37ca8fa288c8ca7f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Mon, 13 Apr 2020 22:15:55 +0300 Subject: [PATCH] add access to the session manager through new 'Session.Man' field relative to: https://github.com/kataras/iris/issues/1485 Former-commit-id: c4ced38b74af42bfcd17abe6b439b35db6837bbf --- sessions/provider.go | 11 ++++++----- sessions/session.go | 15 ++++++++++----- sessions/sessions.go | 4 ++-- sessions/sessions_test.go | 4 ++-- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/sessions/provider.go b/sessions/provider.go index bba86b2f..effdb230 100644 --- a/sessions/provider.go +++ b/sessions/provider.go @@ -36,7 +36,7 @@ func (p *provider) RegisterDatabase(db Database) { } // newSession returns a new session from sessionid -func (p *provider) newSession(sid string, expires time.Duration) *Session { +func (p *provider) newSession(man *Sessions, sid string, expires time.Duration) *Session { onExpire := func() { p.Destroy(sid) } @@ -60,6 +60,7 @@ func (p *provider) newSession(sid string, expires time.Duration) *Session { sess := &Session{ sid: sid, + Man: man, provider: p, flashes: make(map[string]*flashMessage), Lifetime: lifetime, @@ -69,8 +70,8 @@ func (p *provider) newSession(sid string, expires time.Duration) *Session { } // Init creates the session and returns it -func (p *provider) Init(sid string, expires time.Duration) *Session { - newSession := p.newSession(sid, expires) +func (p *provider) Init(man *Sessions, sid string, expires time.Duration) *Session { + newSession := p.newSession(man, sid, expires) p.mu.Lock() p.sessions[sid] = newSession p.mu.Unlock() @@ -110,7 +111,7 @@ func (p *provider) UpdateExpiration(sid string, expires time.Duration) error { } // Read returns the store which sid parameter belongs -func (p *provider) Read(sid string, expires time.Duration) *Session { +func (p *provider) Read(man *Sessions, sid string, expires time.Duration) *Session { p.mu.Lock() if sess, found := p.sessions[sid]; found { sess.runFlashGC() // run the flash messages GC, new request here of existing session @@ -120,7 +121,7 @@ func (p *provider) Read(sid string, expires time.Duration) *Session { } p.mu.Unlock() - return p.Init(sid, expires) // if not found create new + return p.Init(man, sid, expires) // if not found create new } func (p *provider) registerDestroyListener(ln DestroyListener) { diff --git a/sessions/session.go b/sessions/session.go index 9c3b417f..f6473770 100644 --- a/sessions/session.go +++ b/sessions/session.go @@ -15,11 +15,16 @@ type ( // // This is what will be returned when sess := sessions.Start(). Session struct { - sid string - isNew bool - flashes map[string]*flashMessage - mu sync.RWMutex // for flashes. + sid string + isNew bool + flashes map[string]*flashMessage + mu sync.RWMutex // for flashes. + // Lifetime it contains the expiration data, use it for read-only information. + // See `Sessions.UpdateExpiration` too. Lifetime LifeTime + // Man is the sessions manager that this session created of. + Man *Sessions + provider *provider } @@ -37,7 +42,7 @@ type ( // Note that this method does NOT remove the client's cookie, although // it should be reseted if new session is attached to that (client). // -// Use the session's manager `Destroy(ctx)` in order to remove the cookie as well. +// Use the session's manager `Destroy(ctx)` in order to remove the cookie instead. func (s *Session) Destroy() { s.provider.deleteSession(s) } diff --git a/sessions/sessions.go b/sessions/sessions.go index d2b09521..f80c8344 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -84,7 +84,7 @@ func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOpt if cookieValue == "" { // cookie doesn't exist, let's generate a session and set a cookie. sid := s.config.SessionIDGenerator(ctx) - sess := s.provider.Init(sid, s.config.Expires) + sess := s.provider.Init(s, sid, s.config.Expires) sess.isNew = s.provider.db.Len(sid) == 0 s.updateCookie(ctx, sid, s.config.Expires, cookieOptions...) @@ -92,7 +92,7 @@ func (s *Sessions) Start(ctx context.Context, cookieOptions ...context.CookieOpt return sess } - return s.provider.Read(cookieValue, s.config.Expires) + return s.provider.Read(s, cookieValue, s.config.Expires) } const contextSessionKey = "iris.session" diff --git a/sessions/sessions_test.go b/sessions/sessions_test.go index ba5c3033..74082073 100644 --- a/sessions/sessions_test.go +++ b/sessions/sessions_test.go @@ -237,12 +237,12 @@ func TestSessionsUpdateExpiration(t *testing.T) { app.Post("/remember_me", func(ctx iris.Context) { // re-sends the cookie with the new Expires and MaxAge fields, // test checks that on same session id too. - sess.UpdateExpiration(ctx, 24*time.Hour) + sessions.Get(ctx).Man.UpdateExpiration(ctx, 24*time.Hour) writeResponse(ctx) }) app.Get("/destroy", func(ctx iris.Context) { - sess.Destroy(ctx) // this will delete the cookie too. + sessions.Get(ctx).Man.Destroy(ctx) // this will delete the cookie too. }) e := httptest.New(t, app, httptest.URL("http://example.com"))