Documentation
¶
Index ¶
- Constants
- func GenerateCSRFToken() string
- type Config
- func (c *Config) Clone() *Config
- func (c *Config) Disable() *Config
- func (c *Config) Enable() *Config
- func (c *Config) GetTokenBinding(path string) string
- func (c *Config) GobDecode(data []byte) error
- func (c Config) GobEncode() ([]byte, error)
- func (Config) GormDBDataType(db *gorm.DB, field *schema.Field) string
- func (c Config) GormDataType() string
- func (c Config) GormValue(_ context.Context, db *gorm.DB) clause.Expr
- func (c *Config) IsValid() bool
- func (c *Config) Scan(val any) (err error)
- func (c *Config) ShouldValidate(method ...string) bool
- func (c Config) Value() (driver.Value, error)
- func (c *Config) WithExemptMethods(methods ...string) *Config
- func (c *Config) WithSameSite(sameSite http.SameSite) *Config
- func (c *Config) WithSecure(secure bool) *Config
- func (c *Config) WithStrategy(strategy Strategy) *Config
- func (c *Config) WithTimeout(seconds time.Duration) *Config
- func (c *Config) WithTokenMode(tokenMode TokenMode) *Config
- type Strategy
- type TokenMode
Constants ¶
View Source
const ( DefaultTokenName = "X-CSRF-Token" // HTTP Header/Form/Query中的字段名 DefaultCookieName = "csrf_token" // HttpOnly Cookie名称 DefaultStatusName = "csrf_token_status" // 状态Cookie名称 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Enabled bool `json:"enabled,omitempty" yaml:"enabled" ini:"enabled"` // 是否启用CSRF保护
Strategy Strategy `json:"strategy,omitempty" yaml:"strategy" ini:"strategy"` // 防护策略
Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout" ini:"timeout"` // Token超时时间(秒)
TokenMode TokenMode `json:"token_mode,omitempty" yaml:"token_mode" ini:"token_mode"` // Token模式
SameSite http.SameSite `json:"same_site,omitempty" yaml:"same_site" ini:"same_site"` // SameSite策略: strict/lax/none
Secure bool `json:"secure,omitempty" yaml:"secure" ini:"secure"` // 是否仅HTTPS
ExemptMethods []string `json:"exempt_methods,omitempty" yaml:"exempt_methods" ini:"exempt_methods"` // 豁免的HTTP方法
}
func SessionTokenConfig ¶
func SessionTokenConfig() *Config
SessionTokenConfig 使用session存储token的预设配置
func TimedTokenConfig ¶
TimedTokenConfig 使用定时存储token的预设配置
func (*Config) GetTokenBinding ¶
GetTokenBinding 这里需要用xxhash对 path进行hash编码。
func (Config) GormDBDataType ¶
func (Config) GormDataType ¶
func (*Config) ShouldValidate ¶
ShouldValidate 验证是否需要进行CSRF验证
func (*Config) WithExemptMethods ¶
func (*Config) WithSecure ¶
func (*Config) WithStrategy ¶
func (*Config) WithTokenMode ¶
type Strategy ¶
type Strategy string
const ( StrategyNone Strategy = "none" // 不需要CSRF保护 StrategyToken Strategy = "token" // 使用Token验证 // StrategyDoubleTap 双重提交Cookie(Double Submit Cookie) 是一种CSRF防护技术,它的核心思想是: // 服务端生成一个随机Token,通过Cookie发送给浏览器 // 前端在请求中携带这个token,通常放在header或者query中, // 服务端验证请求中的Token和Cookie中的Token是否一致 // /api/csrf-token接口在生成token时,需要根据对应path的配置来处理,另外需要设置两个cookie,一个可读,一个用于验证;可读的不设置具体值 StrategyDoubleTap Strategy = "double_tap" // 双重提交Cookie )
Click to show internal directories.
Click to hide internal directories.