session
v0.0.0-20240711162211-159a3375b30a
Documentation
pkg.go.dev ↗Variables
ErrNoID (2 declarations)
var (
// ErrNoID indicates that no identifying information was provided
ErrNoID = errors.New("session: id missing")
// ErrExpired indicates any referenced session has since expired
ErrExpired = errors.New("session: expired")
)
Functions
ContextMiddleware
func ContextMiddleware(man HTTPReader) http.Middleware
ContextMiddleware applies *session.T to http.Request.Context
ContextMiddlewareRequired
func ContextMiddlewareRequired(man HTTPReader) http.Middleware
ContextMiddlewareRequired requires *session.T for http.Request.Context
ContextMiddlewareRequiredNot
func ContextMiddlewareRequiredNot(man HTTPReader) http.Middleware
ContextMiddlewareRequiredNot requires *session.T=nil for http.Request.Context
NewContext
func NewContext(ctx context.Context, session *T) context.Context
NewContext returns a new Context that carries *session.T
set session=nil to prevent repeated lookups for anonymous connections
Types
HTTPReader
type HTTPReader interface {
// ReadHTTP returns Session by *http.Request
ReadHTTP(*http.Request) (*T, error)
}
HTTPReader is an interface for recognizing Sessions in http.Request
HTTPReaderFunc
type HTTPReaderFunc func(*http.Request) (*T, error)
HTTPReaderFunc.ReadHTTP
func (f HTTPReaderFunc) ReadHTTP(r *http.Request) (*T, error)
HTTPWriter
type HTTPWriter interface {
// WriteHTTP writes the Set-Cookie header in http.Writer
WriteHTTP(http.Writer, *T)
}
HTTPWriter is an interface for writing Sessions to http.Writer
Manager
type Manager interface {
HTTPReader
HTTPWriter
// Size returns the current len of the map
Size() int
// Get returns a Session by ID
Get(id string) *T
// Must refreshes and returns the Session with the given username if one exists, and creates one if necessary
Must(name string) *T
// Observe adds an Observer
Observe(Observer)
// Update resets the internal expiry time of a Session
Update(id string) error
// Delete removes a Session
Delete(id string)
}
Manager is an interface for dealing with sessions
Observer
type Observer = maps.Observer[string, *T]
ObserverFunc
type ObserverFunc = maps.ObserverFunc[string, *T]
T
type T struct {
// contains filtered or unexported fields
}
T is a session
FromContext
func FromContext(ctx context.Context) (session *T, ok bool)
FromContext returns the Session value stored in ctx, if any
returns (nil, true) to indicate Session lookup has already failed
New
func New(id, name string, time time.Time) *T
New creates a session
T.Expired
func (t *T) Expired() bool
Expired returns t.Expires().Before(time.Now())
T.Expires
func (t *T) Expires() time.Time
Expires returns the time this session expires
T.ID
func (t *T) ID() string
ID returns the SessionID
T.Name
func (t *T) Name() string
Name returns the name given during creation
T.NewContext
func (t *T) NewContext(ctx context.Context) context.Context
NewContext exposes package-level NewContext
T.SetExpires
func (t *T) SetExpires(time time.Time)
SetExpires updates the time this session expires