-
Notifications
You must be signed in to change notification settings - Fork 1
/
session_middleware.go
197 lines (161 loc) · 4.97 KB
/
session_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package middleware
import (
"os"
"github.com/gorilla/sessions"
"github.com/labstack/echo-contrib/session"
"github.com/labstack/echo/v4"
"github.com/ockibagusp/golang-website-example/models"
"github.com/ockibagusp/golang-website-example/tests/method"
modelsTest "github.com/ockibagusp/golang-website-example/tests/models"
)
// base.html -> {{if eq ((index .session.Values "is_auth_type") | tostring) -1 }}ok{{end}}
// GetAuth: get session to authenticated
func GetAuth(c echo.Context) (session_gorilla *sessions.Session, err error) {
// Test: session_test = true
if os.Getenv("session_test") == "1" && method.SetSession == false {
session_gorilla, err = modelsTest.GetAuthSession()
} else {
if session_gorilla, err = session.Get("session", c); err != nil {
return
}
}
is_auth_type := session_gorilla.Values["is_auth_type"]
if IsAdmin(is_auth_type) || IsUser(is_auth_type) {
return session_gorilla, nil
}
if _, ok := session_gorilla.Values["username"]; !ok {
session_gorilla.Values["username"] = ""
}
if _, ok := session_gorilla.Values["is_auth_type"]; !ok {
session_gorilla.Values["is_auth_type"] = -1
}
return
}
// IsAdmin: allows access only to authenticated administrators
func IsAdmin(is_auth_type interface{}) bool {
return is_auth_type == 1
}
// IsUser: allows access only to authenticated users
func IsUser(is_auth_type interface{}) bool {
return is_auth_type == 2
}
// GetAdmin: allows access only to authenticated administrators
func GetAdmin(c echo.Context) (session_gorilla *sessions.Session, err error) {
if session_gorilla, err = session.Get("session", c); err != nil {
return
}
is_auth_type := session_gorilla.Values["is_auth_type"]
if IsAdmin(is_auth_type) {
return
}
if _, ok := session_gorilla.Values["username"]; !ok {
session_gorilla.Values["username"] = ""
}
if _, ok := session_gorilla.Values["is_auth_type"]; !ok {
session_gorilla.Values["is_auth_type"] = -1
}
return
}
// SetSession: set session from User
func SetSession(user models.User, c echo.Context) (session_gorilla *sessions.Session, err error) {
// Test: session_test = true
if os.Getenv("session_test") == "1" {
modelsTest.SetAuthSession(&user)
}
session_gorilla, err = session.Get("session", c)
if err != nil {
return
}
session_gorilla.Options = &sessions.Options{
Path: "/",
MaxAge: 86400 * 7, // 7 days expired
HttpOnly: true,
Secure: true,
}
session_gorilla.Values["username"] = user.Username
if user.IsAdmin == 1 {
session_gorilla.Values["is_auth_type"] = 1 // admin: 1
} else if user.IsAdmin == 0 {
session_gorilla.Values["is_auth_type"] = 2 // user: 2
}
session_gorilla.Save(c.Request(), c.Response())
return
}
// ClearSession: delete session from User
func ClearSession(c echo.Context) (err error) {
var session_gorilla *sessions.Session
if session_gorilla, err = session.Get("session", c); err != nil {
return
}
session_gorilla.Options = &sessions.Options{
Path: "/",
MaxAge: -1,
HttpOnly: true,
Secure: true,
}
session_gorilla.Values["username"] = ""
session_gorilla.Values["is_auth_type"] = -1
session_gorilla.Save(c.Request(), c.Response())
return
}
// RefreshSession: refresh session from User
func RefreshSession(user models.User, c echo.Context) (session_gorilla *sessions.Session, err error) {
return
}
/////
// session for flash message.
////
const session_flash = "flash"
// cookieStoreFlash: new cookie store session for flash
func cookieStoreFlash() *sessions.CookieStore {
return sessions.NewCookieStore(
[]byte("secret-session-key"),
)
}
// SetFlash: set session for flash message
func SetFlash(c echo.Context, name, value string) {
tx_session_flash := session_flash
if name == "message" {
tx_session_flash += "-message"
} else if name == "error" {
tx_session_flash += "-error"
}
session, _ := cookieStoreFlash().Get(c.Request(), tx_session_flash)
session.AddFlash(value, name)
session.Save(c.Request(), c.Response())
}
// GetFlash: get session for flash messages
func GetFlash(c echo.Context, name string) (flashes []string) {
tx_session_flash := session_flash
if name == "message" {
tx_session_flash += "-message"
} else if name == "error" {
tx_session_flash += "-error"
}
session, _ := cookieStoreFlash().Get(c.Request(), tx_session_flash)
fls := session.Flashes(name)
if len(fls) > 0 {
session.Save(c.Request(), c.Response())
for _, fl := range fls {
flashes = append(flashes, fl.(string))
}
return flashes
}
return nil
}
// SetFlashSuccess: set session for flash message: success
func SetFlashSuccess(c echo.Context, success string) {
SetFlash(c, "success", success)
}
// GetFlashSuccess: get session for flash messages: []success
func GetFlashSuccess(c echo.Context) []string {
return GetFlash(c, "success")
}
// SetFlashError: get session for flash message: error
func SetFlashError(c echo.Context, error string) {
SetFlash(c, "error", error)
}
// GetFlashError: get session for flash message: []error
func GetFlashError(c echo.Context) []string {
return GetFlash(c, "error")
}