cookie

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package cookie provides cookie encryption using PASETO

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SecureCookie

func SecureCookie() bool

SecureCookie returns true if the cookie should be secure

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client implements reading and writing encrypted cookies

func New

func New(keyBase64 string) (*Client, error)

New returns a new Client keyBase64 must be a base64-encoded string of at least 32 random bytes, used to derived the symmetric key.

func (*Client) Decrypt

func (c *Client) Decrypt(cookieName, cookieValue string) (*Values, error)

Decrypt decrypts a cookie and returns the values

func (*Client) Delete

func (c *Client) Delete(w http.ResponseWriter, cookieName string)

Delete deletes a cookie from the response

func (*Client) Encrypt

func (c *Client) Encrypt(cookieName string, expiration time.Time, values *Values) string

Encrypt encrypts a cookie and returns the value

func (*Client) Read

func (c *Client) Read(r *http.Request, cookieName string) (values *Values, found bool, err error)

Read reads the cookie from the request

func (*Client) WritePersistentCookie

func (c *Client) WritePersistentCookie(w http.ResponseWriter, cookieName, domain string, httpOnly bool, sameSite http.SameSite, expiration time.Duration, values *Values)

WritePersistentCookie writes a persistent cookie to the response

func (*Client) WriteSessionCookie

func (c *Client) WriteSessionCookie(w http.ResponseWriter, cookieName, domain string, httpOnly bool, sameSite http.SameSite, values *Values)

WriteSessionCookie writes a session cookie to the response

type Key

type Key string

Key is a type for storing values in a cookie

type Values

type Values struct {
	// contains filtered or unexported fields
}

Values holds data to be stored in the cookie

func NewValues

func NewValues() *Values

NewValues returns a new empty token

func (*Values) Get

func (v *Values) Get(key Key, output any) (err error)

Get gets the given key and writes the value into output (which should be a a pointer), if present by parsing the JSON using encoding/json.

func (*Values) GetString added in v0.7.2

func (v *Values) GetString(key Key) (string, error)

GetString returns the value for a given key as a string, or error if this is not possible (cannot be a string, or value does not exist)

func (*Values) GetTime added in v0.7.2

func (v *Values) GetTime(key Key) (time.Time, error)

GetTime returns the time for a given key as a string, or error if this is not possible (cannot parse as a time, or value does not exist)

func (*Values) Set

func (v *Values) Set(key Key, value any) error

Set sets the key with the specified value. Note that this value needs to be serialisable to JSON using encoding/json. Set will check this and return an error if it is not serialisable.

func (*Values) SetString added in v0.7.2

func (v *Values) SetString(key Key, value string) *Values

SetString sets the given key with value. If, for some reason, the provided string cannot be serialized as JSON SetString will panic.

func (*Values) SetTime added in v0.7.2

func (v *Values) SetTime(key Key, value time.Time) *Values

SetTime sets the given key with the given time, encoded using RFC3339 (the time format used by common PASETO claims).

Jump to

Keyboard shortcuts

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