forked from kataras/go-sessions
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcookie.go
233 lines (204 loc) · 6.66 KB
/
cookie.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package sessions
import (
"bytes"
"encoding/base64"
"encoding/gob"
"github.com/valyala/fasthttp"
"math/rand"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
var (
// CookieExpireDelete may be set on Cookie.Expire for expiring the given cookie.
CookieExpireDelete = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
// CookieExpireUnlimited indicates that the cookie doesn't expire.
CookieExpireUnlimited = time.Now().AddDate(23, 0, 0)
)
// GetCookie returns cookie's value by it's name
// returns empty string if nothing was found
func GetCookie(name string, req *http.Request) string {
c, err := req.Cookie(name)
if err != nil {
return ""
}
return c.Value
}
// AddCookie adds a cookie
func AddCookie(cookie *http.Cookie, res http.ResponseWriter) {
if v := cookie.String(); v != "" {
res.Header().Add("Set-Cookie", v)
}
}
// RemoveCookie deletes a cookie by it's name/key
func RemoveCookie(name string, res http.ResponseWriter, req *http.Request) {
c, err := req.Cookie(name)
if err != nil {
return
}
c.Expires = CookieExpireDelete
c.MaxAge = -1
c.Value = ""
c.Path = "/"
AddCookie(c, res)
}
var cookiePool sync.Pool
// AcquireCookie returns an empty Cookie object from the pool.
//
// The returned object may be returned back to the pool with ReleaseCookie.
// This allows reducing GC load.
func AcquireCookie() *http.Cookie {
v := cookiePool.Get()
if v == nil {
return &http.Cookie{}
}
cookie := v.(*http.Cookie)
cookie.HttpOnly = true
cookie.Path = ""
cookie.HttpOnly = false
cookie.Name = ""
cookie.Raw = ""
cookie.Value = ""
cookie.Domain = ""
cookie.MaxAge = -1
cookie.Expires = CookieExpireUnlimited
return cookie
}
// ReleaseCookie returns the Cookie object acquired with AcquireCookie back
// to the pool.
//
// Do not access released Cookie object, otherwise data races may occur.
func ReleaseCookie(cookie *http.Cookie) {
cookiePool.Put(cookie)
}
// FastHTTP
// GetFasthttpCookie returns cookie's value by it's name
// returns empty string if nothing was found
func GetFasthttpCookie(name string, reqCtx *fasthttp.RequestCtx) (val string) {
bcookie := reqCtx.Request.Header.Cookie(name)
if bcookie != nil {
val = string(bcookie)
}
return
}
// AddFasthttpCookie adds a cookie to the client
func AddFasthttpCookie(c *fasthttp.Cookie, reqCtx *fasthttp.RequestCtx) {
reqCtx.Response.Header.SetCookie(c)
}
// RemoveFasthttpCookie deletes a cookie by it's name/key
func RemoveFasthttpCookie(name string, reqCtx *fasthttp.RequestCtx) {
reqCtx.Response.Header.DelCookie(name)
cookie := fasthttp.AcquireCookie()
//cookie := &fasthttp.Cookie{}
cookie.SetKey(name)
cookie.SetValue("")
cookie.SetPath("/")
cookie.SetHTTPOnly(true)
exp := time.Now().Add(-time.Duration(1) * time.Minute) //RFC says 1 second, but let's do it 1 minute to make sure is working...
cookie.SetExpire(exp)
AddFasthttpCookie(cookie, reqCtx)
fasthttp.ReleaseCookie(cookie)
// delete request's cookie also, which is temporarly available
reqCtx.Request.Header.DelCookie(name)
}
// IsValidCookieDomain returns true if the receiver is a valid domain to set
// valid means that is recognised as 'domain' by the browser, so it(the cookie) can be shared with subdomains also
func IsValidCookieDomain(domain string) bool {
if domain == "0.0.0.0" || domain == "127.0.0.1" {
// for these type of hosts, we can't allow subdomains persistance,
// the web browser doesn't understand the mysubdomain.0.0.0.0 and mysubdomain.127.0.0.1 mysubdomain.32.196.56.181. as scorrectly ubdomains because of the many dots
// so don't set a cookie domain here, let browser handle this
return false
}
dotLen := strings.Count(domain, ".")
if dotLen == 0 {
// we don't have a domain, maybe something like 'localhost', browser doesn't see the .localhost as wildcard subdomain+domain
return false
}
if dotLen >= 3 {
if lastDotIdx := strings.LastIndexByte(domain, '.'); lastDotIdx != -1 {
// chekc the last part, if it's number then propably it's ip
if len(domain) > lastDotIdx+1 {
_, err := strconv.Atoi(domain[lastDotIdx+1:])
if err == nil {
return false
}
}
}
}
return true
}
func encodeCookieValue(value string) string {
return base64.URLEncoding.EncodeToString([]byte(value))
}
func decodeCookieValue(value string) (string, error) {
v, err := base64.URLEncoding.DecodeString(value)
if err != nil {
return "", err
}
return string(v), nil
}
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
// ----------------------------------Strings & Serialization----------------------------
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
var src = rand.NewSource(time.Now().UnixNano())
// Random takes a parameter (int) and returns random slice of byte
// ex: var randomstrbytes []byte; randomstrbytes = Random(32)
// note: this code doesn't belongs to me, but it works just fine*
func Random(n int) []byte {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return b
}
// RandomString accepts a number(10 for example) and returns a random string using simple but fairly safe random algorithm
func RandomString(n int) string {
return string(Random(n))
}
// Serialize serialize any type to gob bytes and after returns its the base64 encoded string
func Serialize(m interface{}) (string, error) {
b := bytes.Buffer{}
encoder := gob.NewEncoder(&b)
err := encoder.Encode(m)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
// Deserialize accepts an encoded string and a data struct which will be filled with the desierialized string
// using gob decoder
func Deserialize(str string, m interface{}) error {
by, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return err
}
b := bytes.Buffer{}
b.Write(by)
d := gob.NewDecoder(&b)
// d := gob.NewDecoder(bytes.NewBufferString(str))
err = d.Decode(&m)
if err != nil {
return err
}
return nil
}