-
Notifications
You must be signed in to change notification settings - Fork 40
/
gated.go
55 lines (39 loc) · 1.3 KB
/
gated.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
package middleware
import (
"fmt"
"log"
"net/http"
"github.com/EngineerKamesh/gofullstack/volume2/section6/gopherfaceauth/common/authenticate"
)
func GatedContentHandler(next http.HandlerFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
shouldRedirectToLogin := false
secureCookieMap, err := authenticate.ReadSecureCookieValues(w, r)
if err != nil {
log.Print(err)
}
//fmt.Printf("secure cookie contents: %+v\n", secureCookieMap)
// Check if the sid key which is used to store the session id value
// has been populated in the map using the comma ok idiom
if _, ok := secureCookieMap["sid"]; ok == true {
gfSession, err := authenticate.SessionStore.Get(r, "gopherface-session")
fmt.Printf("gopherface session values: %+v\n", gfSession.Values)
if err != nil {
log.Print(err)
return
}
// Check if the session id stored in the secure cookie matches
// the id and username on the server-side session
if gfSession.Values["sessionID"] == secureCookieMap["sid"] && gfSession.Values["username"] == secureCookieMap["username"] {
next(w, r)
} else {
shouldRedirectToLogin = true
}
} else {
shouldRedirectToLogin = true
}
if shouldRedirectToLogin == true {
http.Redirect(w, r, "/login", 302)
}
})
}