cookie

package module
v3.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2018 License: MIT Imports: 8 Imported by: 0

README

Build Codecov ReportCard GoDoc License

Quick Start

Download and install
$ go get -u -v github.com/go-session/cookie
Create file server.go
package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/go-session/cookie"
	"github.com/go-session/session"
)

var (
	hashKey = []byte("FF51A553-72FC-478B-9AEF-93D6F506DE91")
)

func main() {
	session.InitManager(
		session.SetStore(
			cookie.NewCookieStore(
				cookie.SetCookieName("demo_cookie_store_id"),
				cookie.SetHashKey(hashKey),
			),
		),
	)

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		store.Set("foo", "bar")
		err = store.Save()
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		http.Redirect(w, r, "/foo", 302)
	})

	http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) {
		store, err := session.Start(context.Background(), w, r)
		if err != nil {
			fmt.Fprint(w, err)
			return
		}

		foo, ok := store.Get("foo")
		if !ok {
			fmt.Fprint(w, "does not exist")
			return
		}

		fmt.Fprintf(w, "foo:%s", foo)
	})

	http.ListenAndServe(":8080", nil)
}
Build and run
$ go build server.go
$ ./server
Open in your web browser

http://localhost:8080

foo:bar

MIT License

Copyright (c) 2018 Lyric

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCookieStore

func NewCookieStore(opt ...Option) session.ManagerStore

NewCookieStore Create an instance of a cookie store

Types

type Option

type Option func(*options)

Option A cookie parameter options

func SetBlockFunc

func SetBlockFunc(blockFunc func([]byte) (cipher.Block, error)) Option

SetBlockFunc sets the encryption function used to create a cipher.Block

func SetBlockKey

func SetBlockKey(blockKey []byte) Option

SetBlockKey used to encrypt values

func SetCookieName

func SetCookieName(cookieName string) Option

SetCookieName Set the cookie name

func SetHashFunc

func SetHashFunc(hashFunc func() hash.Hash) Option

SetHashFunc sets the hash function used to create HMAC

func SetHashKey

func SetHashKey(hashKey []byte) Option

SetHashKey used to authenticate values using HMAC

func SetMaxAge

func SetMaxAge(maxAge int) Option

SetMaxAge restricts the maximum age, in seconds, for the cookie value

func SetMaxLength

func SetMaxLength(maxLength int) Option

SetMaxLength restricts the maximum length, in bytes, for the cookie value

func SetMinAge

func SetMinAge(minAge int) Option

SetMinAge restricts the minimum age, in seconds, for the cookie value

func SetSecure

func SetSecure(secure bool) Option

SetSecure Set cookie security

Jump to

Keyboard shortcuts

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