Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +02:00
|
|
|
// Copyright 2017 Gerasimos Maropoulos, ΓΜ. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
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.
|
|
|
|
// (or write to a *session's value which is race if we don't lock)
|
|
|
|
// narrow locks are fasters but are useless here.
|
|
|
|
mu sync.Mutex
|
|
|
|
sessions map[string]*session
|
|
|
|
databases []Database
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// newProvider returns a new sessions provider
|
|
|
|
func newProvider() *provider {
|
|
|
|
return &provider{
|
|
|
|
sessions: make(map[string]*session, 0),
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSession returns a new session from sessionid
|
|
|
|
func (p *provider) newSession(sid string, expires time.Duration) *session {
|
|
|
|
|
|
|
|
sess := &session{
|
|
|
|
sid: sid,
|
|
|
|
provider: p,
|
2017-06-08 02:39:15 +02:00
|
|
|
values: p.loadSessionValuesFromDB(sid),
|
2017-02-15 19:06:19 +01:00
|
|
|
flashes: make(map[string]*flashMessage),
|
|
|
|
}
|
|
|
|
|
|
|
|
if expires > 0 { // if not unlimited life duration and no -1 (cookie remove action is based on browser's session)
|
|
|
|
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(sid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess
|
|
|
|
}
|
|
|
|
|
2017-06-08 02:39:15 +02:00
|
|
|
// can return nil
|
|
|
|
func (p *provider) loadSessionValuesFromDB(sid string) memstore.Store {
|
|
|
|
var store memstore.Store
|
2017-02-15 19:06:19 +01:00
|
|
|
|
|
|
|
for i, n := 0, len(p.databases); i < n; i++ {
|
|
|
|
if dbValues := p.databases[i].Load(sid); 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-06-08 02:39:15 +02:00
|
|
|
return store
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
|
2017-06-08 02:39:15 +02:00
|
|
|
func (p *provider) updateDatabases(sid string, store memstore.Store) {
|
|
|
|
|
|
|
|
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++ {
|
|
|
|
p.databases[i].Update(sid, mapValues)
|
|
|
|
}
|
2017-02-15 19:06:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init creates the session and returns it
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read returns the store which sid parameter belongs
|
Publish the new version :airplane: | Look description please!
# FAQ
### Looking for free support?
http://support.iris-go.com
https://kataras.rocket.chat/channel/iris
### Looking for previous versions?
https://github.com/kataras/iris#version
### Should I upgrade my Iris?
Developers are not forced to upgrade if they don't really need it. Upgrade whenever you feel ready.
> Iris uses the [vendor directory](https://docs.google.com/document/d/1Bz5-UB7g2uPBdOx-rw5t9MxJwkfpx90cqG9AFL0JAYo) feature, so you get truly reproducible builds, as this method guards against upstream renames and deletes.
**How to upgrade**: Open your command-line and execute this command: `go get -u github.com/kataras/iris`.
For further installation support, please click [here](http://support.iris-go.com/d/16-how-to-install-iris-web-framework).
### About our new home page
http://iris-go.com
Thanks to [Santosh Anand](https://github.com/santoshanand) the http://iris-go.com has been upgraded and it's really awesome!
[Santosh](https://github.com/santoshanand) is a freelancer, he has a great knowledge of nodejs and express js, Android, iOS, React Native, Vue.js etc, if you need a developer to find or create a solution for your problem or task, please contact with him.
The amount of the next two or three donations you'll send they will be immediately transferred to his own account balance, so be generous please!
Read more at https://github.com/kataras/iris/blob/master/HISTORY.md
Former-commit-id: eec2d71bbe011d6b48d2526eb25919e36e5ad94e
2017-06-03 22:22:52 +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()
|
|
|
|
return sess
|
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
return p.Init(sid, expires) // if not found create new
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
delete(p.sessions, sid)
|
|
|
|
p.updateDatabases(sid, nil)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
delete(p.sessions, sess.ID())
|
|
|
|
p.updateDatabases(sess.ID(), nil)
|
|
|
|
}
|
|
|
|
p.mu.Unlock()
|
|
|
|
|
|
|
|
}
|