sessions

package module
v0.0.0-...-1d4fd80 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2023 License: MIT Imports: 14 Imported by: 0

README

go.mills.io/sessions

Build Status Go Report Card Go Reference

go.mills.io/sessions is a session handling library for Web applications for the Go programming language. There is no special requirement for using this library, no frameworks necessary. By default an in-memory store is used for storing sessions and the store is an interface that different implementations can be provided such as a database.

Quick Start

TBD

License

go.mills.io/sessions is licensed under the terms of the MIT license

Documentation

Index

Constants

View Source
const DefaultSessionDuration = time.Hour

DefaultSessionDuration is the default duration for saving session data in the store. Most Store implementations will automatically delete saved session data after this time.

Variables

View Source
var (
	ErrSessionNotFound = errors.New("session not found or expired")
	ErrSessionExpired  = errors.New("session expired")
)
View Source
var ErrInvalidID = errors.New("error: invalid session id")

ErrInvalidID is returned when an invalid session id is passed to ValidateID()

Functions

func LoadSession

func LoadSession(data []byte, sess *Session) error

Types

type ID

type ID string

ID represents a valid, digitally-signed session ID

const InvalidSessionID ID = ""

InvalidSessionID represents an empty, invalid session ID

func NewSessionID

func NewSessionID(signingKey string) (ID, error)

NewSessionID creates and returns a new digitally-signed session ID, using `signingKey` as the HMAC signing key. An error is returned only if there was an error generating random bytes for the session ID

func ValidateSessionID

func ValidateSessionID(id string, signingKey string) (ID, error)

ValidateSessionID validates the `id` parameter using the `signingKey` and returns an error if invalid, or a SignedID if valid

func (ID) String

func (sid ID) String() string

type Manager

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

Manager ...

func NewManager

func NewManager(options *Options, store Store) *Manager

NewManager ...

func (*Manager) Create

func (m *Manager) Create(w http.ResponseWriter) (*Session, error)

Create ...

func (*Manager) Delete

func (m *Manager) Delete(w http.ResponseWriter, r *http.Request)

Delete ...

func (*Manager) ExemptGlob

func (m *Manager) ExemptGlob(pathGlob string)

func (*Manager) ExemptPath

func (m *Manager) ExemptPath(path string)

func (*Manager) GetOrCreate

func (m *Manager) GetOrCreate(w http.ResponseWriter, r *http.Request) (*Session, error)

GetOrCreate ...

func (*Manager) Handler

func (m *Manager) Handler(next http.Handler) http.Handler

Handler ...

func (*Manager) Validate

func (m *Manager) Validate(value string) (ID, error)

Validate ....

type Map

type Map map[string]string

Map ...

type MemoryStore

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

MemoryStore represents an in-memory session store. This should be used only for testing and prototyping. Production systems should use a shared server store like redis

func (*MemoryStore) DelSession

func (s *MemoryStore) DelSession(sid string) error

DelSession ...

func (*MemoryStore) GetAllSessions

func (s *MemoryStore) GetAllSessions() ([]*Session, error)

All ...

func (*MemoryStore) GetSession

func (s *MemoryStore) GetSession(sid string) (*Session, error)

GetSession ...

func (*MemoryStore) HasSession

func (s *MemoryStore) HasSession(sid string) bool

HasSession ...

func (*MemoryStore) LenSessions

func (s *MemoryStore) LenSessions() int64

Count ...

func (*MemoryStore) SetSession

func (s *MemoryStore) SetSession(sid string, sess *Session) error

SetSession ...

func (*MemoryStore) SyncSession

func (s *MemoryStore) SyncSession(sess *Session) error

SyncSession ...

type Options

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

Options ...

func NewOptions

func NewOptions(name, secret string, secure bool, expiry time.Duration) *Options

NewOptions ...

type Session

type Session struct {
	sync.RWMutex

	ID        string    `json:"id"`
	Data      Map       `json:"data"`
	CreatedAt time.Time `json:"created"`
	ExpiresAt time.Time `json:"expires"`
	// contains filtered or unexported fields
}

Session ...

func FromRequest

func FromRequest(r *http.Request) *Session

func NewSession

func NewSession(store Store) *Session

func (*Session) Bytes

func (s *Session) Bytes() ([]byte, error)

func (*Session) Del

func (s *Session) Del(key string) error

func (*Session) Expired

func (s *Session) Expired() bool

func (*Session) Get

func (s *Session) Get(key string) (val string, ok bool)

func (*Session) Has

func (s *Session) Has(key string) bool

func (*Session) Set

func (s *Session) Set(key, val string) error

type Store

type Store interface {
	LenSessions() int64
	GetSession(sid string) (*Session, error)
	SetSession(sid string, sess *Session) error
	HasSession(sid string) bool
	DelSession(sid string) error
	GetAllSessions() ([]*Session, error)
	SyncSession(sess *Session) error
}

Store represents a session data store. This is an abstract interface that can be implemented against several different types of data stores. For example, session data could be stored in memory in a concurrent map, or more typically in a shared key/value server store like redis.

func NewMemoryStore

func NewMemoryStore(sessionDuration time.Duration) Store

NewMemoryStore constructs and returns a new MemoryStore

Jump to

Keyboard shortcuts

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