csrf

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CSRFTokenContextKey contextKey = "csrf_token"
	SessionIDContextKey contextKey = "session_id"
)

Variables

View Source
var (
	ErrTokenMismatch           = errors.New("csrf token mismatch")
	ErrTokenNotFound           = errors.New("csrf token not found")
	ErrTokenExpired            = errors.New("csrf token expired")
	ErrKeyOrTokenEmpty         = errors.New("key or token must not be empty")
	DefaultSessionID           = "session_id"
	DefaultExpirationTime      = 10 * time.Minute
	DefaultCleanupIntervalTime = 10 * time.Minute
)

Functions

func GenerateCSRFToken

func GenerateCSRFToken() (string, error)

GenerateCSRFToken creates a secure random token for CSRF protection

Types

type DefaultMemoryCSRFStore

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

DefaultMemoryCSRFStore includes token expiration and cleanup

func NewDefaultMemoryCSRFStore

func NewDefaultMemoryCSRFStore(durations ...time.Duration) *DefaultMemoryCSRFStore

NewDefaultMemoryCSRFStore creates a store with periodic cleanup

func (*DefaultMemoryCSRFStore) Store

func (s *DefaultMemoryCSRFStore) Store(key, token string) error

Store saves a token with expiration

func (*DefaultMemoryCSRFStore) Validate

func (s *DefaultMemoryCSRFStore) Validate(key, token string) error

type MemoryCSRFStore

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

MemoryCSRFStore is a simple in-memory implementation of CSRFStore

func NewMemoryCSRFStore

func NewMemoryCSRFStore() *MemoryCSRFStore

NewMemoryCSRFStore creates a new MemoryCSRFStore

func (*MemoryCSRFStore) Store

func (s *MemoryCSRFStore) Store(key, token string) error

Store saves a token with the given key

func (*MemoryCSRFStore) Validate

func (s *MemoryCSRFStore) Validate(key, token string) error

Validate checks if the provided token matches the one stored under the given key

type Store

type Store interface {
	// Store saves a token with the given key
	Store(key, token string) error

	// Validate checks if the token is valid for the key
	Validate(key, token string) error
}

Store defines an interface for storing and retrieving CSRF tokens. Implementations can use any session storage mechanism.

type TokenEntry

type TokenEntry struct {
	Token      string
	Expiration time.Time
}

TokenEntry stores a token with its expiration time

Jump to

Keyboard shortcuts

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