2019-04-24 21:52:08 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2020-12-29 01:44:47 +00:00
|
|
|
fasthttpsession "github.com/authelia/session/v2"
|
|
|
|
"github.com/authelia/session/v2/providers/memory"
|
|
|
|
"github.com/authelia/session/v2/providers/redis"
|
2019-04-24 21:52:08 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/internal/utils"
|
2019-04-24 21:52:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Provider a session provider.
|
|
|
|
type Provider struct {
|
|
|
|
sessionHolder *fasthttpsession.Session
|
2020-04-03 23:11:33 +00:00
|
|
|
RememberMe time.Duration
|
2020-04-05 12:37:21 +00:00
|
|
|
Inactivity time.Duration
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProvider instantiate a session provider given a configuration.
|
|
|
|
func NewProvider(configuration schema.SessionConfiguration) *Provider {
|
|
|
|
providerConfig := NewProviderConfig(configuration)
|
|
|
|
|
|
|
|
provider := new(Provider)
|
|
|
|
provider.sessionHolder = fasthttpsession.New(providerConfig.config)
|
2020-04-05 12:37:21 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
duration, err := utils.ParseDurationString(configuration.RememberMeDuration)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-03 23:11:33 +00:00
|
|
|
provider.RememberMe = duration
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
duration, err = utils.ParseDurationString(configuration.Inactivity)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2020-04-05 12:37:21 +00:00
|
|
|
provider.Inactivity = duration
|
|
|
|
|
2020-05-18 02:45:47 +00:00
|
|
|
var providerImpl fasthttpsession.Provider
|
|
|
|
if providerConfig.redisConfig != nil {
|
|
|
|
providerImpl, err = redis.New(*providerConfig.redisConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
providerImpl, err = memory.New(memory.Config{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = provider.sessionHolder.SetProvider(providerImpl)
|
2019-04-24 21:52:08 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
2020-05-02 05:06:39 +00:00
|
|
|
// GetSession return the user session from a request.
|
2019-04-24 21:52:08 +00:00
|
|
|
func (p *Provider) GetSession(ctx *fasthttp.RequestCtx) (UserSession, error) {
|
|
|
|
store, err := p.sessionHolder.Get(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return NewDefaultUserSession(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSessionJSON, ok := store.Get(userSessionStorerKey).([]byte)
|
|
|
|
|
|
|
|
// If userSession is not yet defined we create the new session with default values
|
|
|
|
// and save it in the store.
|
|
|
|
if !ok {
|
|
|
|
userSession := NewDefaultUserSession()
|
|
|
|
store.Set(userSessionStorerKey, userSession)
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return userSession, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var userSession UserSession
|
|
|
|
err = json.Unmarshal(userSessionJSON, &userSession)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return NewDefaultUserSession(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return userSession, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSession save the user session.
|
|
|
|
func (p *Provider) SaveSession(ctx *fasthttp.RequestCtx, userSession UserSession) error {
|
|
|
|
store, err := p.sessionHolder.Get(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSessionJSON, err := json.Marshal(userSession)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
store.Set(userSessionStorerKey, userSessionJSON)
|
2020-05-18 02:45:47 +00:00
|
|
|
|
2020-05-18 21:50:50 +00:00
|
|
|
err = p.sessionHolder.Save(ctx, store)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-05 19:35:32 +00:00
|
|
|
|
2019-04-24 21:52:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegenerateSession regenerate a session ID.
|
|
|
|
func (p *Provider) RegenerateSession(ctx *fasthttp.RequestCtx) error {
|
2020-05-18 02:45:47 +00:00
|
|
|
err := p.sessionHolder.Regenerate(ctx)
|
2019-04-24 21:52:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// DestroySession destroy a session ID and delete the cookie.
|
|
|
|
func (p *Provider) DestroySession(ctx *fasthttp.RequestCtx) error {
|
|
|
|
return p.sessionHolder.Destroy(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateExpiration update the expiration of the cookie and session.
|
|
|
|
func (p *Provider) UpdateExpiration(ctx *fasthttp.RequestCtx, expiration time.Duration) error {
|
|
|
|
store, err := p.sessionHolder.Get(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.SetExpiration(expiration)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-05-18 02:45:47 +00:00
|
|
|
return p.sessionHolder.Save(ctx, store)
|
2019-04-24 21:52:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetExpiration get the expiration of the current session.
|
|
|
|
func (p *Provider) GetExpiration(ctx *fasthttp.RequestCtx) (time.Duration, error) {
|
|
|
|
store, err := p.sessionHolder.Get(ctx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return time.Duration(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return store.GetExpiration(), nil
|
|
|
|
}
|