session

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package session provides encrypted cookie sessions, one-shot flash messages, and CSRF protection for UI-mode applications.

Index

Constants

View Source
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 Forget

func Forget(c *gin.Context, key string) error

Forget removes a session value.

func Get

func Get(c *gin.Context, key string) any

Get reads a session value.

func Middleware

func Middleware(options Options) (gin.HandlerFunc, error)

Middleware installs the encrypted cookie session store.

func PutFlash

func PutFlash(c *gin.Context, kind, message string) error

PutFlash stores a flash message, e.g. PutFlash(c, "success", "Task created.").

func Set

func Set(c *gin.Context, key string, value any) error

Set writes and saves a session value.

func TemplateField

func TemplateField(c *gin.Context) template.HTML

TemplateField renders a hidden input carrying the CSRF token for HTML forms.

func Token

func Token(c *gin.Context) string

Token returns the session's CSRF token, minting one when absent.

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.

func Flashes

func Flashes(c *gin.Context) []Flash

Flashes returns and clears the stored flash messages.

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.

Jump to

Keyboard shortcuts

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