cookies

package module
v0.0.0-...-8bbfb5f Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2019 License: MIT Imports: 3 Imported by: 1

README

cookies

Build Status

Signed and unsigned cookies based on Keygrip. It derives from pillarjs/cookies.

API

New(rw ReadWriter, opts *Options)
  • rw ReadWriter for get and set cookie
  • opts.Keys key list for keygrip
  • opts.Path the same as http.Cookie.Path
  • opts.Domain the same as http.Cookie.Domain
  • opts.Expires the same as http.Cookie.Expires
  • opts.MaxAge the same as http.Cookie.MaxAge
  • opts.Secure the same as http.Cookie.Secure
  • opts.HttpOnly the same as http.Cookie.HttpOnly

It will create a cookie instance. The options will use for http.Cookie except Options.Keys.

c := cookies.New(nil, &cookie.Options{
  Keys: []string{
    "A",
  },
  Domain: "aslant.site",
  MaxAge: 3600,
})
CreateCookie(name, value string)

Creeat a http cookie by the opts

c := cookies.New(nil, &cookie.Options{
  Keys: []string{
    "A",
  },
  Domain: "aslant.site",
  MaxAge: 3600,
})
cookie := c.CreateCookie("jt", "random-string")
Get(name string, signed bool)

Get the cookie value, if signed is true, it will verify use keys.

c := cookies.New(nil, opts)
cookieName := "jt"
cookieValue := "myCookie"
jt := c.CreateCookie(cookieName, cookieValue)
r := httptest.NewRequest(http.MethodGet, "http://aslant.site/api/users/me", nil)
r.AddCookie(jt)
w := httptest.NewRecorder()

c.RW = cookies.NewHTTPReadWriter(r, w)

// "" there is not sig cookie exists
fmt.Println(c.Get(cookieName, true))

Set the cookie, the signed is true, it will set a sig cookie too.

r := httptest.NewRequest(http.MethodGet, "http://aslant.site/api/users/me", nil)
w := httptest.NewRecorder()
rw := cookies.NewHTTPReadWriter(r, w)
c := cookies.New(rw, opts)
cookieName := "jt"
cookieValue := "myCookie"
jt := c.CreateCookie(cookieName, cookieValue)
c.Set(jt, true)

test

go test -race -coverprofile=test.out ./... && go tool cover --html=test.out

Documentation

Index

Constants

View Source
const (
	// SigSuffix the signed cookie's suffix
	SigSuffix = ".sig"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cookies

type Cookies struct {
	RW ReadWriter
	// contains filtered or unexported fields
}

Cookies cookies ins

func New

func New(rw ReadWriter, opts *Options) *Cookies

New create a instance of cookies

func (*Cookies) CreateCookie

func (c *Cookies) CreateCookie(name, value string) *http.Cookie

CreateCookie create a cookie

func (*Cookies) Get

func (c *Cookies) Get(name string, signed bool) string

Get get the value of cookie

func (*Cookies) GetKeygrip

func (c *Cookies) GetKeygrip() *keygrip.Keygrip

GetKeygrip get the keygrip instance

func (*Cookies) Set

func (c *Cookies) Set(cookie *http.Cookie, signed bool) (err error)

Set set the cookie

type HTTPReadWriter

type HTTPReadWriter struct {
	// contains filtered or unexported fields
}

HTTPReadWriter http read writer

func NewHTTPReadWriter

func NewHTTPReadWriter(req *http.Request, resp http.ResponseWriter) *HTTPReadWriter

NewHTTPReadWriter new http readwriter

func (*HTTPReadWriter) Cookie

func (h *HTTPReadWriter) Cookie(name string) (*http.Cookie, error)

Cookie get cookie from http request

func (*HTTPReadWriter) SetCookie

func (h *HTTPReadWriter) SetCookie(cookie *http.Cookie) error

SetCookie set the cookie to http response

type Options

type Options struct {
	Keys     []string
	Path     string
	Domain   string
	Expires  time.Time
	MaxAge   int
	Secure   bool
	HttpOnly bool
}

Options init options

type ReadWriter

type ReadWriter interface {
	// Get get the cookie by name
	Cookie(name string) (*http.Cookie, error)
	// SetCookie set the cookie
	SetCookie(cookie *http.Cookie) error
}

ReadWriter cookie reader and writer

Jump to

Keyboard shortcuts

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