cookie

package
v0.36.0 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: MIT Imports: 5 Imported by: 3 modules, 2 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.

Documentation

Overview

Package cookie provides a declarative helper to read, write and delete an HTTP cookie with consistent attributes.

A Cookie is created with New and functional Option values. Its domain is resolved per request by a DomainProvider, which can restrict cookies to a set of allowed domains, and its expiry is computed from a TimeProvider:

c := cookie.New("session",
	cookie.WithExpires(24*time.Hour),
	cookie.WithDomainProvider(cookie.NewDomainProvider(
		cookie.DomainProviderWithDomains("*.example.com"),
	)),
)
if _, err := c.Set(w, r, value); err != nil {
	// handle error
}

Index

Constants

This section is empty.

Variables

View Source
var ErrDomainNotAllowed = errors.New("domain not allowed")

ErrDomainNotAllowed is returned by a DomainProvider created by NewDomainProvider when the request domain matches none of the allowed domains.

Functions

This section is empty.

Types

type Cookie struct {
	// Name the name of the cookie
	Name string
	// Path the path of the created cookie
	Path string
	// Secure the secure flag of the created cookie
	Secure bool
	// MaxAge the max age flag of the created cookie
	MaxAge int
	// Expires the expires flag of the created cookie
	Expires time.Duration
	// SameSite the same site flag of the created cookie
	SameSite http.SameSite
	// HTTPOnly the http only of the created cookie
	HTTPOnly bool
	// TimeProvider function to retrieve the now time for the expires flag of the created cookie
	TimeProvider TimeProvider
	// DomainProvider function to retrieve the domain flag of the created cookie
	DomainProvider DomainProvider
}

Cookie describes a named HTTP cookie and the attributes used when it is written by Cookie.Set. Create one with New.

func New

func New(name string, opts ...Option) Cookie

New returns a Cookie with the given name, configured by opts. Without options the cookie uses path "/", is Secure and HttpOnly, and uses http.SameSiteDefaultMode. Nil options are ignored.

func (Cookie) Delete

func (c Cookie) Delete(w http.ResponseWriter, r *http.Request) error

Delete expires the cookie on the client by writing it with an empty value and a negative Max-Age. It does nothing if r does not carry the cookie and returns any error from the domain provider.

func (Cookie) Get

func (c Cookie) Get(r *http.Request) (*http.Cookie, error)

Get returns the cookie from r, or http.ErrNoCookie if it is not present.

func (Cookie) Set

func (c Cookie) Set(w http.ResponseWriter, r *http.Request, value string, opts ...Option) (*http.Cookie, error)

Set writes the cookie with the given value to w and returns it. opts override the cookie's attributes for this call only; the name and domain provider of c are always used. It returns an error if the domain provider rejects the request's domain.

type DomainProvider

type DomainProvider func(r *http.Request) (string, error)

DomainProvider returns the cookie domain to use for the request r.

func NewDomainProvider

func NewDomainProvider(opts ...DomainProviderOption) DomainProvider

NewDomainProvider returns a DomainProvider that derives the domain from the request using keelhttp.GetRequestDomain. If allowed domains are configured, the domain is first replaced by its mapping, if any, and ErrDomainNotAllowed is returned unless it matches one of them.

type DomainProviderOption

type DomainProviderOption func(options *DomainProviderOptions)

DomainProviderOption configures DomainProviderOptions.

func DomainProviderWithDomains

func DomainProviderWithDomains(v ...string) DomainProviderOption

DomainProviderWithDomains sets the allowed domains.

func DomainProviderWithMappings

func DomainProviderWithMappings(v map[string]string) DomainProviderOption

DomainProviderWithMappings sets the domain mappings.

type DomainProviderOptions

type DomainProviderOptions struct {
	// Domains lists the allowed domains; a "*." prefix also matches any
	// subdomain. An empty list allows every domain.
	Domains []string
	// Mappings maps a request domain to the cookie domain to use instead.
	// It is only consulted when Domains is not empty.
	Mappings map[string]string
}

DomainProviderOptions configures NewDomainProvider.

func GetDefaultDomainProviderOptions

func GetDefaultDomainProviderOptions() DomainProviderOptions

GetDefaultDomainProviderOptions returns the default options, which allow every domain.

type Option

type Option func(options *Cookie)

Option configures a Cookie in New or overrides its attributes in Cookie.Set.

func WithDomainProvider

func WithDomainProvider(v DomainProvider) Option

WithDomainProvider sets the function resolving the Domain attribute from the request. Defaults to NewDomainProvider without options.

func WithExpires

func WithExpires(v time.Duration) Option

WithExpires sets the lifetime used to compute the Expires attribute relative to the TimeProvider. Non-positive values omit Expires, which is the default.

func WithHTTPOnly

func WithHTTPOnly(v bool) Option

WithHTTPOnly sets the HttpOnly attribute. Defaults to true.

func WithMaxAge

func WithMaxAge(v int) Option

WithMaxAge sets the Max-Age attribute in seconds; see http.Cookie.MaxAge for the meaning of zero and negative values. Defaults to 0.

func WithPath

func WithPath(v string) Option

WithPath sets the Path attribute. Defaults to "/".

func WithSameSite

func WithSameSite(v http.SameSite) Option

WithSameSite sets the SameSite attribute. Defaults to http.SameSiteDefaultMode.

func WithSecure

func WithSecure(v bool) Option

WithSecure sets the Secure attribute. Defaults to true.

func WithTimeProvider

func WithTimeProvider(v TimeProvider) Option

WithTimeProvider sets the clock used to compute the Expires attribute. Defaults to NewTimeProvider without options.

type TimeProvider

type TimeProvider func() time.Time

TimeProvider returns the current time used to compute cookie expiry.

func NewTimeProvider

func NewTimeProvider(opts ...TimeProviderOption) TimeProvider

NewTimeProvider returns a TimeProvider reporting time.Now plus the configured offset.

type TimeProviderOption

type TimeProviderOption func(options *TimeProviderOptions)

TimeProviderOption configures TimeProviderOptions.

func TimeProviderWithOffset

func TimeProviderWithOffset(v time.Duration) TimeProviderOption

TimeProviderWithOffset sets the offset added to the current time. Defaults to 0.

type TimeProviderOptions

type TimeProviderOptions struct {
	// Offset is added to the current time.
	Offset time.Duration
}

TimeProviderOptions configures NewTimeProvider.

func GetDefaultTimeProviderOptions

func GetDefaultTimeProviderOptions() TimeProviderOptions

GetDefaultTimeProviderOptions returns the default options, which use no offset.

Jump to

Keyboard shortcuts

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