session

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(cfg *Config) error

Initialize a session store from the configuration

func RegisterStore

func RegisterStore(store Store)

Register a new session store

Types

type Config

type Config struct {
	// Store indicates which data store to use to hold the sessions.
	// Available built in stores are "db", "redis", "mem"
	Store string `config:"platform.server.session.store" default:"redis"`
	// Enable the use of sessions
	Enabled bool `config:"platform.server.session.enabled" default:"yes"`
	// UseCookie will enable client sessions through cookies
	UseCookie bool `config:"platform.server.session.useCookie" default:"yes"`
	// CookieName for the session cookie
	CookieName string `config:"platform.server.session.cookieName" default:"_session"`
	// HeaderName of the header that will contain the session id
	HeaderName string `config:"platform.server.session.headerName" default:"X-Session-Id"`
	// TTL is the maximum inactivity of a session till it gets removed
	TTL time.Duration `config:"platform.server.session.ttl" default:"1h"`
}

type Session

type Session struct {
	// Unique session id
	ID Token `json:"sessionid" bson:"_id"`
	// Map of values stored on the session
	Data map[string]interface{} `json:"data" bson:"data"`
	// LastActivity Last time the session was used
	LastActivity time.Time `json:"lastactivity" bson:"lastActivity"`
	// ip of user
	IP string `json:"ip" bson:"ip"`
	// CSRF token
	CSRFToken string `json:"csrf" bson:"CSRF"`
	// User agent of the session holder
	UA string `json:"ua" bson:"UA"`
	// Date when the session was created
	Created time.Time `json:"created" bson:"created"`
	// Persistent flag will keep the sessions alive for a longer period of time
	Persistent bool `json:"persistent" bson:"persistent"`
	// Name oif the user
	Name *string `json:"name" bson:"name"`
	// Account id if linked with account
	AccountID *string `json:"accountid" bson:"accountId"`
	// Account id if linked with account
	ImpersonateAccountID *int `json:"impersonateAccountid" bson:"impersonateAccountid"`
	// Unique device id
	DeviceID *string `json:"deviceid" bson:"deviceId"`
	// Session can be locked for various reasons
	Locked bool `json:"locked" bson:"locked"`
	// List of user permissions
	Permissions *platform.Permissions `json:"permissions" bson:"permissions"`
}

User session

func New

func New(r *http.Request) *Session

Creates a new session based on the user request

func Restore

func Restore(token Token) *Session

func (*Session) Destroy

func (s *Session) Destroy()

func (*Session) GetData

func (s *Session) GetData(key string) interface{}

func (*Session) Set

func (s *Session) Set()

func (*Session) SetData

func (s *Session) SetData(key string, val interface{})

type Store

type Store interface {
	// Store factory
	New() error
	// Session store name
	Type() string
	// Stores a session in the store
	Set(*Session) error
	// Retrieves a session from the store
	Get(Token) *Session
	// Removes a session from the store
	Del(Token) error
	// List all available sessions.
	// If argument is provided, it will return only sessions that match the account
	List(*string) []*Session
	// Removes all expired sessions
	GC()
	// Closes the store
	Close()
}

Interface of a session store

type Token

type Token string

func (Token) Valid

func (t Token) Valid() bool

Valid returns true if the session token is valid

Jump to

Keyboard shortcuts

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