sessions

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package sessions manages users sessions

Index

Constants

View Source
const (
	DefaultSessionName = "__session_id"
)

Variables

View Source
var DefaultSessionConfig = SessionConfig{
	Skipper: middleware.DefaultSkipper,
}
View Source
var (
	// ErrInvalidSession is returned when the session is invalid
	ErrInvalidSession = errors.New("invalid session provided")
)
View Source
var SessionContextKey = &ContextKey{"SessionContextKey"}

SessionContextKey is the context key for the session-context

Functions

func GenerateSessionID

func GenerateSessionID() string

GenerateSessionID returns a random ulid

func LoadAndSave

func LoadAndSave(sessionManager CookieStore, client *redis.Client, logger *zap.SugaredLogger) echo.MiddlewareFunc

LoadAndSave is a middleware function that loads and saves session data using a provided session manager. It takes a `SessionManager` as input and returns a middleware function that can be used with an Echo framework application

func LoadAndSaveWithConfig

func LoadAndSaveWithConfig(config *SessionConfig) echo.MiddlewareFunc

LoadAndSaveWithConfig is a middleware that loads and saves session data using a provided session manager configuration. It takes a `SessionConfig` struct as input, which contains the skipper function and the session manager

func NewDebugSessionCookie

func NewDebugSessionCookie(session string) *http.Cookie

NewDebugSessionCookie creates a debug cookie from a session id

func NewSessionCookie

func NewSessionCookie(session string) *http.Cookie

NewSessionCookie creates a cookie from a session id

func Token

func Token(ctx context.Context) map[string]string

Token returns the session token from the context

Types

type Config

type Config struct {
	// SigningKey must be a 16, 32, or 64 character string used to encode the cookie
	SigningKey string `yaml:"signingKey" split_words:"true" default:"my-signing-secret"` // $DATUM_SESSIONS_SIGNING_KEY
	// EncryptionKey must be a 16, 32, or 64 character string used to encode the cookie
	EncryptionKey string `yaml:"encryptionKey" split_words:"true" default:"encryptionsecret"` // $DATUM_SESSIONS_ENCRYPTION_KEY
}

type ContextKey

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

ContextKey is the key name for the additional context

type CookieStore

type CookieStore interface {
	// New returns a new named Session
	New(name string) *Session
	// Get a named Session from the request
	Get(req *http.Request, name string) (*Session, error)
	// Save writes a Session to the ResponseWriter
	Save(w http.ResponseWriter, session *Session) error
	// Destroy removes (expires) a named Session
	Destroy(w http.ResponseWriter, name string)
	// GetUserFromSession with the provided cookie name
	GetUserFromSession(req *http.Request, name string) (string, error)
}

A CookieStore manages creating, accessing, writing, and expiring Sessions.

func NewCookieStore

func NewCookieStore(config *cookies.CookieConfig, keyPairs ...[]byte) CookieStore

NewCookieStore returns a new Store that signs and optionally encrypts session state in http cookies.

type PersistentStore

type PersistentStore interface {
	Exists(ctx context.Context, userID string) (int64, error)
	GetSession(ctx context.Context, userID string) (string, error)
	StoreSession(ctx context.Context, sessionID string, userID string) error
	DeleteSession(ctx context.Context, userID string) error
}

PersistentStore is defining an interface for session store

func NewStore

func NewStore(client *redis.Client) PersistentStore

NewStore returns a new Store that stores to a persistent backend (redis)

type Session

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

Session represents state values maintained in a sessions Store.

func New

func New(store CookieStore) *Session

New returns a new Session.

func NewWithName

func NewWithName(store CookieStore, name string) *Session

NewWithName returns a new Session with the provided name

func (*Session) Destroy

func (s *Session) Destroy(w http.ResponseWriter)

Destroy destroys the session. Identical to calling store.Destroy(w, session.name).

func (*Session) Get

func (s *Session) Get(key string) string

Get returns the state value for the given key.

func (*Session) GetOk

func (s *Session) GetOk(key string) (string, bool)

GetOk returns the state value for the given key and whether they key exists.

func (*Session) Name

func (s *Session) Name() string

Name returns the name of the session.

func (*Session) Save

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

Save adds or updates the session. Identical to calling store.Save(w, session).

func (*Session) Set

func (s *Session) Set(key string, value string)

Set sets a key/value pair in the session state.

type SessionConfig

type SessionConfig struct {
	// Skipper is a function that determines whether a particular request should be skipped or not
	Skipper middleware.Skipper
	// SessionManager is responsible for managing the session cookies. It handles the creation, retrieval, and deletion of
	// session cookies for each user session
	SessionManager CookieStore
	// Store is used to store and retrieve session data in a persistent manner such as to a redis backend
	Store PersistentStore
	// RedisClient establishes a connection to a Redis server and perform operations such as storing and retrieving data
	RedisClient *redis.Client
	// Logger is used to log errors in the middleware
	Logger *zap.SugaredLogger
}

SessionConfig is used to configure session management

func NewSessionConfig

func NewSessionConfig(sm CookieStore, rc *redis.Client, logger *zap.SugaredLogger) *SessionConfig

NewSessionConfig creates a new session config

func (*SessionConfig) SaveAndStoreSession

func (sc *SessionConfig) SaveAndStoreSession(ctx echo.Context, userID string) error

SaveAndStoreSession saves the session to the cookie and to the persistent store (redis)

Jump to

Keyboard shortcuts

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