2016-05-30 16:08:09 +02:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/imdario/mergo"
|
2016-09-04 21:02:31 +02:00
|
|
|
"github.com/kataras/go-sessions/fasthttp"
|
|
|
|
"time"
|
2016-05-30 16:08:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
universe time.Time // 0001-01-01 00:00:00 +0000 UTC
|
2016-07-26 20:26:54 +02:00
|
|
|
// CookieExpireNever the default cookie's life for sessions, unlimited (23 years)
|
|
|
|
CookieExpireNever = time.Now().AddDate(23, 0, 0)
|
2016-05-30 16:08:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// DefaultCookieName the secret cookie's name for sessions
|
2016-06-03 04:11:50 +02:00
|
|
|
DefaultCookieName = "irissessionid"
|
|
|
|
// DefaultSessionGcDuration is the default Session Manager's GCDuration , which is 2 hours
|
2016-05-30 16:08:09 +02:00
|
|
|
DefaultSessionGcDuration = time.Duration(2) * time.Hour
|
2016-08-18 02:20:59 +02:00
|
|
|
// DefaultCookieLength is the default Session Manager's CookieLength, which is 32
|
|
|
|
DefaultCookieLength = 32
|
2016-05-30 16:08:09 +02:00
|
|
|
)
|
|
|
|
|
2016-09-04 21:02:31 +02:00
|
|
|
// Sessions the configuration for sessions
|
|
|
|
// has 6 fields
|
|
|
|
// first is the cookieName, the session's name (string) ["mysessionsecretcookieid"]
|
|
|
|
// second enable if you want to decode the cookie's key also
|
|
|
|
// third is the time which the client's cookie expires
|
|
|
|
// forth is the cookie length (sessionid) int, defaults to 32, do not change if you don't have any reason to do
|
|
|
|
// fifth is the gcDuration (time.Duration) when this time passes it removes the unused sessions from the memory until the user come back
|
|
|
|
// sixth is the DisableSubdomainPersistence which you can set it to true in order dissallow your q subdomains to have access to the session cook
|
|
|
|
//
|
|
|
|
type Sessions sessions.Config
|
2016-05-30 16:08:09 +02:00
|
|
|
|
|
|
|
// DefaultSessions the default configs for Sessions
|
|
|
|
func DefaultSessions() Sessions {
|
|
|
|
return Sessions{
|
2016-06-30 04:58:04 +02:00
|
|
|
Cookie: DefaultCookieName,
|
2016-08-18 02:20:59 +02:00
|
|
|
CookieLength: DefaultCookieLength,
|
2016-07-01 00:38:29 +02:00
|
|
|
DecodeCookie: false,
|
2016-07-20 05:33:24 +02:00
|
|
|
Expires: 0,
|
2016-06-30 04:58:04 +02:00
|
|
|
GcDuration: DefaultSessionGcDuration,
|
2016-06-30 16:55:33 +02:00
|
|
|
DisableSubdomainPersistence: false,
|
2016-05-30 16:08:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges the default with the given config and returns the result
|
|
|
|
func (c Sessions) Merge(cfg []Sessions) (config Sessions) {
|
|
|
|
|
|
|
|
if len(cfg) > 0 {
|
|
|
|
config = cfg[0]
|
|
|
|
mergo.Merge(&config, c)
|
|
|
|
} else {
|
|
|
|
_default := c
|
|
|
|
config = _default
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-03 04:11:50 +02:00
|
|
|
// MergeSingle merges the default with the given config and returns the result
|
2016-05-30 16:08:09 +02:00
|
|
|
func (c Sessions) MergeSingle(cfg Sessions) (config Sessions) {
|
|
|
|
|
|
|
|
config = cfg
|
|
|
|
mergo.Merge(&config, c)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|