cookie

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: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrefixNone   = iota // 无前缀
	PrefixSecure        // __Secure- 前缀
	PrefixHost          // __Host- 前缀
)

前缀类型常量

View Source
const (
	PartitionNone        = iota // 不分区
	PartitionPartitioned        // 分区Cookie (Partitioned)
)

分区类型常量

Variables

View Source
var (
	ErrInvalidCookieConfig  = errors.New("无效的Cookie配置")
	ErrInsecureCookieConfig = errors.New("不安全的Cookie配置")
)

错误定义

Functions

func CreateHostPrefixCookie

func CreateHostPrefixCookie(name, value string, maxAge int) *http.Cookie

CreateHostPrefixCookie 创建使用__Host-前缀的Cookie

func CreatePartitionedCookie

func CreatePartitionedCookie(name, value string, maxAge int) *http.Cookie

CreatePartitionedCookie 创建分区Cookie

func CreateSecureCookie

func CreateSecureCookie(name, value string, maxAge int) *http.Cookie

CreateSecureCookie 创建安全Cookie

func CreateSecurePrefixCookie

func CreateSecurePrefixCookie(name, value string, maxAge int) *http.Cookie

CreateSecurePrefixCookie 创建使用__Secure-前缀的Cookie

func IsPartitioned

func IsPartitioned(cookie *http.Cookie) bool

IsPartitioned 检查Cookie是否为分区Cookie

func ParseCookieName

func ParseCookieName(name string) (prefix int, originalName string)

ParseCookieName 解析Cookie名,返回前缀类型和去除前缀的名称

func SetCookie

func SetCookie(w http.ResponseWriter, opts CookieOptions) error

SetCookie 安全地设置Cookie

func ValidateCookieSecurity

func ValidateCookieSecurity(cookie *http.Cookie) error

ValidateCookieSecurity 验证Cookie安全性

Types

type CookieOptions

type CookieOptions struct {
	// 基本信息
	Name     string
	Value    string
	Path     string
	Domain   string
	MaxAge   int
	Expires  time.Time
	Secure   bool
	HttpOnly bool
	SameSite http.SameSite

	// 增强特性
	Prefix    int  // Cookie前缀类型 (PrefixNone, PrefixSecure, PrefixHost)
	Partition bool // 是否为分区Cookie
}

CookieOptions 定义Cookie选项

func DefaultCookieOptions

func DefaultCookieOptions() CookieOptions

DefaultCookieOptions 返回默认的安全Cookie选项

func (*CookieOptions) SecurityLevel

func (opts *CookieOptions) SecurityLevel() int

安全性等级评估

func (*CookieOptions) SecurityLevelString

func (opts *CookieOptions) SecurityLevelString() string

SecurityLevelString 返回安全等级的描述

func (*CookieOptions) ToCookie

func (opts *CookieOptions) ToCookie() (*http.Cookie, error)

ToCookie 将选项转换为http.Cookie

func (*CookieOptions) Validate

func (opts *CookieOptions) Validate() error

Validate 验证Cookie选项是否有效

Jump to

Keyboard shortcuts

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