session

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 9, 2023 License: MIT Imports: 10 Imported by: 1

README

go-session Coverage Status

For now just experimenting with sessions in go

Documentation

Index

Constants

View Source
const (
	LogKeySID        = "session.sid"
	LogKeyRQID       = "session.rqud"
	LogKeyDebugError = "session.dbg_error"
)

Variables

View Source
var ErrSessionNotFound = errors.New("sessionservice: session not found")

Functions

func ValidateSessionID

func ValidateSessionID(sid string) error

ValidateSessionID validate session id format

Types

type Conf

type Conf struct {
	IdleTimeout time.Duration
	AbsTimout   time.Duration
}

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 CtxKey

type CtxKey string

CtxKey type alias for session data attributes keys

type SameSite

type SameSite int
const (
	SameSiteDefaultMode SameSite = iota + 1
	SameSiteLaxMode
	SameSiteStrictMode
	SameSiteNoneMode
)

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

func NewService(s Store, l logr.Logger, reqIDKey interface{}) Service

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

func NewSession() (Session, error)

NewSession return new session with default configuration IdleTimeout = 24h AbsTimeout = 7d Anonym = true Active = true Opts: Secure, HTTPOnly, Strict

func (*Session) AddAttribute

func (s *Session) AddAttribute(k string, v interface{})

AddAttribute add a new attribute to the session

func (*Session) GetAttribute

func (s *Session) GetAttribute(k string) (interface{}, bool)

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

func (s *Session) GetBool(k string) (v, ok bool)

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

func (s *Session) GetBoolSlice(k string) ([]bool, bool)

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

func (s *Session) GetFloat32(k string) (float32, bool)

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

func (s *Session) GetFloat32Slice(k string) ([]float32, bool)

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

func (s *Session) GetFloat64(k string) (float64, bool)

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

func (s *Session) GetFloat64Slice(k string) ([]float64, bool)

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

func (s *Session) GetInt(k string) (int, bool)

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

func (s *Session) GetInt32Slice(k string) ([]int32, bool)

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

func (s *Session) GetInt64(k string) (int64, bool)

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

func (s *Session) GetInt64Slice(k string) ([]int64, bool)

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

func (s *Session) GetSlice(k string) ([]interface{}, bool)

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

func (s *Session) GetString(k string) (string, bool)

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

func (s *Session) GetStringSlice(k string) ([]string, bool)

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

func (s *Session) GetStruct(k string, out interface{}) bool

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

func (s *Session) GetTime(k string) (time.Time, bool)

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

func (s *Session) GetTimeSlice(k string) ([]time.Time, bool)

GetTimeSlice return a value as []time.Time from the session by key if Value is []time.Time return (nil, false) otherwise

func (*Session) IsExpired

func (s *Session) IsExpired() bool

IsExpired check if session is expired

func (*Session) WithAttributes added in v0.1.2

func (s *Session) WithAttributes(attrs map[string]interface{})

func (*Session) WithCookieConf

func (s *Session) WithCookieConf(cc CookieConf)

WithCookieConf add cookie to the session

func (*Session) WithSessionConf

func (s *Session) WithSessionConf(sc Conf)

WithSessionConf configure session timeouts

func (*Session) WithUserID

func (s *Session) WithUserID(uid string)

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
}

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL