Documentation
¶
Overview ¶
Package session provides encrypted cookie sessions, one-shot flash messages, and CSRF protection for UI-mode applications.
Index ¶
- Constants
- func CSRF(options CSRFOptions) gin.HandlerFunc
- func Forget(c *gin.Context, key string) error
- func Get(c *gin.Context, key string) any
- func Middleware(options Options) (gin.HandlerFunc, error)
- func PutFlash(c *gin.Context, kind, message string) error
- func Set(c *gin.Context, key string, value any) error
- func TemplateField(c *gin.Context) template.HTML
- func Token(c *gin.Context) string
- type CSRFOptions
- type Flash
- type Options
Constants ¶
const MinimumSecretLength = 32
MinimumSecretLength is the smallest accepted session secret, in bytes.
Variables ¶
This section is empty.
Functions ¶
func CSRF ¶
func CSRF(options CSRFOptions) gin.HandlerFunc
CSRF validates a per-session token on every state-changing request. Safe methods (GET/HEAD/OPTIONS/TRACE) pass through and lazily mint the token. Install it after the session middleware.
func Middleware ¶
func Middleware(options Options) (gin.HandlerFunc, error)
Middleware installs the encrypted cookie session store.
func TemplateField ¶
TemplateField renders a hidden input carrying the CSRF token for HTML forms.
Types ¶
type CSRFOptions ¶
type CSRFOptions struct {
// FieldName is the form field carrying the token, defaulting to _csrf.
FieldName string
// HeaderName is checked before the form field, defaulting to X-CSRF-Token.
HeaderName string
// Skipper exempts requests; by default paths under /api/ are skipped
// because token-authenticated JSON APIs are not CSRF-prone.
Skipper func(*gin.Context) bool
}
CSRFOptions defines an implementation type used by this package.
type Flash ¶
type Flash struct {
// Kind store data used by this type.
Kind string
// Message store data used by this type.
Message string
}
Flash is a one-shot message that survives exactly one redirect.
type Options ¶
type Options struct {
// Secret signs and encrypts the session cookie and must contain at least
// 32 bytes. One secret drives both keys.
Secret []byte
// CookieName defaults to gin_kit_session.
CookieName string
// MaxAge defaults to seven days, in seconds.
MaxAge int
// Secure marks the cookie HTTPS-only; enable it outside development.
Secure bool
// Domain store data used by this type.
Domain string
// Path defaults to /.
Path string
// SameSite defaults to Lax.
SameSite http.SameSite
}
Options defines an implementation type used by this package.