session

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: MIT Imports: 7 Imported by: 0

README

Session Middleware

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCannotFetch = errors.New("cannot fetch session, check if there is a session.Manager configured")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Key        string        // session cookie key (required)
	Path       string        // see http.Cookie
	Domain     string        // see http.Cookie
	Expires    time.Time     // see http.Cookie
	RawExpires string        // see http.Cookie
	MaxAge     int           // see http.Cookie
	Secure     bool          // see http.Cookie
	HttpOnly   bool          // see http.Cookie
	SameSite   http.SameSite // see http.Cookie
	Raw        string        // see http.Cookie
	Unparsed   []string      // see http.Cookie
}

Config cookie store expects conn.secret_key_base to be set

type Cookie struct {
	Log               string           // Log level to use when the cookie cannot be decoded. Defaults to `debug`, can be set to false to disable it.
	Serializer        chain.Serializer // cookie serializer module that defines `Encode(any)` and `Decode(any)`. Defaults to `json`.
	SigningKeyring    *crypto.Keyring  // a crypto.Keyring used with for signing/verifying a cookie.
	EncryptionKeyring *crypto.Keyring  // a crypto.Keyring used for encrypting/decrypting a cookie.
	EncryptionAAD     []byte           // Additional authenticated data (AAD)
}

Cookie Stores the session in a cookie. https://edgeapi.rubyonrails.org/classes/ActionDispatch/Session/CookieStore.html https://funcptr.net/2013/08/25/user-sessions,-what-data-should-be-stored-where-/

func (*Cookie) Delete

func (c *Cookie) Delete(ctx *chain.Context, sid string)

func (*Cookie) Get

func (c *Cookie) Get(ctx *chain.Context, rawCookie string) (sid string, data map[string]any)

func (*Cookie) Init

func (c *Cookie) Init(config Config, router *chain.Router) (err error)

func (*Cookie) Name

func (c *Cookie) Name() string

func (*Cookie) Put

func (c *Cookie) Put(ctx *chain.Context, sid string, data map[string]any) (rawCookie string, err error)

type Manager

type Manager struct {
	Config
	Store Store // session store module (required)
}

Manager cookie store expects conn.secret_key_base to be set

func (*Manager) Handle

func (m *Manager) Handle(ctx *chain.Context, next func() error) error

func (*Manager) Init

func (m *Manager) Init(method string, path string, router *chain.Router)

type Session

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

func Fetch

func Fetch(ctx *chain.Context) (*Session, error)

Fetch LazyLoad session from context. It only returns result if there is a global session.Manager configured

func FetchByKey

func FetchByKey(ctx *chain.Context, key string) (*Session, error)

FetchByKey LazyLoad session from context using a session.Manager Key

func (*Session) Clear

func (s *Session) Clear()

Clear Clears the entire session.

This function removes every key from the session, clearing the session.

Note that, even if Clear is used, the session is still sent to the client. If the session should be effectively *dropped*, Destroy should be used.

func (*Session) Delete

func (s *Session) Delete(key string)

Delete Deletes `key` from session.

func (*Session) Destroy

func (s *Session) Destroy()

Destroy drops the session, a session cookie will not be included in the response

func (*Session) Exist

func (s *Session) Exist(key string) (exist bool)

Exist checks if the value exists in the data for that session

func (*Session) Get

func (s *Session) Get(key string) any

Get Returns session value for the given `key`. If `key` is not set, `nil` is returned.

func (*Session) GetMap

func (s *Session) GetMap() map[string]any

GetMap Returns the whole session.

func (*Session) IgnoreChanges

func (s *Session) IgnoreChanges()

IgnoreChanges ignores all changes made to the session in this request cycle

func (*Session) Put

func (s *Session) Put(key string, value any)

Put puts the specified `value` in the session for the given `key`.

func (*Session) Renew

func (s *Session) Renew()

Renew generates a new session id for the cookie

type Store

type Store interface {
	Name() string

	// Init Initializes the store.
	Init(config Config, router *chain.Router) error

	// Get Parses the given cookie.
	//
	// Returns a session id and the session contents. The session id is any value that can be used to identify the
	// session by the store.
	//
	// The session id may be nil in case the cookie does not identify any value in the store. The session contents must
	// be a map.
	Get(ctx *chain.Context, rawCookie string) (sid string, data map[string]any)

	// Put  Stores the session associated with given session id.
	//
	// If an empty string is given as sid, a new session id should be generated and returned.
	Put(ctx *chain.Context, sid string, data map[string]any) (rawCookie string, err error)

	// Delete Removes the session associated with given session id from the store.
	Delete(ctx *chain.Context, sid string)
}

Store Specification for session stores.

Jump to

Keyboard shortcuts

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