csrf

package
v0.1.27 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTokenNotFound 表示请求中未找到CSRF令牌
	ErrTokenNotFound = errors.New("CSRF token not found in request")
	// ErrTokenInvalid 表示请求中的CSRF令牌无效
	ErrTokenInvalid = errors.New("CSRF token invalid")
	// ErrTokenExpired 表示CSRF令牌已过期
	ErrTokenExpired = errors.New("CSRF token expired")
)

Functions

func New

func New(options ...func(*Config)) mist.Middleware

New 创建新的CSRF保护中间件

func WithCookieDomain

func WithCookieDomain(domain string) func(*Config)

WithCookieDomain 设置cookie域

func WithCookieMaxAge

func WithCookieMaxAge(maxAge time.Duration) func(*Config)

WithCookieMaxAge 设置cookie最大存活时间

func WithCookieName

func WithCookieName(name string) func(*Config)

WithCookieName 设置cookie名称

func WithCookiePath

func WithCookiePath(path string) func(*Config)

WithCookiePath 设置cookie路径

func WithCookieSameSite added in v0.1.24

func WithCookieSameSite(sameSite http.SameSite) func(*Config)

WithCookieSameSite 设置cookie的SameSite属性

func WithCookieSecure

func WithCookieSecure(secure bool) func(*Config)

WithCookieSecure 设置cookie是否仅HTTPS

func WithErrorHandler

func WithErrorHandler(handler func(ctx *mist.Context, err error)) func(*Config)

WithErrorHandler 设置自定义错误处理

func WithFormField

func WithFormField(field string) func(*Config)

WithFormField 设置表单字段名称

func WithHeaderName

func WithHeaderName(name string) func(*Config)

WithHeaderName 设置CSRF头名称

func WithIgnoreMethods

func WithIgnoreMethods(methods []string) func(*Config)

WithIgnoreMethods 设置忽略的HTTP方法

func WithTokenLength

func WithTokenLength(length int) func(*Config)

提供配置选项函数 WithTokenLength 设置令牌长度

func WithTokenMode added in v0.1.24

func WithTokenMode(mode TokenMode) func(*Config)

WithTokenMode 设置令牌验证模式

func WithTokenRefreshInterval added in v0.1.24

func WithTokenRefreshInterval(interval time.Duration) func(*Config)

WithTokenRefreshInterval 设置令牌刷新间隔

func WithTokenTTL added in v0.1.24

func WithTokenTTL(ttl time.Duration) func(*Config)

WithTokenTTL 设置令牌有效时间

Types

type Config

type Config struct {
	// TokenLength 令牌长度,默认32字节
	TokenLength int
	// CookieName CSRF cookie名称,默认为"_csrf"
	CookieName string
	// CookiePath cookie路径,默认为"/"
	CookiePath string
	// CookieDomain cookie的域,可选
	CookieDomain string
	// CookieMaxAge cookie最大存活时间,默认为24小时
	CookieMaxAge time.Duration
	// CookieSecure 是否仅通过HTTPS发送cookie,默认为false
	CookieSecure bool
	// CookieHTTPOnly 是否禁止JavaScript访问cookie,默认为true
	CookieHTTPOnly bool
	// CookieSameSite SameSite属性,默认为Lax
	CookieSameSite http.SameSite
	// HeaderName 请求中CSRF头名称,默认为"X-CSRF-Token"
	HeaderName string
	// FormField 表单中CSRF字段名称,默认为"csrf_token"
	FormField string
	// ErrorHandler 自定义错误处理
	ErrorHandler func(ctx *mist.Context, err error)
	// IgnoreMethods 忽略的HTTP方法(默认忽略GET, HEAD, OPTIONS, TRACE)
	IgnoreMethods []string
	// TokenMode 令牌验证模式,默认为StandardMode
	TokenMode TokenMode
	// TokenTTL 令牌有效时间,默认为2小时,设置为0则永不过期
	TokenTTL time.Duration
	// TokenRefreshInterval 令牌刷新间隔,默认为30分钟,设置为0则不刷新
	TokenRefreshInterval time.Duration
}

Config 配置CSRF保护

type TokenData added in v0.1.24

type TokenData struct {
	Token      string    // 令牌值
	Created    time.Time // 创建时间
	LastAccess time.Time // 最后访问时间
}

TokenData 存储令牌相关数据

type TokenMode added in v0.1.24

type TokenMode int

TokenMode 定义CSRF令牌验证模式

const (
	// StandardMode 标准模式 - 令牌存储在Cookie中并与请求中的令牌比较
	StandardMode TokenMode = iota
	// DoubleSubmitMode 双重提交模式 - 令牌存储在Cookie中并用于签名请求中的令牌
	DoubleSubmitMode
)

Jump to

Keyboard shortcuts

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