session

package
v0.0.0-...-1c407d2 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2022 License: MIT Imports: 12 Imported by: 0

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("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 Key

type Key int

Key ...

const (
	SessionKey Key = iota
)

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) 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 NewMemoryStore

func NewMemoryStore(sessionDuration time.Duration) *MemoryStore

NewMemoryStore constructs and returns a new MemoryStore

func (*MemoryStore) DelSession

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

DelSession ...

func (*MemoryStore) GetAllSessions

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

GetAllSessions ...

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) 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 {
	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 NewSession

func NewSession(store Store) *Session

func (*Session) Bytes

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

func (*Session) Del

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

func (*Session) Expired

func (sess *Session) Expired() bool

func (*Session) Get

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

func (*Session) Has

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

func (*Session) Set

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

type Store

type Store interface {
	GetSession(sid string) (*Session, error)
	SetSession(sid string, sess *Session) error
	HasSession(sid string) bool
	DelSession(sid string) error

	SyncSession(sess *Session) error

	GetAllSessions() ([]*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.

Jump to

Keyboard shortcuts

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