forked from kennygrant/gohackernews
/
session.go
101 lines (82 loc) · 2.66 KB
/
session.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
package session
import (
"net/http"
"strconv"
"github.com/fragmenta/auth"
"github.com/fragmenta/mux"
"github.com/fragmenta/server/log"
"github.com/kennygrant/gohackernews/src/users"
)
// CurrentUser returns the saved user (or an empty anon user)
// for the current session cookie
func CurrentUser(w http.ResponseWriter, r *http.Request) *users.User {
// Start with an anon user by default (role 0, id 0)
user := &users.User{}
// Build the session from the secure cookie, or create a new one
session, err := auth.Session(w, r)
if err != nil {
//log.Info(log.V{"msg": "session error", "error": err, "status": http.StatusInternalServerError})
return user
}
// Fetch the current user record if we have one recorded in the session
var id int64
val := session.Get(auth.SessionUserKey)
// If we have no value, we have no login
if len(val) == 0 {
//log.Info(log.V{"msg": "session error", "session": session, "status": http.StatusInternalServerError})
return user
}
if len(val) > 0 {
id, err = strconv.ParseInt(val, 10, 64)
if err != nil {
log.Info(log.V{"msg": "session error decoding", "val": val, "error": err, "status": http.StatusInternalServerError})
return user
}
}
if id > 0 {
user, err = users.Find(id)
if err != nil {
log.Info(log.V{"msg": "session error user not found", "user_id": id, "error": err, "status": http.StatusNotFound})
return user
}
}
return user
}
// clearSession clears the request session cookie entirely.
// If an error is encountered in processing params, the session is cleared.
func clearSession(w http.ResponseWriter, r *http.Request) error {
// Clear the session
session, err := auth.SessionGet(r)
if err != nil {
return err
}
session.Clear(w)
return nil
}
// CheckAuthenticity checks the authenticity token in params against cookie -
// The masked token is inserted into forms and POSTS by js.
// The token is inserted into the cookie by the middleware above.
// This is a shortcut for where you don't need params otherwise.
func CheckAuthenticity(w http.ResponseWriter, r *http.Request) error {
// We should never be called on GET requests
if r.Method == http.MethodGet {
return nil
}
// Get the token from params and compare against cookie
params, err := mux.Params(r)
if err != nil {
clearSession(w, r)
return err
}
// log.Info(log.V{"PARAMS": params})
// Get the token from params (it is inserted there by js)
// we do this to allow just one token in the head of every page
token := params.Get(auth.SessionTokenKey)
// Compare that param against the token stored in the session cookie.
err = auth.CheckAuthenticityToken(token, r)
if err != nil {
clearSession(w, r)
return err
}
return nil
}