Middleware

middleware/encryptcookie

github.com/gofiber/fiber/v3/middleware/encryptcookie

import "github.com/gofiber/fiber/v3/middleware/encryptcookie"

Variables

Source: middleware/encryptcookie/utils.go:16

var (
	ErrInvalidKeyLength      = errors.New("encryption key must be 16, 24, or 32 bytes")
	ErrInvalidEncryptedValue = errors.New("encrypted value is not valid")
)

ConfigDefault is the default config

Source: middleware/encryptcookie/config.go:40

var ConfigDefault = Config{
	Next:      nil,
	Except:    []string{},
	Key:       "",
	Encryptor: EncryptCookie,
	Decryptor: DecryptCookie,
}

Functions

func DecryptCookie(name, value, key string) (string, error)

Source: middleware/encryptcookie/utils.go:46

DecryptCookie Decrypts a cookie value with specific encryption key

func EncryptCookie(name, value, key string) (string, error)

Source: middleware/encryptcookie/utils.go:37

EncryptCookie Encrypts a cookie value with specific encryption key

func GenerateKey(length int) string

Source: middleware/encryptcookie/utils.go:124

GenerateKey returns a random string of 16, 24, or 32 bytes. The length of the key determines the AES encryption algorithm used: 16 bytes for AES-128, 24 bytes for AES-192, and 32 bytes for AES-256-GCM.

func New(config ...Config) fiber.Handler

Source: middleware/encryptcookie/encryptcookie.go:16

New creates a new middleware handler

Types

type Config

Source: middleware/encryptcookie/config.go:10

Config defines the config for middleware.

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c fiber.Ctx) bool

	// Custom function to encrypt cookies.
	//
	// Optional. Default: EncryptCookie (using AES-GCM)
	Encryptor func(name, decryptedString, key string) (string, error)

	// Custom function to decrypt cookies.
	//
	// Optional. Default: DecryptCookie (using AES-GCM)
	Decryptor func(name, encryptedString, key string) (string, error)

	// Base64 encoded unique key to encode & decode cookies.
	//
	// Required. Key length should be 16, 24, or 32 bytes when decoded
	// if using the default EncryptCookie and DecryptCookie functions.
	// You may use `encryptcookie.GenerateKey(length)` to generate a new key.
	Key string

	// Array of cookie keys that should not be encrypted.
	//
	// Optional. Default: []
	Except []string
}
Fields
  • Next func(c fiber.Ctx) bool

    Next defines a function to skip this middleware when returned true.

    Optional. Default: nil

  • Encryptor func(name, decryptedString, key string) (string, error)

    Custom function to encrypt cookies.

    Optional. Default: EncryptCookie (using AES-GCM)

  • Decryptor func(name, encryptedString, key string) (string, error)

    Custom function to decrypt cookies.

    Optional. Default: DecryptCookie (using AES-GCM)

  • Key string

    Base64 encoded unique key to encode & decode cookies.

    Required. Key length should be 16, 24, or 32 bytes when decoded if using the default EncryptCookie and DecryptCookie functions. You may use encryptcookie.GenerateKey(length) to generate a new key.

  • Except []string

    Array of cookie keys that should not be encrypted.

    Optional. Default: []