Documentation
¶
Overview ¶
Package cookie provides a common interface for reading and writing HTTP cookies, including cookies whose values are encrypted.
Index ¶
- func NewRandomEncryptedCookieSalt() (string, error)
- func NewRandomEncryptedCookieSecret() (string, error)
- func NewRandomEncryptedCookieURI(name string) (string, error)
- func RegisterCookie(ctx context.Context, scheme string, f CookieInitializeFunc) error
- func Schemes() []string
- type Config
- type Cookie
- type CookieInitializeFunc
- type EncryptedCookie
- func (c *EncryptedCookie) Delete(rsp http.ResponseWriter) error
- func (c *EncryptedCookie) Get(req *http.Request) (*memguard.LockedBuffer, error)
- func (c *EncryptedCookie) GetString(req *http.Request) (string, error)
- func (c *EncryptedCookie) Set(rsp http.ResponseWriter, buf *memguard.LockedBuffer) error
- func (c *EncryptedCookie) SetString(rsp http.ResponseWriter, value string) error
- func (c *EncryptedCookie) SetStringWithCookie(rsp http.ResponseWriter, value string, http_cookie *http.Cookie) error
- func (c *EncryptedCookie) SetWithCookie(rsp http.ResponseWriter, buf *memguard.LockedBuffer, http_cookie *http.Cookie) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewRandomEncryptedCookieSalt ¶
NewRandomEncryptedCookieSalt returns a random salt value suitable for `encrypted://` cookie URIs.
func NewRandomEncryptedCookieSecret ¶
NewRandomEncryptedCookieSecret returns a random secret value suitable for `encrypted://` cookie URIs.
func NewRandomEncryptedCookieURI ¶
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.
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 ¶
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.
type Cookie ¶
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 ¶
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 ¶
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 ¶
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 ¶
func (c *EncryptedCookie) Get(req *http.Request) (*memguard.LockedBuffer, error)
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 ¶
func (c *EncryptedCookie) Set(rsp http.ResponseWriter, buf *memguard.LockedBuffer) error
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'.