-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie.go
More file actions
138 lines (115 loc) · 2.85 KB
/
Copy pathcookie.go
File metadata and controls
138 lines (115 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package webkit
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/dairaga/config"
"github.com/dairaga/log"
"github.com/gorilla/securecookie"
)
var _secureC *securecookie.SecureCookie
var _cookieDomain string
func init() {
_cookieDomain = config.GetString("cookie.domain", "")
hashK := config.GetString("cookie.hash", "")
blockK := config.GetString("cookie.block", "")
if hashK != "" && blockK != "" {
_secureC = securecookie.New([]byte(hashK), []byte(blockK))
}
if _secureC != nil {
log.Debug("init secure cookie")
} else {
log.Debug("init cookie")
}
}
// UseSecureCookie ...
func UseSecureCookie(hashKey, blockKey string) {
_secureC = securecookie.New([]byte(hashKey), []byte(hashKey))
}
// CookieDomain ...
func CookieDomain(domain string) {
_cookieDomain = domain
}
func _encode(name string, value interface{}) (string, error) {
if _secureC != nil {
return _secureC.Encode(name, value)
}
var valuebytes []byte
switch v := value.(type) {
case string:
valuebytes = []byte(v)
case fmt.Stringer:
valuebytes = []byte(v.String())
default:
var err error
valuebytes, err = json.Marshal(value)
if err != nil {
return "", err
}
}
return base64.StdEncoding.EncodeToString(valuebytes), nil
}
func _decode(name, value string, data interface{}) error {
if _secureC != nil {
return _secureC.Decode(name, value, data)
}
databytes, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return err
}
switch v := data.(type) {
case *string:
*v = string(databytes)
return nil
default:
return json.Unmarshal(databytes, data)
}
}
func _cookie(name, val, domain, path string, age int, secure, httpOnly bool) *http.Cookie {
return &http.Cookie{
Name: name,
Value: val,
Domain: domain,
Path: path,
MaxAge: age,
Secure: secure,
HttpOnly: httpOnly,
}
}
// NewCookie ...
func NewCookie(name string, value interface{}, domain, path string, age int, secure, httpOnly bool) (*http.Cookie, error) {
if value == nil {
return _cookie(name, "", domain, path, age, secure, httpOnly), nil
}
val, err := _encode(name, value)
if err != nil {
return nil, err
}
return _cookie(name, val, domain, path, age, secure, httpOnly), nil
}
// DeleteCookie ...
func DeleteCookie(w http.ResponseWriter, name string) {
SetCookie(w, name, nil, -1)
}
// SetCookie ...
func SetCookie(w http.ResponseWriter, name string, value interface{}, maxAge int) error {
c, err := NewCookie(name, value, _cookieDomain, "/", maxAge, false, false)
if err != nil {
return err
}
http.SetCookie(w, c)
return nil
}
// Cookie ...
func Cookie(r *http.Request, name string, data interface{}) error {
if reflect.TypeOf(data).Kind() != reflect.Ptr {
return fmt.Errorf("data must be pointer of something")
}
c, err := r.Cookie(name)
if err != nil {
return err
}
return _decode(name, c.Value, data)
}