Documentation ¶
Index ¶
- Constants
- Variables
- func ValidateSessionID(sid string) error
- type Conf
- type CookieConf
- type CtxKey
- type SameSite
- type Service
- type Session
- func (s *Session) AddAttribute(k string, v interface{})
- func (s *Session) GetAttribute(k string) (interface{}, bool)
- func (s *Session) GetBool(k string) (v, ok bool)
- func (s *Session) GetBoolSlice(k string) ([]bool, bool)
- func (s *Session) GetFloat32(k string) (float32, bool)
- func (s *Session) GetFloat32Slice(k string) ([]float32, bool)
- func (s *Session) GetFloat64(k string) (float64, bool)
- func (s *Session) GetFloat64Slice(k string) ([]float64, bool)
- func (s *Session) GetInt(k string) (int, bool)
- func (s *Session) GetInt32Slice(k string) ([]int32, bool)
- func (s *Session) GetInt64(k string) (int64, bool)
- func (s *Session) GetInt64Slice(k string) ([]int64, bool)
- func (s *Session) GetSlice(k string) ([]interface{}, bool)
- func (s *Session) GetString(k string) (string, bool)
- func (s *Session) GetStringSlice(k string) ([]string, bool)
- func (s *Session) GetStruct(k string, out interface{}) bool
- func (s *Session) GetStructWithDecoder(k string, decoder *mapstructure.Decoder) bool
- func (s *Session) GetTime(k string) (time.Time, bool)
- func (s *Session) GetTimeSlice(k string) ([]time.Time, bool)
- func (s *Session) IsExpired() bool
- func (s *Session) WithAttributes(attrs map[string]interface{})
- func (s *Session) WithCookieConf(cc CookieConf)
- func (s *Session) WithSessionConf(sc Conf)
- func (s *Session) WithUserID(uid string)
- type Store
Constants ¶
const ( LogKeySID = "session.sid" LogKeyRQID = "session.rqud" LogKeyDebugError = "session.dbg_error" )
Variables ¶
var ErrSessionNotFound = errors.New("sessionservice: session not found")
Functions ¶
func ValidateSessionID ¶
ValidateSessionID validate session id format
Types ¶
type Conf ¶
Conf contains session parameters
func DefaultSessionConf ¶
func DefaultSessionConf() Conf
type CookieConf ¶
type CookieConf struct { Path string Domain string Secure bool HTTPOnly bool MaxAge int SameSite SameSite }
CookieConf contains cookie parameters
func DefaultCookieConf ¶
func DefaultCookieConf() CookieConf
type Service ¶
type Service interface { CreateAnonymSession(ctx context.Context, cc CookieConf, sc Conf, keyAndValues ...interface{}) (*Session, error) CreateUserSession(ctx context.Context, uid string, cc CookieConf, sc Conf, keyAndValues ...interface{}) (*Session, error) LoadSession(ctx context.Context, sid string) (*Session, error) InvalidateSession(ctx context.Context, sid string) error AddAttributes(ctx context.Context, sid string, keyAndValues ...interface{}) (*Session, error) RemoveAttributes(ctx context.Context, sid string, keys ...string) (*Session, error) }
func NewService ¶
NewService Create implementation of Service to work with session
logr.Logger may be useful only for debugging purposes, Nnone of errors will be logged as logr.Error. It's up to service that call these methods to decide what is really error in terms of application and what is not.
reqIDKey is key to extract request id from the context
type Session ¶
type Session struct { ID string Data map[string]interface{} Opts CookieConf Anonym bool Active bool UID string IdleTimeout time.Duration AbsTimeout time.Duration LastAccessedAt time.Time CreatedAt time.Time }
Session is representation of session in terms of current module. Data - should be used to store any data within a session.
IdleTimeout and LastAccessedAt will be used to expire session based on user activity within the session.
AbsTimeout and CreatedAt will be used to expire session based on full session lifetime.
UID is supposed to store user identity who session belongs to.
Anonym is supposed to use during authentication process.
func NewSession ¶
NewSession return new session with default configuration IdleTimeout = 24h AbsTimeout = 7d Anonym = true Active = true Opts: Secure, HTTPOnly, Strict
func (*Session) AddAttribute ¶
AddAttribute add a new attribute to the session
func (*Session) GetAttribute ¶
GetAttribute return a value from the session It return nill and false if attribute doesn't exists
func (*Session) GetBool ¶ added in v0.3.1
GetBool return a value as bool from the session by key if Value is bool return (false, false) otherwise
func (*Session) GetBoolSlice ¶ added in v0.3.1
GetBoolSlice return a value as []bool from the session by key if Value is []bool return (nil, false) otherwise
func (*Session) GetFloat32 ¶ added in v0.3.1
GetFloat32 return a value as float32 from the session by key if Value is float32 return (0.0, false) otherwise
func (*Session) GetFloat32Slice ¶ added in v0.3.1
GetFloat32Slice return a value as []float32 from the session by key if Value is []float32 return (nil, false) otherwise
func (*Session) GetFloat64 ¶ added in v0.3.1
GetFloat64 return a value as float64 from the session by key if Value is one of (float32, float64) return (0.0, false) otherwise
func (*Session) GetFloat64Slice ¶ added in v0.3.1
GetFloat64Slice return a value as []float64 from the session by key if Value is []float64 (it won't be converted to []float64, if slice has type float32) return (nil, false) otherwise
func (*Session) GetInt ¶ added in v0.3.1
GetInt return a value as int from the session by key if Value one of (byte/int8, int16, int32, int64, int) return (0, false) otherwise
func (*Session) GetInt32Slice ¶ added in v0.3.1
GetInt32Slice return a value as []int32 from the session by key if Value is []int32 (it won't be converted to []int32, if slice has type of another int) return (nil, false) otherwise
func (*Session) GetInt64 ¶ added in v0.3.1
GetInt64 return a value as int64 from the session by key if Value one of (byte/int8, int16, int32, int64, int) return (0, false) otherwise
func (*Session) GetInt64Slice ¶ added in v0.3.1
GetInt64Slice return a value as []int64 from the session by key if Value is []int64 (it won't be converted to []int64, if slice has type of another int) return (nil, false) otherwise
func (*Session) GetSlice ¶ added in v0.3.1
GetSlice return a value as []interface{} from the session by key if Value is slice return (nil, false) otherwise
func (*Session) GetString ¶ added in v0.3.1
GetString return a value as string from the session by key If value isn't a string, it won't be converted
func (*Session) GetStringSlice ¶ added in v0.3.1
GetStringSlice return a value as []string from the session by key if Value is []string return (nil, false) otherwise
func (*Session) GetStruct ¶ added in v0.3.1
GetStruct convert map or struct from the session by key and write result to out return (false otherwise
to convert value from session to a struct is using https://pkg.go.dev/github.com/mitchellh/mapstructure underneath so, other its features like tags can be used here
func (*Session) GetStructWithDecoder ¶ added in v0.3.1
func (s *Session) GetStructWithDecoder(k string, decoder *mapstructure.Decoder) bool
GetStructWithDecoder convert map or struct from the session by key with custom mapstructure.Decoder (https://pkg.go.dev/github.com/mitchellh/mapstructure) return false otherwise
func (*Session) GetTime ¶ added in v0.3.1
GetTime return a value as time.Time from the session by key if Value is time.Time return (time.Time{}, false) otherwise
func (*Session) GetTimeSlice ¶ added in v0.3.1
GetTimeSlice return a value as []time.Time from the session by key if Value is []time.Time return (nil, false) otherwise
func (*Session) WithAttributes ¶ added in v0.1.2
func (*Session) WithCookieConf ¶
func (s *Session) WithCookieConf(cc CookieConf)
WithCookieConf add cookie to the session
func (*Session) WithSessionConf ¶
WithSessionConf configure session timeouts
func (*Session) WithUserID ¶
WithUserID add user identity to the session
type Store ¶
type Store interface { // Save store session and return its updated copy Save(ctx context.Context, s *Session) (*Session, error) // Save session attributes and return updated copy of session AddAttributes(ctx context.Context, sid string, data map[string]interface{}) (*Session, error) // Remove session attributes and return updated copy of session RemoveAttributes(ctx context.Context, sid string, keys ...string) (*Session, error) // Load session by its id Load(ctx context.Context, sid string) (*Session, error) // Invalidate session by its id Invalidate(ctx context.Context, sid string) error }