cookie

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: BSD-3-Clause Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go package for working with HTTP cookies.

The Cookie interface reads, writes and deletes cookies (as strings or as memguard.LockedBuffer values). Implementations register a URI scheme and are created with NewCookie. The Config type (created by NewConfig) derives cookie attributes (name, domain, path, Secure, SameSite and TTL) from a URI.

Schemes

encrypted://?name={NAME}&secret={SECRET}&salt={SALT}

Cookies whose values are encrypted and authenticated using NaCl secretbox. The secret must be exactly 32 bytes. NewRandomEncryptedCookieURI will generate a URI with random values.

Example

Error handling omitted for the sake of brevity.

package main

import (
	"fmt"
	
	"github.com/aaronland/go-http/v5/cookie"	
)

func main() {

	name := "c"
	secret := "0123456789abcdef0123456789abcdef" // must be exactly 32 bytes
	salt := "s4lty" // required but unused; the secret is used directly as the key
	
	cookie_uri := fmt.Sprintf("encrypted://?name=%s&secret=%s&salt=%s", name, secret, salt)
	ck, _ := cookie.NewCookie(ctx, cookie_uri)
}

See also

Documentation

Overview

Package cookie provides a common interface for reading and writing HTTP cookies, including cookies whose values are encrypted.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRandomEncryptedCookieSalt

func NewRandomEncryptedCookieSalt() (string, error)

NewRandomEncryptedCookieSalt returns a random salt value suitable for `encrypted://` cookie URIs.

func NewRandomEncryptedCookieSecret

func NewRandomEncryptedCookieSecret() (string, error)

NewRandomEncryptedCookieSecret returns a random secret value suitable for `encrypted://` cookie URIs.

func NewRandomEncryptedCookieURI

func NewRandomEncryptedCookieURI(name string) (string, error)

NewRandomEncryptedCookieURI returns a new URI for creating an `EncryptedCookie` instance for 'name' with random values for the required secret and salt.

func RegisterCookie

func RegisterCookie(ctx context.Context, scheme string, f CookieInitializeFunc) error

RegisterCookie associates 'scheme' with 'f' in an internal list of available `Cookie` implementations.

func Schemes

func Schemes() []string

Schemes returns the list of schemes that have been "registered".

Types

type Config

type Config struct {

	// Name is the name of the cookie.
	Name string
	// Domain is the cookie's domain.
	Domain string
	// Path is the cookie's path.
	Path string
	// Secure indicates whether the cookie has the "Secure" attribute set.
	Secure bool
	// SameSite is the cookie's "SameSite" mode.
	SameSite http.SameSite
	// TTL is the optional lifetime of the cookie, used to derive its expiry time.
	TTL *duration.Duration
	// contains filtered or unexported fields
}

Config defines the properties used to create new `http.Cookie` instances.

See also: https://github.com/golang/go/issues/25194

func NewConfig

func NewConfig(ctx context.Context, uri string) (*Config, error)

NewConfig returns a new `Config` derived from 'uri', which is expected to take the form of:

https://example.com/?name=test&ttl=PT1H&samesite=strict

The "name" parameter is required. The optional "ttl" parameter is an ISO 8601 duration and "samesite" may be one of "lax", "strict", "none" or "default". The "Secure" flag is set if the scheme is "https" and the host and path (if present) are used as the cookie's domain and path.

func (*Config) NewCookie

func (cfg *Config) NewCookie(ctx context.Context, value string) (*http.Cookie, error)

NewCookie returns a new `http.Cookie` with 'value' and the properties defined in 'cfg'.

type Cookie interface {
	// Get returns the value of an HTTP cookie as a `awnumar/memguard.LockedBuffer` instance.
	Get(*http.Request) (*memguard.LockedBuffer, error)
	// GetString returns the value of an HTTP cookie as a string.
	GetString(*http.Request) (string, error)
	// Set assigns the value of an HTTP cookie from a `awnumar/memguard.LockedBuffer` instance.
	Set(http.ResponseWriter, *memguard.LockedBuffer) error
	// SetString assigns the value of an HTTP cookie from a string.
	SetString(http.ResponseWriter, string) error
	// SetWithCookie assigns the value of a specific HTTP cookie from a `awnumar/memguard.LockedBuffer` instance.
	SetWithCookie(http.ResponseWriter, *memguard.LockedBuffer, *http.Cookie) error
	// SetStringWithCookie assigns the value of a specific HTTP cookie from a string.
	SetStringWithCookie(http.ResponseWriter, string, *http.Cookie) error
	// Delete removes the cookie from an HTTP response.
	Delete(http.ResponseWriter) error
}

Cookie is an interface for working with HTTP cookies.

func NewCookie

func NewCookie(ctx context.Context, uri string) (Cookie, error)

NewCookie returns a new instance of `Cookie` for the scheme associated with 'uri'. It is assumed that this scheme will have previously been "registered" with the `RegisterCookie` method.

func NewEncryptedCookie

func NewEncryptedCookie(ctx context.Context, uri string) (Cookie, error)

NewEncryptedCookie returns a new `EncryptedCookie` instance derived from 'uri' which is expected to take the form of:

encrypted://?name={NAME}&secret={SECRET}&salt={SALT}

Where `{NAME}` is the name of the cookie for all subsequent operations; `{SECRET}` is the secret key used to encrypt the value of the cookie and must be exactly 32 bytes long. `{SALT}` is required for backwards compatibility but is not used: the secret is used directly as a NaCl secretbox key.

type CookieInitializeFunc

type CookieInitializeFunc = roster.Factory[Cookie]

CookieInitializeFunc is a function used to initialize an implementation of the `Cookie` interface.

type EncryptedCookie

type EncryptedCookie struct {
	Cookie
	// contains filtered or unexported fields
}

EncryptedCookie implements the `Cookie` interface for working with cookies whose values have been encrypted.

func (*EncryptedCookie) Delete

func (c *EncryptedCookie) Delete(rsp http.ResponseWriter) error

Delete removes the cookie associated with 'c' from 'rsp'. The expiring cookie uses the path "/" so it will only clear cookies that were assigned using `Set` or `SetString` (or a `http.Cookie` with the same path).

func (*EncryptedCookie) Get

Get returns the value of the cookie associated with 'c' in 'req' as an unencrypted `memguard.LockedBuffer` instance.

func (*EncryptedCookie) GetString

func (c *EncryptedCookie) GetString(req *http.Request) (string, error)

GetString returns the value of the cookie associated with 'c' in 'req' as an unencrypted string.

func (*EncryptedCookie) Set

Set assigns 'buf' as an encrypted string to a cookie associated with 'c' to 'rsp'.

func (*EncryptedCookie) SetString

func (c *EncryptedCookie) SetString(rsp http.ResponseWriter, value string) error

SetString assigns 'value' as an encrypted string to a cookie associated with 'c' to 'rsp'.

func (*EncryptedCookie) SetStringWithCookie

func (c *EncryptedCookie) SetStringWithCookie(rsp http.ResponseWriter, value string, http_cookie *http.Cookie) error

SetStringWithCookie assigns 'value' as an encrypted string to 'http_cookie' which is assigned to 'rsp'.

func (*EncryptedCookie) SetWithCookie

func (c *EncryptedCookie) SetWithCookie(rsp http.ResponseWriter, buf *memguard.LockedBuffer, http_cookie *http.Cookie) error

SetWithCookie assigns 'buf' as an encrypted string to 'http_cookie' which is assigned to 'rsp'.

Jump to

Keyboard shortcuts

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