cookiestore

package
v0.0.0-...-d31700d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Example (Base64encode)
fmt.Printf("%s\n", base64encode([]byte("a")))
fmt.Printf("%s\n", base64encode([]byte("ab")))
fmt.Printf("%s\n", base64encode([]byte("abc")))
Output:

YQ
YWI
YWJj

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrorEncodedValueTooLong = errors.New("the encoded value is too long")
View Source
var ErrorSessionExpired = errors.New("the session has expired")
View Source
var ErrorSignWrong = errors.New("the sign is wrong")
View Source
var ErrorValueToDecodeIllegal = errors.New("the value to decode is illegal")
View Source
var ErrorValueToDecodeToolong = errors.New("the value to decode is too long")

Functions

This section is empty.

Types

type CookieStore

type CookieStore struct {
	*SecureCookie
}

func New

func New(secret string) *CookieStore

func (*CookieStore) Delete

func (cs *CookieStore) Delete(res http.ResponseWriter, cookie *http.Cookie)
Example
ck := &http.Cookie{Name: "name", MaxAge: 1}

rw := testResponseWriter{make(http.Header)}
s := New("")
s.Delete(rw, ck)
fmt.Println(rw.header)
Output:

map[Set-Cookie:[name=; Expires=Thu, 01 Jan 1970 00:00:01 GMT; Max-Age=0]]

func (*CookieStore) EncodeData

func (cs *CookieStore) EncodeData(cookieName string, data interface{}) ([]byte, error)
Example
s := New("")
fmt.Println(s.EncodeData("name", func() {}))
Output:

[] json: unsupported type: func()

func (CookieStore) Get

func (cs CookieStore) Get(ck *http.Cookie, pointer interface{}) error
Example
s := New("test-hash-key")
type session struct {
	UserId   int    `json:"userId"`
	UserName string `json:"userName"`
}
var data session

ck := &http.Cookie{
	Name:  "name",
	Value: "MXx7InVzZXJJZCI6MTAwMiwidXNlck5hbWUiOiLpn6nmooXmooUifXw8Gskt8KnwT8hJ31mCyxsbBIqWIET52AV5PhPK5-U9_w",
}
data = session{}
if err := s.Get(ck, &data); err != nil {
	fmt.Println(err)
} else {
	fmt.Println(data)
}

ck = &http.Cookie{
	Name:  "name",
	Value: "xyz",
}
data = session{}
if err := s.Get(ck, &data); err != nil {
	fmt.Println(err)
} else {
	fmt.Println(data)
}

ck = &http.Cookie{
	Name:  "name",
	Value: "",
}
data = session{}
if err := s.Get(ck, &data); err != nil {
	fmt.Println(err)
} else {
	fmt.Println(data)
}
Output:

{1002 韩梅梅}
the value to decode is illegal
{0 }

func (*CookieStore) Save

func (cs *CookieStore) Save(res http.ResponseWriter, cookie *http.Cookie, data interface{}) error
Example
ck := &http.Cookie{Name: "name", MaxAge: 86400}
data := map[string]interface{}{"userId": 1002, "userName": "韩梅梅"}

rw := testResponseWriter{make(http.Header)}
s := New("test-hash-key")
s.SecureCookie.timestampForTest = 1
if err := s.Save(rw, ck, data); err != nil {
	fmt.Println(err)
} else {
	fmt.Println(rw.header)
}

s.SecureCookie.MaxLength = 50
fmt.Println(s.Save(rw, ck, data))
Output:

map[Set-Cookie:[name=MXx7InVzZXJJZCI6MTAwMiwidXNlck5hbWUiOiLpn6nmooXmooUifXw8Gskt8KnwT8hJ31mCyxsbBIqWIET52AV5PhPK5-U9_w; Max-Age=86400]]
the encoded value is too long

type SecureCookie

type SecureCookie struct {
	MaxLength int
	// contains filtered or unexported fields
}
Example (HasHashKey)
testSecureCookie(
	[]byte("test-hash-key"), "name", []byte(`{"userId":1002,"userName":"韩梅梅"}`),
)
Output:

MXx7InVzZXJJZCI6MTAwMiwidXNlck5hbWUiOiLpn6nmooXmooUifXw8Gskt8KnwT8hJ31mCyxsbBIqWIET52AV5PhPK5-U9_w
1|{"userId":1002,"userName":"韩梅梅"}|[60 26 201 45 240 169 240 79 200 73 223 89 130 203 27 27 4 138 150 32 68 249 216 5 121 62 19 202 231 229 61 255]
{"userId":1002,"userName":"韩梅梅"}
illegal base64 data at input byte 96
the session has expired
the sign is wrong
the sign is wrong
Example (NoHashKey)
testSecureCookie(nil, "name", []byte("value"))
Output:

MXx2YWx1ZXwr5qm7mYaggd_4i5UIfT3M8MF1wpRnYxhR-Q0Sz2W5eA
1|value|[43 230 169 187 153 134 160 129 223 248 139 149 8 125 61 204 240 193 117 194 148 103 99 24 81 249 13 18 207 101 185 120]
value
illegal base64 data at input byte 52
the session has expired
the sign is wrong
the sign is wrong
Example (VerifyAndRemoveSign)
s := NewSecureCookie(nil)

_, err := s.verifyAndRemoveSign("", []byte("1|v|1234567890123456789012345678901"))
fmt.Println(err)

_, err = s.verifyAndRemoveSign("", []byte("|v|12345678901234567890123456789012"))
fmt.Println(err)

_, err = s.verifyAndRemoveSign("", []byte("1||12345678901234567890123456789012"))
fmt.Println(err)

_, err = s.verifyAndRemoveSign("", []byte("1|v|12345678901234567890123456789012"))
fmt.Println(err)
Output:

the value to decode is illegal
the value to decode is illegal
the value to decode is illegal
the sign is wrong
Example (VerifyAndRemoveTimestamp)
s := NewSecureCookie(nil)

_, err := s.verifyAndRemoveTimestamp([]byte("|v"), 0)
fmt.Println(err)

_, err = s.verifyAndRemoveTimestamp([]byte("a|"), 0)
fmt.Println(err)

s.timestampForTest = 9
_, err = s.verifyAndRemoveTimestamp([]byte("1|v"), 7)
fmt.Println(err)
Output:

the value to decode is illegal
the value to decode is illegal
the session has expired

func NewSecureCookie

func NewSecureCookie(hashKey []byte) *SecureCookie

func (*SecureCookie) Decode

func (s *SecureCookie) Decode(name string, value []byte, maxAge int64) ([]byte, error)

Decode decodes a cookie value.

It decodes the value, and verifies a message authentication code.

The name argument is the cookie name. It must be the same name used when it was stored. The value argument is the encoded cookie value. The maxAge argument is the max seconds since the value is generated.

Example (TooLong)
s := NewSecureCookie(nil)
s.MaxLength = 30
_, err := s.Decode("", base64encode([]byte("1|v|1234567890123456789012345678901")), 0)
fmt.Println(err)
Output:

the value to decode is too long

func (*SecureCookie) Encode

func (s *SecureCookie) Encode(name string, value []byte) ([]byte, error)

Encode encodes a cookie value.

It signs the value with a message authentication code, and encodes it.

The name argument is the cookie name. It is signed with the encoded value. The value argument is the value to be encoded.

Example (TooLong)
s := NewSecureCookie(nil)
s.MaxLength = 30
_, err := s.Encode("", []byte("value"))
fmt.Println(err)
Output:

the encoded value is too long

func (*SecureCookie) GetTimestamp

func (s *SecureCookie) GetTimestamp(value []byte) (int64, error)
Example
fmt.Println(
	NewSecureCookie([]byte("test-hash-key")).GetTimestamp(
		[]byte("MTU4ODc1NjkxMXx7IlVzZXJJZCI6MjE3NjYsIkNvbXBhbnlJZCI6NzQwNCwiU3RhZmZJ"),
	),
)
Output:

1588756911 <nil>

Jump to

Keyboard shortcuts

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