auth

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const SessionTableDDL = `` /* 308-byte string literal not displayed */

SessionTableDDL is the CREATE TABLE statement for the sessions table. Call PGSessionStore.Migrate to execute it.

Variables

View Source
var ErrInvalidHash = errors.New("invalid password hash")
View Source
var ErrSessionNotFound = errors.New("session not found")

Functions

func ActorFromContext

func ActorFromContext(ctx context.Context) (admin.Actor, bool)

func CSRFMiddleware

func CSRFMiddleware(next http.Handler) http.Handler

func HashPassword

func HashPassword(password string, config PasswordConfig) (string, error)

func IssueCSRF

func IssueCSRF(w http.ResponseWriter, secure bool) (string, error)

func VerifyPassword

func VerifyPassword(password, encoded string) (bool, error)

Types

type CacheSessionStore added in v0.4.0

type CacheSessionStore struct {
	Cache  cache.Cache
	Prefix string
}

CacheSessionStore stores sessions in any cache.Cache implementation. Use it with Redis, Memcached, Dragonfly, KeyDB, or an existing app cache.

func NewCacheSessionStore added in v0.4.0

func NewCacheSessionStore(c cache.Cache) *CacheSessionStore

func (*CacheSessionStore) Create added in v0.4.0

func (s *CacheSessionStore) Create(ctx context.Context, actor admin.Actor, ttl time.Duration) (Session, error)

func (*CacheSessionStore) Delete added in v0.4.0

func (s *CacheSessionStore) Delete(ctx context.Context, id string) error

func (*CacheSessionStore) Get added in v0.4.0

func (s *CacheSessionStore) Get(ctx context.Context, id string) (Session, error)

type MemorySessionStore

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

func NewMemorySessionStore

func NewMemorySessionStore() *MemorySessionStore

func (*MemorySessionStore) Create

func (s *MemorySessionStore) Create(_ context.Context, actor admin.Actor, ttl time.Duration) (Session, error)

func (*MemorySessionStore) Delete

func (s *MemorySessionStore) Delete(_ context.Context, id string) error

func (*MemorySessionStore) Get

type PGSessionStore

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

PGSessionStore is a PostgreSQL-backed SessionStore. Use NewPGSessionStore to construct it, then call Migrate once at startup.

func NewPGSessionStore

func NewPGSessionStore(pool *pgxpool.Pool) *PGSessionStore

NewPGSessionStore creates a PGSessionStore backed by the supplied pool.

func (*PGSessionStore) Cleanup

func (s *PGSessionStore) Cleanup(ctx context.Context) (int64, error)

Cleanup deletes all expired sessions and returns the number of rows removed. Schedule this periodically (e.g. every hour) to keep the table small.

func (*PGSessionStore) Create

func (s *PGSessionStore) Create(ctx context.Context, actor admin.Actor, ttl time.Duration) (Session, error)

Create inserts a new session for actor with the given TTL and returns it.

func (*PGSessionStore) Delete

func (s *PGSessionStore) Delete(ctx context.Context, id string) error

Delete removes a session. It is not an error if the session does not exist.

func (*PGSessionStore) Get

func (s *PGSessionStore) Get(ctx context.Context, id string) (Session, error)

Get retrieves a non-expired session by ID. Returns ErrSessionNotFound when the session does not exist or has expired.

func (*PGSessionStore) Migrate

func (s *PGSessionStore) Migrate(ctx context.Context) error

Migrate creates the sessions table and index if they do not exist. Call this once at application startup before handling any requests.

type PasswordConfig

type PasswordConfig struct {
	Memory      uint32
	Iterations  uint32
	Parallelism uint8
	SaltLength  uint32
	KeyLength   uint32
}

func DefaultPasswordConfig

func DefaultPasswordConfig() PasswordConfig

type RateLimiter

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

func NewRateLimiter

func NewRateLimiter(limit int, window time.Duration) *RateLimiter

func (*RateLimiter) Allow

func (l *RateLimiter) Allow(key string) bool

func (*RateLimiter) Middleware

func (l *RateLimiter) Middleware(next http.Handler) http.Handler

type Session

type Session struct {
	ID        string
	Actor     admin.Actor
	ExpiresAt time.Time
	CreatedAt time.Time
}

type SessionManager

type SessionManager struct {
	Store      SessionStore
	CookieName string
	TTL        time.Duration
	Secure     bool
	SameSite   http.SameSite
}

func NewSessionManager

func NewSessionManager(store SessionStore) *SessionManager

func (*SessionManager) End

func (*SessionManager) Middleware

func (m *SessionManager) Middleware(next http.Handler) http.Handler

func (*SessionManager) Start

type SessionStore

type SessionStore interface {
	Create(ctx context.Context, actor admin.Actor, ttl time.Duration) (Session, error)
	Get(ctx context.Context, id string) (Session, error)
	Delete(ctx context.Context, id string) error
}

Jump to

Keyboard shortcuts

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