csrf

package
v0.0.0-...-18d7262 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package csrf provides CSRF protection for web handlers

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken indicates a missing or invalid CSRF token
	ErrInvalidToken = errors.New("invalid csrf token")

	// ErrTokenExpired indicates the CSRF token has expired
	ErrTokenExpired = errors.New("csrf token expired")
)

Functions

This section is empty.

Types

type Manager

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

Manager handles CSRF token generation and validation

func NewManager

func NewManager(store Store, secret []byte, expiresIn time.Duration) *Manager

NewManager creates a new CSRF token manager

func (*Manager) CheckHealth

func (m *Manager) CheckHealth(ctx context.Context) error

CheckHealth verifies the CSRF manager is operational

func (*Manager) GenerateToken

func (m *Manager) GenerateToken(ctx context.Context) (string, error)

GenerateToken creates and stores a new CSRF token

func (*Manager) ValidateToken

func (m *Manager) ValidateToken(ctx context.Context, token string) error

ValidateToken checks if a token is valid

type RedisStore

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

RedisStore implements the Store interface using Redis

func (*RedisStore) CheckHealth

func (s *RedisStore) CheckHealth(ctx context.Context) error

CheckHealth verifies Redis connectivity

func (*RedisStore) SaveToken

func (s *RedisStore) SaveToken(ctx context.Context, token string, expiresIn time.Duration) error

SaveToken stores a CSRF token with expiration

func (*RedisStore) ValidateToken

func (s *RedisStore) ValidateToken(ctx context.Context, token string) error

ValidateToken checks if a token exists and has not expired

type Store

type Store interface {
	// SaveToken stores a CSRF token with expiry
	SaveToken(ctx context.Context, token string, expiresIn time.Duration) error

	// ValidateToken checks if a token exists and is valid
	ValidateToken(ctx context.Context, token string) error

	// CheckHealth verifies the store is operational
	CheckHealth(ctx context.Context) error
}

Store provides token storage operations

func NewRedisStore

func NewRedisStore(client *redis.Client) Store

NewRedisStore creates a new Redis-backed CSRF token store

Jump to

Keyboard shortcuts

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