-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.go
52 lines (45 loc) · 1.02 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
package main
import (
"net/http"
"github.com/gorilla/securecookie"
)
var (
cookieHandler = securecookie.New(
[]byte("in0y(>'@N+#N6A5*=iL%lM}[U`|AH#8ltj@02>e9gwsU&Wu'JNuhCRFPri7Z{*H1"),
[]byte("zy-hA<!J(oXKp]sy]HoJkiuqK_R&8EnB"))
)
const (
cookieName = "auth"
cookieKey = "auth"
)
func getAuth(r *http.Request) (auth string) {
if cookie, err := r.Cookie(cookieName); err == nil {
cookieValue := make(map[string]string)
if err = cookieHandler.Decode(cookieName, cookie.Value, &cookieValue); err == nil {
auth = cookieValue[cookieKey]
}
}
return auth
}
func setSession(auth string, w http.ResponseWriter) {
value := map[string]string{
cookieKey: auth,
}
if encoded, err := cookieHandler.Encode(cookieName, value); err == nil {
cookie := &http.Cookie{
Name: cookieName,
Value: encoded,
Path: "/",
}
http.SetCookie(w, cookie)
}
}
func clearSession(w http.ResponseWriter) {
cookie := &http.Cookie{
Name: cookieName,
Value: "",
Path: "/",
MaxAge: -1,
}
http.SetCookie(w, cookie)
}