cookies

package
v0.0.0-...-baf4ea5 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package cookies provides cookie related utilities

Index

Constants

View Source
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

func GetCookieValue(r *http.Request, name string) string

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

func RequestAddCookie(r *http.Request, name, value string, opts ...CookieOpt)

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

type CookieOpt func(*http.Cookie) *http.Cookie

CookieOpt are cookie options that change the nature of the cookie being set.

func AllowJS

func AllowJS(allowJS bool) CookieOpt

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.

func Duration

func Duration(duration time.Duration) CookieOpt

Duration sets how long the cookie is valid for. Default is three months.

func MustHTTPS

func MustHTTPS(mustHttps bool) CookieOpt

MustHTTPS sets whether the cookie must be sent over a HTTPS connection. 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 NewEncoder

func NewEncoder(key string) Encoder

NewEncoder returns a new Encoder.

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.

Jump to

Keyboard shortcuts

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