session

package
v0.0.0-...-20f29a4 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPostgresTable = "bebo_sessions"

Variables

View Source
var (
	// ErrInvalidCookie indicates an invalid or tampered session cookie.
	ErrInvalidCookie = errors.New("invalid session cookie")
)

Functions

This section is empty.

Types

type CookieStore

type CookieStore struct {
	Name     string
	Keys     [][]byte
	Path     string
	MaxAge   time.Duration
	Secure   bool
	HTTPOnly bool
	SameSite http.SameSite
}

CookieStore stores sessions in a signed cookie.

func NewCookieStore

func NewCookieStore(name string, key []byte, oldKeys ...[]byte) *CookieStore

NewCookieStore creates a cookie store with key rotation support.

func (*CookieStore) Clear

func (s *CookieStore) Clear(w http.ResponseWriter, session *Session)

Clear expires the session cookie.

func (*CookieStore) Get

func (s *CookieStore) Get(r *http.Request) (*Session, error)

Get loads a session from the request.

func (*CookieStore) Save

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

Save writes the session cookie.

type MemoryOption

type MemoryOption func(*MemoryStore)

MemoryOption configures a MemoryStore.

func WithSessionHTTPOnly

func WithSessionHTTPOnly(enabled bool) MemoryOption

WithSessionHTTPOnly sets the session cookie HttpOnly flag.

func WithSessionPath

func WithSessionPath(path string) MemoryOption

WithSessionPath sets the session cookie path.

func WithSessionSameSite

func WithSessionSameSite(mode http.SameSite) MemoryOption

WithSessionSameSite sets the session cookie SameSite flag.

func WithSessionSecure

func WithSessionSecure(enabled bool) MemoryOption

WithSessionSecure sets the session cookie Secure flag.

type MemoryStore

type MemoryStore struct {
	Name     string
	TTL      time.Duration
	Path     string
	Secure   bool
	HTTPOnly bool
	SameSite http.SameSite
	// contains filtered or unexported fields
}

MemoryStore stores sessions in memory and uses a session ID cookie.

func NewMemoryStore

func NewMemoryStore(name string, ttl time.Duration, options ...MemoryOption) *MemoryStore

NewMemoryStore creates an in-memory store.

func (*MemoryStore) Clear

func (s *MemoryStore) Clear(w http.ResponseWriter, session *Session)

Clear removes a session.

func (*MemoryStore) Get

func (s *MemoryStore) Get(r *http.Request) (*Session, error)

Get loads a session from the request.

func (*MemoryStore) Save

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

Save persists a session.

type PostgresOptions

type PostgresOptions struct {
	DisableDefaults bool
	DB              *sql.DB
	Name            string
	Table           string
	TTL             time.Duration
	Timeout         time.Duration
	Path            string
	Secure          bool
	HTTPOnly        bool
	SameSite        http.SameSite
}

PostgresOptions configures a Postgres store.

type PostgresStore

type PostgresStore struct {
	DB       *sql.DB
	Name     string
	Table    string
	TTL      time.Duration
	Timeout  time.Duration
	Path     string
	Secure   bool
	HTTPOnly bool
	SameSite http.SameSite
	// contains filtered or unexported fields
}

PostgresStore stores sessions in PostgreSQL using a session ID cookie.

func NewPostgresStore

func NewPostgresStore(options PostgresOptions) (*PostgresStore, error)

NewPostgresStore builds a Postgres-backed store.

func (*PostgresStore) Cleanup

func (s *PostgresStore) Cleanup(ctx context.Context) error

Cleanup removes expired sessions.

func (*PostgresStore) Clear

func (s *PostgresStore) Clear(w http.ResponseWriter, session *Session)

Clear removes a session.

func (*PostgresStore) EnsureTable

func (s *PostgresStore) EnsureTable(ctx context.Context) error

EnsureTable creates the session table if it does not exist.

func (*PostgresStore) Get

func (s *PostgresStore) Get(r *http.Request) (*Session, error)

Get loads a session from the request.

func (*PostgresStore) Save

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

Save persists a session.

type RedisOptions

type RedisOptions struct {
	DisableDefaults bool
	Name            string
	Network         string
	Address         string
	Username        string
	Password        string
	DB              int
	TTL             time.Duration
	Prefix          string
	DialTimeout     time.Duration
	ReadTimeout     time.Duration
	WriteTimeout    time.Duration
	Path            string
	Secure          bool
	HTTPOnly        bool
	SameSite        http.SameSite
}

RedisOptions configures a Redis store.

type RedisStore

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

RedisStore stores sessions in Redis using a session ID cookie.

func NewRedisStore

func NewRedisStore(options RedisOptions) *RedisStore

NewRedisStore creates a Redis-backed session store.

func (*RedisStore) Clear

func (s *RedisStore) Clear(w http.ResponseWriter, session *Session)

Clear removes a session.

func (*RedisStore) Get

func (s *RedisStore) Get(r *http.Request) (*Session, error)

Get loads a session from the request.

func (*RedisStore) Save

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

Save persists a session.

type Session

type Session struct {
	ID     string
	Values map[string]string
	// contains filtered or unexported fields
}

Session represents session data.

func (*Session) Clear

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

Clear clears the session from the configured store.

func (*Session) Delete

func (s *Session) Delete(key string)

Delete removes a key.

func (*Session) Get

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

Get returns a value.

func (*Session) IsNew

func (s *Session) IsNew() bool

IsNew reports whether the session was newly created for this request.

func (*Session) Save

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

Save writes the session to the configured store.

func (*Session) Set

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

Set sets a key value.

type Store

type Store interface {
	Get(*http.Request) (*Session, error)
	Save(http.ResponseWriter, *Session) error
	Clear(http.ResponseWriter, *Session)
}

Store defines a session persistence backend.

Jump to

Keyboard shortcuts

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