sookie

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 9 Imported by: 0

README

sookie

Opinionated Secure Cookie

  • MsgPack encoded
  • Zstd compressed
  • XChaCha20-Poly1305 authenticated & encrypted

Documentation

Overview

Package sookie provides a simple way to set and get cookies with encryption and compression. It uses the XChaCha20-Poly1305 AEAD algorithm for encryption and Zstandard for compression. The cookie value is base64 encoded and can be safely sent over HTTP headers.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrExpired is returned when the cookie has expired.
	ErrExpired = errors.New("sookie: cookie expired")
)

Functions

func Del added in v0.2.0

func Del(w http.ResponseWriter, r *http.Request, cookie http.Cookie)

Del deletes a cookie with the given name from the response, if it was present in the request.

func Get

func Get[V any](secret []byte, r *http.Request, name string) (V, error)

Get retrieves a cookie with the given name from the request. The cookie value is decrypted and decompressed using the XChaCha20-Poly1305 AEAD algorithm. The cookie value is unmarshaled into the given type V. If the cookie is not found, the http.ErrNoCookie error is returned. If the cookie is expired, the ErrExpired error is returned.

func Open added in v0.4.0

func Open[V any](secret []byte, raw string) (V, error)

Open retrieves a value from the raw encrypted string. The raw value is decrypted and decompressed using the XChaCha20-Poly1305 AEAD algorithm. The raw value is unmarshaled into the given type V. If the raw value is expired, the ErrExpired error is returned.

func Seal added in v0.4.0

func Seal[V any](secret []byte, expires time.Time, value V) (string, error)

Seal encodes a Value. The value is encrypted and compressed using the XChaCha20-Poly1305 AEAD algorithm and Zstandard compression. The expiry time, if non-zero will be used when Opening the value to ensure it has not expired.

func Set

func Set[V any](secret []byte, w http.ResponseWriter, value V, cookie http.Cookie) error

Set sets a cookie with the given value. The value is encrypted and compressed using the XChaCha20-Poly1305 AEAD algorithm and Zstandard compression. The cookie is set with the given name and options. MaxAge or Expires may optionally be set to control the expiration of the cookie. MaxAge takes precedence over Expires. The http.Cookie `Value` field must be empty and the passed in value will me marshaled and used instead. The cookie will be deleted if MaxAge is less than 0 (and an empty value will be sent).

Types

This section is empty.

Jump to

Keyboard shortcuts

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