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 ¶
- Variables
- type Cookie
- type DomainProvider
- type DomainProviderOption
- type DomainProviderOptions
- type Option
- func WithDomainProvider(v DomainProvider) Option
- func WithExpires(v time.Duration) Option
- func WithHTTPOnly(v bool) Option
- func WithMaxAge(v int) Option
- func WithPath(v string) Option
- func WithSameSite(v http.SameSite) Option
- func WithSecure(v bool) Option
- func WithTimeProvider(v TimeProvider) Option
- type TimeProvider
- type TimeProviderOption
- type TimeProviderOptions
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithHTTPOnly sets the HttpOnly attribute. Defaults to true.
func WithMaxAge ¶
WithMaxAge sets the Max-Age attribute in seconds; see http.Cookie.MaxAge for the meaning of zero and negative values. Defaults to 0.
func WithSameSite ¶
WithSameSite sets the SameSite attribute. Defaults to http.SameSiteDefaultMode.
func WithSecure ¶
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 ¶
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 ¶
TimeProviderOptions configures NewTimeProvider.
func GetDefaultTimeProviderOptions ¶
func GetDefaultTimeProviderOptions() TimeProviderOptions
GetDefaultTimeProviderOptions returns the default options, which use no offset.