sessions

package
v0.0.0-...-3a46aa3 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: MIT, BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCookie

func NewCookie(name, value string, options *Options) *http.Cookie

NewCookie returns an http.Cookie with the options set. It also sets the Expires field calculated based on the MaxAge value, for Internet Explorer compatibility.

func Save

func Save(ht *khttp.Transport) error

Save saves all sessions used during the current request.

Types

type CookieStore

type CookieStore struct {
	Codecs  []securecookie.Codec
	Options *Options
}

CookieStore stores sessions using secure cookies.

func NewCookieStore

func NewCookieStore(keyPairs ...[]byte) *CookieStore

NewCookieStore returns a new CookieStore.

Keys are defined in pairs to allow key rotation, but the common case is to set a single authentication key and optionally an encryption key.

The first key in a pair is used for authentication and the second for encryption. The encryption key can be set to nil or omitted in the last pair, but the authentication key is required in all pairs.

It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes.

func (*CookieStore) Get

func (s *CookieStore) Get(ht *khttp.Transport, name string) (*Session, error)

Get returns a session for the given name after adding it to the registry.

It returns a new session if the sessions doesn't exist. Access IsNew on the session to check if it is an existing session or a new one.

It returns a new session and an error if the session exists but could not be decoded.

func (*CookieStore) MaxAge

func (s *CookieStore) MaxAge(age int)

MaxAge sets the maximum age for the store and the underlying cookie implementation. Individual sessions can be deleted by setting Options.MaxAge = -1 for that session.

func (*CookieStore) New

func (s *CookieStore) New(ht *khttp.Transport, name string) (*Session, error)

New returns a session for the given name without adding it to the registry.

The difference between New() and Get() is that calling New() twice will decode the session data twice, while Get() registers and reuses the same decoded session after the first call.

func (*CookieStore) Save

func (s *CookieStore) Save(ht *khttp.Transport, session *Session) error

Save adds a single session to the response.

type GobSerializer

type GobSerializer struct{}

GobSerializer uses gob package to encode the session map

func (GobSerializer) Deserialize

func (s GobSerializer) Deserialize(d []byte, ss *Session) error

func (GobSerializer) Serialize

func (s GobSerializer) Serialize(ss *Session) ([]byte, error)

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer encode the session map to JSON.

func (JSONSerializer) Deserialize

func (s JSONSerializer) Deserialize(d []byte, ss *Session) error

func (JSONSerializer) Serialize

func (s JSONSerializer) Serialize(ss *Session) ([]byte, error)

type MultiError

type MultiError []error

MultiError stores multiple errors.

Borrowed from the App Engine SDK.

func (MultiError) Error

func (m MultiError) Error() string

type Options

type Options struct {
	Path   string
	Domain string
	// MaxAge=0 means no Max-Age attribute specified and the cookie will be
	// deleted after the browser session ends.
	// MaxAge<0 means delete cookie immediately.
	// MaxAge>0 means Max-Age attribute present and given in seconds.
	MaxAge   int
	Secure   bool
	HttpOnly bool
	SameSite http.SameSite
}

Options stores configuration for a session or session store.

Fields are a subset of http.Cookie fields.

type RedisStore

type RedisStore struct {
	Codecs        []securecookie.Codec
	Options       *Options // default configuration
	DefaultMaxAge int      // default Redis TTL for a MaxAge == 0 session
	// contains filtered or unexported fields
}

RedisStore stores sessions in a redis backend.

func NewRedisStore

func NewRedisStore(rdCmd redis.Cmdable, keyPairs ...[]byte) (*RedisStore, error)

func (*RedisStore) Get

func (s *RedisStore) Get(ht *khttp.Transport, name string) (*Session, error)

func (*RedisStore) New

func (s *RedisStore) New(ht *khttp.Transport, name string) (*Session, error)

func (*RedisStore) Save

func (s *RedisStore) Save(ht *khttp.Transport, session *Session) error

func (*RedisStore) SetKeyPrefix

func (s *RedisStore) SetKeyPrefix(p string)

SetKeyPrefix set the prefix

func (*RedisStore) SetMaxAge

func (s *RedisStore) SetMaxAge(v int)

func (*RedisStore) SetMaxLength

func (s *RedisStore) SetMaxLength(l int)

func (*RedisStore) SetSerializer

func (s *RedisStore) SetSerializer(ss SessionSerializer)

SetSerializer sets the serializer

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry stores sessions used during a request.

func GetRegistry

func GetRegistry(ht *khttp.Transport) *Registry

GetRegistry returns a registry instance for the current request.

func (*Registry) Get

func (s *Registry) Get(store Store, name string) (session *Session, err error)

Get registers and returns a session for the given name and session store.

It returns a new session if there are no sessions registered for the name.

func (*Registry) Save

func (s *Registry) Save(ht *khttp.Transport) error

Save saves all sessions registered for the current request.

type Session

type Session struct {
	ID      string
	Values  map[interface{}]interface{}
	Options *Options
	IsNew   bool
	// contains filtered or unexported fields
}

Session stores the values and optional configuration for a session.

func NewSession

func NewSession(store Store, name string) *Session

NewSession is called by session stores to create a new session instance.

func (*Session) AddFlash

func (s *Session) AddFlash(value interface{}, vars ...string)

AddFlash adds a flash message to the session.

A single variadic argument is accepted, and it is optional: it defines the flash key. If not defined "_flash" is used by default.

func (*Session) Flashes

func (s *Session) Flashes(vars ...string) []interface{}

Flashes returns a slice of flash messages from the session.

A single variadic argument is accepted, and it is optional: it defines the flash key. If not defined "_flash" is used by default.

func (*Session) Name

func (s *Session) Name() string

Name returns the name used to register the session.

func (*Session) Save

func (s *Session) Save(ht *khttp.Transport) error

Save is a convenience method to save this session. It is the same as calling store.Save(request, response, session). You should call Save before writing to the response or returning from the handler.

func (*Session) Store

func (s *Session) Store() Store

Store returns the session store used to register the session.

type SessionSerializer

type SessionSerializer interface {
	Deserialize(d []byte, ss *Session) error
	Serialize(ss *Session) ([]byte, error)
}

SessionSerializer provides an interface hook for alternative serializers

type Store

type Store interface {
	Get(ht *khttp.Transport, name string) (*Session, error)
	New(ht *khttp.Transport, name string) (*Session, error)
	Save(ht *khttp.Transport, s *Session) error
}

Jump to

Keyboard shortcuts

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