2017-02-15 19:06:19 +01:00
|
|
|
package sessions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
2017-06-08 02:39:15 +02:00
|
|
|
|
|
|
|
"github.com/kataras/iris/core/memstore"
|
2017-02-15 19:06:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// provider contains the sessions and external databases (load and update).
|
|
|
|
// It's the session memory manager
|
|
|
|
provider struct {
|
|
|
|
// we don't use RWMutex because all actions have read and write at the same action function.
|
2017-07-10 17:32:42 +02:00
|
|
|
// (or write to a *Session's value which is race if we don't lock)
|
2017-02-15 19:06:19 +01:00
|
|
|
// narrow locks are fasters but are useless here.
|
|
|
|
mu sync.Mutex
|
2017-07-10 17:32:42 +02:00
|
|
|
sessions map[string]*Session
|
2017-02-15 19:06:19 +01:00
|
|
|
databases []Database
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// newProvider returns a new sessions provider
|
|
|
|
func newProvider() *provider {
|
|
|
|
return &provider{
|
2017-07-10 17:32:42 +02:00
|
|
|
sessions: make(map[string]*Session, 0),
|
2017-02-15 19:06:19 +01:00
|
|
|
databases: make([]Database, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterDatabase adds a session database
|
|
|
|
// a session db doesn't have write access
|
|
|
|
func (p *provider) RegisterDatabase(db Database) {
|
|
|
|
p.mu.Lock() // for any case
|
|
|
|
p.databases = append(p.databases, db)
|
|
|
|
p.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
// startAutoDestroy start a task which destoy the session when expire date is reached,
|
|
|
|
// but only if `expires` parameter is positive. It updates the expire date of the session from `expires` parameter.
|
|
|
|
func (p *provider) startAutoDestroy(s *Session, expires time.Duration) bool {
|
|
|
|
res := expires > 0
|
|
|
|
if res { // if not unlimited life duration and no -1 (cookie remove action is based on browser's session)
|
|
|
|
expireDate := time.Now().Add(expires)
|
|
|
|
|
|
|
|
s.expireAt = &expireDate
|
|
|
|
s.timer = time.AfterFunc(expires, func() {
|
|
|
|
// the destroy makes the check if this session is exists then or not,
|
|
|
|
// this is used to destroy the session from the server-side also
|
|
|
|
// it's good to have here for security reasons, I didn't add it on the gc function to separate its action
|
|
|
|
p.Destroy(s.sid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
// newSession returns a new session from sessionid
|
2017-07-10 17:32:42 +02:00
|
|
|
func (p *provider) newSession(sid string, expires time.Duration) *Session {
|
2017-07-31 20:49:30 +02:00
|
|
|
values, expireAt := p.loadSessionValuesFromDB(sid)
|
|
|
|
|
2017-07-10 17:32:42 +02:00
|
|
|
sess := &Session{
|
2017-02-15 19:06:19 +01:00
|
|
|
sid: sid,
|
|
|
|
provider: p,
|
2017-07-31 20:49:30 +02:00
|
|
|
values: values,
|
2017-02-15 19:06:19 +01:00
|
|
|
flashes: make(map[string]*flashMessage),
|
2017-07-31 20:49:30 +02:00
|
|
|
expireAt: expireAt,
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
if (len(values) > 0) && (sess.expireAt != nil) {
|
|
|
|
// Restore expiration state
|
|
|
|
// However, if session save in database has no expiration date,
|
|
|
|
// therefore the expiration will be reinitialised with session configuration
|
|
|
|
expires = sess.expireAt.Sub(time.Now())
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
p.startAutoDestroy(sess, expires)
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
return sess
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
// can return nil memstore
|
|
|
|
func (p *provider) loadSessionValuesFromDB(sid string) (memstore.Store, *time.Time) {
|
2017-06-08 02:39:15 +02:00
|
|
|
var store memstore.Store
|
2017-07-31 20:49:30 +02:00
|
|
|
var expireDate *time.Time
|
2017-02-15 19:06:19 +01:00
|
|
|
|
|
|
|
for i, n := 0, len(p.databases); i < n; i++ {
|
2017-07-31 20:49:30 +02:00
|
|
|
dbValues, currentExpireDate := p.databases[i].Load(sid)
|
|
|
|
if dbValues != nil && len(dbValues) > 0 {
|
2017-06-08 02:39:15 +02:00
|
|
|
for k, v := range dbValues {
|
|
|
|
store.Set(k, v)
|
|
|
|
}
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
2017-07-31 20:49:30 +02:00
|
|
|
|
|
|
|
if (currentExpireDate != nil) && ((expireDate == nil) || expireDate.After(*currentExpireDate)) {
|
|
|
|
expireDate = currentExpireDate
|
|
|
|
}
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
// Check if session has already expired
|
|
|
|
if (expireDate != nil) && expireDate.Before(time.Now()) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return store, expireDate
|
|
|
|
}
|
2017-06-08 02:39:15 +02:00
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
func (p *provider) updateDatabases(sess *Session, store memstore.Store) {
|
2017-06-08 02:39:15 +02:00
|
|
|
if l := store.Len(); l > 0 {
|
|
|
|
mapValues := make(map[string]interface{}, l)
|
|
|
|
|
|
|
|
store.Visit(func(k string, v interface{}) {
|
|
|
|
mapValues[k] = v
|
|
|
|
})
|
|
|
|
|
|
|
|
for i, n := 0, len(p.databases); i < n; i++ {
|
2017-07-31 20:49:30 +02:00
|
|
|
p.databases[i].Update(sess.sid, mapValues, sess.expireAt)
|
2017-06-08 02:39:15 +02:00
|
|
|
}
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init creates the session and returns it
|
2017-07-10 17:32:42 +02:00
|
|
|
func (p *provider) Init(sid string, expires time.Duration) *Session {
|
2017-02-15 19:06:19 +01:00
|
|
|
newSession := p.newSession(sid, expires)
|
|
|
|
p.mu.Lock()
|
|
|
|
p.sessions[sid] = newSession
|
|
|
|
p.mu.Unlock()
|
|
|
|
return newSession
|
|
|
|
}
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
// UpdateExpiraton update expire date of a session, plus it updates destroy task
|
|
|
|
func (p *provider) UpdateExpiraton(sid string, expires time.Duration) (done bool) {
|
|
|
|
if expires <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
p.mu.Lock()
|
|
|
|
sess, found := p.sessions[sid]
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if sess.timer == nil {
|
|
|
|
return p.startAutoDestroy(sess, expires)
|
|
|
|
} else {
|
|
|
|
if expires <= 0 {
|
|
|
|
sess.timer.Stop()
|
|
|
|
sess.timer = nil
|
|
|
|
sess.expireAt = nil
|
|
|
|
} else {
|
|
|
|
expireDate := time.Now().Add(expires)
|
|
|
|
|
|
|
|
sess.expireAt = &expireDate
|
|
|
|
sess.timer.Reset(expires)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
// Read returns the store which sid parameter belongs
|
2017-07-10 17:32:42 +02:00
|
|
|
func (p *provider) Read(sid string, expires time.Duration) *Session {
|
2017-02-15 19:06:19 +01:00
|
|
|
p.mu.Lock()
|
|
|
|
if sess, found := p.sessions[sid]; found {
|
|
|
|
sess.runFlashGC() // run the flash messages GC, new request here of existing session
|
|
|
|
p.mu.Unlock()
|
2017-07-31 20:49:30 +02:00
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
return sess
|
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
2017-07-31 20:49:30 +02:00
|
|
|
return p.Init(sid, expires) // if not found create new
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy destroys the session, removes all sessions and flash values,
|
|
|
|
// the session itself and updates the registered session databases,
|
|
|
|
// this called from sessionManager which removes the client's cookie also.
|
|
|
|
func (p *provider) Destroy(sid string) {
|
|
|
|
p.mu.Lock()
|
|
|
|
if sess, found := p.sessions[sid]; found {
|
|
|
|
sess.values = nil
|
|
|
|
sess.flashes = nil
|
2017-07-31 20:49:30 +02:00
|
|
|
if sess.timer != nil {
|
|
|
|
sess.timer.Stop()
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
delete(p.sessions, sid)
|
2017-07-31 20:49:30 +02:00
|
|
|
p.updateDatabases(sess, nil)
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestroyAll removes all sessions
|
|
|
|
// from the server-side memory (and database if registered).
|
|
|
|
// Client's session cookie will still exist but it will be reseted on the next request.
|
|
|
|
func (p *provider) DestroyAll() {
|
|
|
|
p.mu.Lock()
|
|
|
|
for _, sess := range p.sessions {
|
2017-07-31 20:49:30 +02:00
|
|
|
if sess.timer != nil {
|
|
|
|
sess.timer.Stop()
|
|
|
|
}
|
|
|
|
|
2017-02-15 19:06:19 +01:00
|
|
|
delete(p.sessions, sess.ID())
|
2017-07-31 20:49:30 +02:00
|
|
|
p.updateDatabases(sess, nil)
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
}
|