session

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2023 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package session implements routines and interfaces for handling users session (creating, killing, authorizing).

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRenewers = errors.New("session: you haven't provided any renewers")

ErrNoRenewers is returned by Renewer composed by RenewerComposite when there are no renewers to run.

Functions

func Guard

func Guard(renewer Renewer) func(http.Handler) http.Handler

Guard returns http middleware which guards from clients accessing given handler without valid session.

func WithOptions

func WithOptions(ctx context.Context, state State, args WithOptionsArguments) error

WithOptions applies options to given state and saves it. It does nothing more thatn regular Saver if you won't provide options.

Types

type Builder

type Builder struct {
	UserID string

	Nickname string

	Values map[string]interface{}
}

Builder contains arguments and dependencies for building new session for user with given ID.

type Killer

type Killer interface {
	// Kill is method for purging session.
	Kill(context.Context, http.ResponseWriter) error
}

Killer purges session. It can make given session implementation expired.

type Option

type Option func(*State)

type Renewer

type Renewer interface {
	// Renew is method for restoring session from
	// provided request data from client.
	Renew(*http.Request) (*State, error)
}

Renewer retrieves session data from http request.

func RenewerComposite

func RenewerComposite(renewers ...Renewer) Renewer

RenewerComposite compose multiple Renewers into single one. Composed renewer will run each renewer till the first will successfully return any State.

type Saver

type Saver interface {
	// Save is method for returning session data or
	// session identifier to client.
	Save(context.Context, http.ResponseWriter, State) error
}

Saver saves data in database and returns public information about session to client.

type State

type State struct {
	// ID is unique identifier of single session.
	ID string

	// UserID is user ID of session's owner.
	UserID string

	// Nickname is user name of session's owner.
	Nickname string

	// Values are key/value storage for additional
	// session data.
	Values map[string]interface{}
}

State holds common data for storing in single user session.

func New

func New(ctx context.Context, b Builder) *State

New returns fresh session for given user and saves it in database. You can output returned session to user after tokenizing it.

type WithOptionsArguments

type WithOptionsArguments struct {
	Saver   Saver
	Writer  http.ResponseWriter
	Options []Option
}

WithOptionsArguments holds arguments for WithOptions function. All arguments are required.

Jump to

Keyboard shortcuts

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