-
Notifications
You must be signed in to change notification settings - Fork 4
/
cookies.go
58 lines (46 loc) · 1 KB
/
cookies.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
package disguard // import "go.zeta.pm/disguard"
import (
"net/http"
"time"
)
// GetSession ...
func (o *Session) getSession(r *http.Request) (*User, error) {
cookie, err := r.Cookie("session")
if err != nil {
return nil, err
}
var user User
if err = o.cookieHandler.Decode("session", cookie.Value, &user); err != nil {
return nil, err
}
return &user, nil
}
// SetSession ...
func (o *Session) setSession(w http.ResponseWriter, user User) error {
encoded, err := o.cookieHandler.Encode("session", user)
if err != nil {
return err
}
cookie := &http.Cookie{
Name: "session",
Value: encoded,
MaxAge: 60 * 60 * 24 * 7,
Expires: time.Now().Add(7 * 24 * time.Hour),
Path: "/",
HttpOnly: true,
}
http.SetCookie(w, cookie)
return nil
}
// ClearSession ...
func (o *Session) clearSession(w http.ResponseWriter) error {
cookie := &http.Cookie{
Name: "session",
Value: "",
MaxAge: -1,
Path: "/",
HttpOnly: true,
}
http.SetCookie(w, cookie)
return nil
}