session

package
v0.0.0-...-4c4b7df Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package session provides simple server side session handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessMiddleware

func AccessMiddleware(next http.Handler, redirect RedirectFunc) http.Handler

Middleware to check if session is set. Pass the next http.Handler to be called and a redirect function, which is called when session is not set. If you pass nil for the redirect function, it will return nothing to the client.

func Middleware

func Middleware(next HandlerFunc) http.Handler

Middleware to inject current session.

func New

func New(cookieName string, maxLifetime int, provider Provider) error

Creates a new session manager with given cookie name and max lifetime in seconds. The cookie name must not be an emtpy string, the lifetime must be greater than 0 and a provider must be passed. If not, an error will be returned.

Types

type HandlerFunc

type HandlerFunc func(Session, http.ResponseWriter, *http.Request)

Handler function with session injected.

type MemProvider

type MemProvider struct {
	Provider
	// contains filtered or unexported fields
}

MemProvider provides a in memory solution to store sessions. Use the NewMemProvider() function to create a new instance and pass it to Manager. The methods should not be called manually!

func NewMemProvider

func NewMemProvider() *MemProvider

Creates a new in memory provider for the session manager.

func (*MemProvider) Destroy

func (p *MemProvider) Destroy(session *Session) error

Removes a session from memory.

func (*MemProvider) GC

func (p *MemProvider) GC()

Iterates over all existing sessions in memory and removes sessions which exceeded their lifetime.

func (*MemProvider) Init

func (p *MemProvider) Init(token string, lifetime time.Time) (Session, error)

Stores a new session in memory with given token and lifetime.

func (*MemProvider) Read

func (p *MemProvider) Read(token string) (Session, error)

Reads a session from memory by given token.

func (*MemProvider) Write

func (p *MemProvider) Write(session *Session) error

Updates a session in memory.

type Provider

type Provider interface {
	Init(string, time.Time) (Session, error)
	Read(string) (Session, error)
	Write(*Session) error
	Destroy(*Session) error
	GC()
}

A provider abstracts the session backend used by the manager, so that different kinds of storages can be used. For an example see MemProvider.

type RedirectFunc

type RedirectFunc func(http.ResponseWriter, *http.Request) bool

Redirect function in case the session is not set. If true is returned, the middleware will continue calling the next http.Handler.

type Session

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

Session provides access to user context related data. Generic data can be set and obtained and will be available on page switch. Call Save() to make sure the data is stored. A session must be initialized by the session manager, to make sure it can be used later.

func GetCurrentSession

func GetCurrentSession(r *http.Request) (Session, error)

Returns the session for the http.Request. If not found, an empty session and an error will be returned.

func GetSession

func GetSession(token string) (Session, error)

Returns a session by session token if found. If not, an empty session and an error will be returned.

func NewSession

func NewSession(w http.ResponseWriter, r *http.Request) (Session, error)

Creates a new session and returns it. If the http.ResponseWriter or http.Request is not set, the session token won't be stored in cookie. So to create a new session without using http, call it with both nil as parameters.

func (*Session) Active

func (s *Session) Active() bool

Returns true when this session is active.

func (*Session) Destroy

func (s *Session) Destroy(w http.ResponseWriter, r *http.Request) error

Destroys and invalidates the session. http.ResponseWriter and http.Request are optional, if both are set, the session cookie will be destroyed (which is recommended).

func (*Session) Get

func (s *Session) Get(key string, value interface{}) error

Stores session variable for given key within given variable. When the variable cannot be found, is not assignable (not a pointer) or is nil, an error will be returned. If the type of of the variable found does not match the type of the variable it will be stored in, this method will panic.

func (*Session) Remove

func (s *Session) Remove(key string)

Removes session variable for given key. Use the Save() method to remove it persistently.

func (*Session) Renew

func (s *Session) Renew() error

Resets the lifetime of this session.

func (*Session) Save

func (s *Session) Save() error

Saves the session. Call when data was changed using the Set() method to make it available on next call.

func (*Session) Set

func (s *Session) Set(key string, value interface{}) error

Sets session variable for given key. If a variable is stored for that key already, it will be replaced. Use the Save() method to store it for later usage and obtain it with Get().

func (*Session) Token

func (s *Session) Token() string

Returns the session token, which is a unique base 64 URL encoded string.

Jump to

Keyboard shortcuts

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