2023-01-12 10:57:44 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
"github.com/fasthttp/session/v2"
|
2023-01-12 10:57:44 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Session a session provider.
|
|
|
|
type Session struct {
|
2023-01-26 02:23:47 +00:00
|
|
|
Config schema.SessionCookie
|
2023-01-12 10:57:44 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
sessionHolder *session.Session
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultUserSession returns a new default UserSession for this session provider.
|
|
|
|
func (p *Session) NewDefaultUserSession() (userSession UserSession) {
|
|
|
|
userSession = NewDefaultUserSession()
|
|
|
|
|
|
|
|
userSession.CookieDomain = p.Config.Domain
|
|
|
|
|
|
|
|
return userSession
|
2023-01-12 10:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSession return the user session from a request.
|
2023-01-25 09:36:40 +00:00
|
|
|
func (p *Session) GetSession(ctx *fasthttp.RequestCtx) (userSession UserSession, err error) {
|
|
|
|
var store *session.Store
|
2023-01-12 10:57:44 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if store, err = p.sessionHolder.Get(ctx); err != nil {
|
|
|
|
return p.NewDefaultUserSession(), err
|
2023-01-12 10:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-01-25 09:36:40 +00:00
|
|
|
userSession = p.NewDefaultUserSession()
|
2023-01-12 10:57:44 +00:00
|
|
|
|
|
|
|
store.Set(userSessionStorerKey, userSession)
|
|
|
|
|
|
|
|
return userSession, nil
|
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if err = json.Unmarshal(userSessionJSON, &userSession); err != nil {
|
|
|
|
return p.NewDefaultUserSession(), err
|
2023-01-12 10:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return userSession, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSession save the user session.
|
2023-01-25 09:36:40 +00:00
|
|
|
func (p *Session) SaveSession(ctx *fasthttp.RequestCtx, userSession UserSession) (err error) {
|
|
|
|
var (
|
|
|
|
store *session.Store
|
|
|
|
userSessionJSON []byte
|
|
|
|
)
|
2023-01-12 10:57:44 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if store, err = p.sessionHolder.Get(ctx); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if userSessionJSON, err = json.Marshal(userSession); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
store.Set(userSessionStorerKey, userSessionJSON)
|
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if err = p.sessionHolder.Save(ctx, store); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegenerateSession regenerate a session ID.
|
|
|
|
func (p *Session) RegenerateSession(ctx *fasthttp.RequestCtx) error {
|
2023-01-25 09:36:40 +00:00
|
|
|
return p.sessionHolder.Regenerate(ctx)
|
2023-01-12 10:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DestroySession destroy a session ID and delete the cookie.
|
|
|
|
func (p *Session) DestroySession(ctx *fasthttp.RequestCtx) error {
|
|
|
|
return p.sessionHolder.Destroy(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateExpiration update the expiration of the cookie and session.
|
2023-01-25 09:36:40 +00:00
|
|
|
func (p *Session) UpdateExpiration(ctx *fasthttp.RequestCtx, expiration time.Duration) (err error) {
|
|
|
|
var store *session.Store
|
2023-01-12 10:57:44 +00:00
|
|
|
|
2023-01-25 09:36:40 +00:00
|
|
|
if store, err = p.sessionHolder.Get(ctx); err != nil {
|
2023-01-12 10:57:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.SetExpiration(expiration)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return p.sessionHolder.Save(ctx, store)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetExpiration get the expiration of the current session.
|
|
|
|
func (p *Session) 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
|
|
|
|
}
|