-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware.go
88 lines (80 loc) · 3.04 KB
/
middleware.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
/***
* Middleware for dealing with login/session cookies
*/
package main
import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"log"
"net/http"
)
// ensureLoggedIn tests if the user is logged in, reading in from the context to see if a flag is set.
// Note that this flag is not a boolean any more, I'm using this pseudo-flag to store the username
func ensureLoggedIn() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
loggedInInterface := session.Get("Username")
if loggedInInterface == nil || loggedInInterface == "" {
if *config["ginMode"] == "debug" {
log.Printf("[INFO]: ensureLoggedIn(): No authenticated user")
}
c.Abort()
c.HTML(http.StatusOK, "404.tpl", environment(c,
gin.H{
"errorcode": http.StatusUnauthorized,
"errortext": http.StatusText(http.StatusUnauthorized),
"errorbody": "You must be authenticated to continue",
"titleCommon": *config["titleCommon"] + " - " + http.StatusText(http.StatusUnauthorized),
"logintemplate": false,
}))
//c.AbortWithStatus(http.StatusUnauthorized)
} else {
if *config["ginMode"] == "debug" {
log.Printf("[INFO]: ensureLoggedIn(): Username is %q", loggedInInterface)
}
}
}
}
// ensureNotLoggedIn tests if the user is NOT logged in, reading in from the context to see if a flag is set.
func ensureNotLoggedIn() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
loggedInInterface := session.Get("Username")
if loggedInInterface != nil && loggedInInterface != "" {
if *config["ginMode"] == "debug" {
log.Printf("[INFO]: ensureNotLoggedIn(): Username is %q", loggedInInterface)
}
c.Abort()
c.HTML(http.StatusOK, "404.tpl", environment(c,
gin.H{
"errorcode": http.StatusUnauthorized,
"errortext": "Already authenticated",
"errorbody": "You have already logged in!",
"titleCommon": *config["titleCommon"] + " - Already authenticated",
"logintemplate": false,
}))
// c.AbortWithStatus(http.StatusUnauthorized)
} else {
if *config["ginMode"] == "debug" {
log.Printf("[INFO]: ensureNotLoggedIn(): No authenticated user")
}
}
}
}
// setUserStatus gets loaded for each page, and sees if the cookie is set. This seems to be the 'correct' way to do this under Gin.
func setUserStatus() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
// Note that all the things below may set everything to empty strings, which is FINE! (gwyneth 20200628)
c.Set("Username", session.Get("Username"))
c.Set("Email", session.Get("Email"))
c.Set("Libravatar", session.Get("Libravatar"))
c.Set("Token", session.Get("Token"))
c.Set("UUID", session.Get("UUID"))
c.Set("RememberMe", session.Get("RememberMe"))
c.Set("sidebarCollapsed", session.Get("sidebarCollapsed"))
if *config["ginMode"] == "debug" {
log.Printf("[INFO]: setUserStatus(): Authenticated? %q (username) Cookie token: %q Libravatar: %q", session.Get("Username"), session.Get("Token"), session.Get("Libravatar"))
}
}
}