session

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2021 License: MIT Imports: 7 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// Default Flash Class
	FlashError   = "error"
	FlashInfo    = "info"
	FlashSuccess = "success"
	FlashWarning = "warning"

	// Default Flash Msg
	BadCredsMsg      = "Hmm... check those credentials."
	BadInputMsg      = "Hmm... check your form, something isn't correct."
	DefaultErrMsg    = "Uh oh! We've run into an issue."
	EmailNotValidMsg = "It looks like your primary email has not been validated. Please complete this process and try again."
	LinkSentMsg      = "Email sent! Please open the link in your email to reset your password."
	NoAccessMsg      = "Oops, sending you back somewhere safe."
)

Variables

View Source
var (
	ErrFailedConfig = errors.New("failed config")
	ErrNotValid     = errors.New("not valid")
	ErrNoUser       = errors.New("no user")
)
View Source
var ContactUsErr = DefaultErrMsg + " Please contact us at %s if the issue persists."

Functions

func WithCookie

func WithCookie() func(*Service) error

WithCookie configures the Service to back session storage with cookies.

func WithMaxAge

func WithMaxAge(secs int) func(*Service) error

WithMaxAge sets the time-to-live of a session.

Call before other options so this value is available.

Otherwise, the Service uses defaultMaxAge.

func WithRedis

func WithRedis(uri, pass string) func(*Service) error

WithRedis configures the Service to back session storage with Redis.

To authenticate to the Redis server, provide pass, otherwise its zero-value is acceptable.

Types

type Flash

type Flash struct {
	Class string `json:"class"`
	Msg   string `json:"msg"`
}

A Flash is a structured message set in a session.

type FlashSessionable

type FlashSessionable interface {
	ClearFlashes(w http.ResponseWriter, r *http.Request)
	Flashes(w http.ResponseWriter, r *http.Request) []Flash
	SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error
}

FlashSessionable defines the behavior of a session that includes flashes in it.

type Service

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

A Service wraps a gorilla.Store to manage constructing a new one and accessing the sessions contained in it.

Service implements SessionStorer.

func NewStoreService

func NewStoreService(env, authKey, encryptKey string, opts ...ServiceOpt) (Service, error)

NewStoreService initiates a data store for user web sessions with the provided hex-encoded authentication key and encryption keys. If no backing storage is provided through a functional option - like WithRedis - NewService stores sessions in cookies.

func (Service) GetSession

func (s Service) GetSession(r *http.Request) (Sessionable, error)

GetSession wraps gorilla.Get, creating a brand new Session or one from the session retrieved.

type ServiceOpt

type ServiceOpt func(*Service) error

A ServiceOpt configures the provided *Service, returning an error if unable to.

type Session

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

A Session provides all functionality for managing a fully featured session.

Its functionality is implemented by lightly wrapping a gorilla.Session.

TODO(dlk): embed *gorilla.Session anonymously? do not export?

func (Session) ClearFlashes

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

func (Session) Delete

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

Delete removes a session by making the MaxAge negative.

func (Session) DeregisterUser

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

DeregisterUser removes the User from the session.

func (Session) Flashes

func (s Session) Flashes(w http.ResponseWriter, r *http.Request) []Flash

Flashes retrieves []Flash stored in the session.

func (Session) Get

func (s Session) Get(key string) interface{}

Get retrieves a value from the session according to the key passed in.

func (Session) RegisterUser

func (s Session) RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error

RegisterUserSession stores the user's ID in the session.

func (Session) ResetExpiry

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

ResetExpiry resets the expiration of the session by saving it.

func (Session) Save

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

Save wraps gorilla.Session.Save, saving the session in the request.

func (Session) Set

func (s Session) Set(w http.ResponseWriter, r *http.Request, key string, val interface{}) error

Set stores a value according to the key passed in on the session.

func (Session) SetFlash

func (s Session) SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error

SetFlash stores the passed in Flash in the session.

func (Session) UserID

func (s Session) UserID() (uint, error)

UserID gets the user ID out of the session. A user ID should be present in a session if the user is successfully authenticated. If no user ID can be found, this ErrNoUser is returned. This ought to only happen when a user is going through an authentication workflow or hitting unauthenticated pages.

If the value returned from the session is not a uint, ErrNotValid is returned and represents a programming error.

type SessionStorer

type SessionStorer interface {
	GetSession(r *http.Request) (Sessionable, error) // TODO(dlk): Sessionable or TrailsSessionable?
}

The SessionStorer defines methods for interacting with a Sessionable for the given *http.Request.

type Sessionable

type Sessionable interface {
	Delete(w http.ResponseWriter, r *http.Request) error
	Get(key string) interface{}
	ResetExpiry(w http.ResponseWriter, r *http.Request) error
	Save(w http.ResponseWriter, r *http.Request) error
	Set(w http.ResponseWriter, r *http.Request, key string, val interface{}) error
}

The Sessionable wraps methods for basic adding values to, deleting, and getting values from a session associated with an *http.Request and saving those to the session store.

type Stub

type Stub struct{}

func (Stub) ClearFlashes

func (s Stub) ClearFlashes(w http.ResponseWriter, r *http.Request)

func (Stub) Delete

func (s Stub) Delete(w http.ResponseWriter, r *http.Request) error

func (Stub) DeregisterUser

func (s Stub) DeregisterUser(w http.ResponseWriter, r *http.Request) error

func (Stub) Flashes

func (s Stub) Flashes(w http.ResponseWriter, r *http.Request) []Flash

func (Stub) Get

func (s Stub) Get(key string) interface{}

func (Stub) RegisterUser

func (s Stub) RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error

func (Stub) ResetExpiry

func (s Stub) ResetExpiry(w http.ResponseWriter, r *http.Request) error

func (Stub) Save

func (s Stub) Save(w http.ResponseWriter, r *http.Request) error

func (Stub) Set

func (s Stub) Set(w http.ResponseWriter, r *http.Request, key string, val interface{}) error

func (Stub) SetFlash

func (s Stub) SetFlash(w http.ResponseWriter, r *http.Request, flash Flash) error

func (Stub) UserID

func (s Stub) UserID() (uint, error)

type TrailsSessionable

type TrailsSessionable interface {
	FlashSessionable
	Sessionable
	UserSessionable
}

The TrailsSessionable composes session's major interfaces.

func NewSession

func NewSession(g *gorilla.Session) TrailsSessionable

NewSession constructs a new Session as an implementation of TrailsSessionable from an interface value that is a *gorilla.Session. If it isn't, ErrNotValid returns.

Typical usage is to pass in the value retrieved from a http.Request.Context. Given context keys are unexported, this package cannot perform that retrieval.

type UserSessionable

type UserSessionable interface {
	DeregisterUser(w http.ResponseWriter, r *http.Request) error
	RegisterUser(w http.ResponseWriter, r *http.Request, ID uint) error
	UserID() (uint, error)
}

The UserSessionable wraps methods for adding, removing, and retrieving user IDs from a session.

Jump to

Keyboard shortcuts

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