Documentation
¶
Overview ¶
Package cookies provides cookie related utilities
Index ¶
- Constants
- func DeleteCookie(w http.ResponseWriter, name string)
- func GetCookieValue(r *http.Request, name string) string
- func RequestAddCookie(r *http.Request, name, value string, opts ...CookieOpt)
- func SetCookie(w http.ResponseWriter, name, value string, opts ...CookieOpt)
- func SetCookieOneMinute(w http.ResponseWriter, name, value string, opts ...CookieOpt)
- type CookieOpt
- type Encoder
Constants ¶
const (
ErrNoCookie erro.BaseError = "Cookie with name '%s' not found"
)
Variables ¶
This section is empty.
Functions ¶
func DeleteCookie ¶
func DeleteCookie(w http.ResponseWriter, name string)
DeleteCookie will delete a cookie of the specified name from the client browser.
func GetCookieValue ¶
GetCookieValue will read a value from a cookie of the specified name. If the cookie doesn't exist, an empty string is returned.
It is a very simple wrapper over directly calling (*http.Request).Cookie().
func RequestAddCookie ¶
RequestAddCookie will add a cookie directly to a request. Normally SetCookie will set a cookie in the client browser, which will then be sent back to the server for every request. RequestAddCookie operates differently by adding the cookie for a single request. This is useful in tests where there is no client browser to set cookies for, in which case you can just attach the cookie directly to a test request instead.
func SetCookie ¶
func SetCookie(w http.ResponseWriter, name, value string, opts ...CookieOpt)
SetCookie will set a cookie of the specified name and value in the client browser.
It takes in additional options which may change various settings of the cookie, such as the duration of the cookie and whether the cookie should be readable by client side javascript.
func SetCookieOneMinute ¶
func SetCookieOneMinute(w http.ResponseWriter, name, value string, opts ...CookieOpt)
SetCookieOneMinute is a wrapper around SetCookie with the duration of the cookie set to one minute
Types ¶
type CookieOpt ¶
CookieOpt are cookie options that change the nature of the cookie being set.
func AllowJS ¶
AllowJS sets whether the cookie can be read by client side javascript. If there is no reason for javascript to access the cookie, do not turn it on to make the cookie more secure. Default is false.
type Encoder ¶
type Encoder struct {
Key string
}
Encoder encapsulates the secret key that will be used to securely sign the cookies set by EncodeVariableInCookie/ DecodeVariableFromCookie. If you are just setting plain cookies with SetCookie/SetCookieOneMinute, you do not need this.
func (Encoder) DecodeVariableFromCookie ¶
func (ce Encoder) DecodeVariableFromCookie(r *http.Request, cookiename string, variablePtr interface{}) error
DecodeVariableFromCookie will retrieve a go variable from a client side cookie that was previously set by EncodeVariableInCookie. If the named cookie does not exist, it will fail with a ErrNoCookie error. Note that you need to pass a pointer to the variable you wish to decode the cookie value into, not the variable itself.
The cookie's digital hash signature will be checked for any tampering. If the signature is wrong, DecodeVariableFromCookie will fail with an auth.ErrDeserializeOutputInvalid error.
func (Encoder) EncodeVariableInCookie ¶
func (ce Encoder) EncodeVariableInCookie(w http.ResponseWriter, cookiename string, variable interface{}, opts ...CookieOpt) error
EncodeVariableInCookie will serialize any go variable into a string and store it in a client side cookie, where it can later be retrieved from the cookie with DecodeVariableFromCookie.