cookies

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValueTooLong         = errors.New("cookie value too long")
	ErrInvalidValue         = errors.New("invalid cookie value")
	ErrInvalidConfiguration = errors.New("invalid cookie configuration")
)

Functions

func NewCookie

func NewCookie(name, value string, policy CookiePolicy) *fiber.Cookie

NewCookie builds a Fiber cookie using a shared policy.

func Read

func Read(c fiber.Ctx, name string) (string, error)

Read decodes the cookie value using base64 and returns it as a string.

func ReadEncrypted

func ReadEncrypted(c fiber.Ctx, name string, cfg EncryptedConfig, deps CryptoDeps) (string, error)

ReadEncrypted reads an encrypted cookie value from the request and decrypts it using the secret key from config. The cookie name is authenticated in the encrypted payload so values cannot be replayed across different cookie names. If decryption fails or the authenticated name does not match, it returns ErrInvalidValue.

func ReadSigned

func ReadSigned(c fiber.Ctx, name string, cfg SignedConfig) (string, error)

ReadSigned reads a signed cookie value and validates its HMAC signature.

func Write

func Write(c fiber.Ctx, cookie *fiber.Cookie, cfg WriteConfig) error

Write encodes the cookie value using base64 and writes it to the response.

func WriteEncrypted

func WriteEncrypted(c fiber.Ctx, cookie *fiber.Cookie, cfg EncryptedConfig, deps CryptoDeps) error

WriteEncrypted encrypts the cookie value using the secret key from config and writes it to the response. The cookie name is included in the encrypted payload so the value cannot be replayed for other cookie names.

func WriteSigned

func WriteSigned(c fiber.Ctx, cookie *fiber.Cookie, cfg SignedConfig) error

WriteSigned encodes the cookie value using base64, signs it with a HMAC.

Types

type CookiePolicy

type CookiePolicy struct {
	Path        string
	Domain      string
	Expires     time.Time
	MaxAge      int
	Secure      bool
	HTTPOnly    bool
	SameSite    string
	SessionOnly bool
	Partitioned bool
}

CookiePolicy defines reusable cookie attributes for API-wide defaults.

func DefaultCookiePolicy

func DefaultCookiePolicy() CookiePolicy

DefaultCookiePolicy returns a secure default cookie policy.

type CryptoDeps

type CryptoDeps struct {
	Encrypt func(secretKey, plaintext string) (string, error)
	Decrypt func(secretKey, ciphertext string) (string, error)
}

CryptoDeps defines encryption/decryption dependencies for cookie helpers.

func DefaultCryptoDeps

func DefaultCryptoDeps() CryptoDeps

DefaultCryptoDeps returns production crypto implementations.

type EncryptedConfig

type EncryptedConfig struct {
	SecretKey string
	Write     WriteConfig
}

EncryptedConfig defines encryption configuration for encrypted cookies.

type SignedConfig

type SignedConfig struct {
	SecretKey []byte
	Write     WriteConfig
}

SignedConfig defines HMAC configuration for signed cookies.

type WriteConfig

type WriteConfig struct {
	MaxSerializedSize int
}

WriteConfig defines behavior for serialized cookie size enforcement.

func DefaultWriteConfig

func DefaultWriteConfig() WriteConfig

DefaultWriteConfig returns default write constraints.

Jump to

Keyboard shortcuts

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