6
6
"net/http"
7
7
"time"
8
8
9
+ "github.com/gorilla/sessions"
10
+ "github.com/labstack/echo-contrib/session"
9
11
"github.com/labstack/echo/v4"
10
12
"github.com/labstack/gommon/log"
11
13
"github.com/ngoduykhanh/wireguard-ui/model"
@@ -21,16 +23,68 @@ func LoginPage() echo.HandlerFunc {
21
23
}
22
24
}
23
25
26
+ // Login for signing in handler
27
+ func Login () echo.HandlerFunc {
28
+ return func (c echo.Context ) error {
29
+ user := new (model.User )
30
+ c .Bind (user )
31
+
32
+ dbuser , err := util .GetUser ()
33
+ if err != nil {
34
+ return c .JSON (http .StatusInternalServerError , jsonHTTPResponse {false , "Cannot query user from DB" })
35
+ }
36
+
37
+ if user .Username == dbuser .Username && user .Password == dbuser .Password {
38
+ // TODO: refresh the token
39
+ sess , _ := session .Get ("session" , c )
40
+ sess .Options = & sessions.Options {
41
+ Path : "/" ,
42
+ MaxAge : 86400 ,
43
+ HttpOnly : true ,
44
+ }
45
+
46
+ // set session_token
47
+ tokenUID := xid .New ().String ()
48
+ sess .Values ["username" ] = user .Username
49
+ sess .Values ["session_token" ] = tokenUID
50
+ sess .Save (c .Request (), c .Response ())
51
+
52
+ // set session_token in cookie
53
+ cookie := new (http.Cookie )
54
+ cookie .Name = "session_token"
55
+ cookie .Value = tokenUID
56
+ cookie .Expires = time .Now ().Add (24 * time .Hour )
57
+ c .SetCookie (cookie )
58
+
59
+ return c .JSON (http .StatusOK , jsonHTTPResponse {true , "Logged in successfully" })
60
+ }
61
+
62
+ return c .JSON (http .StatusUnauthorized , jsonHTTPResponse {false , "Invalid credentials" })
63
+ }
64
+ }
65
+
66
+ // Logout to log a user out
67
+ func Logout () echo.HandlerFunc {
68
+ return func (c echo.Context ) error {
69
+ clearSession (c )
70
+ return c .Redirect (http .StatusTemporaryRedirect , "/login" )
71
+ }
72
+ }
73
+
24
74
// WireGuardClients handler
25
75
func WireGuardClients () echo.HandlerFunc {
26
76
return func (c echo.Context ) error {
77
+ // access validation
78
+ validSession (c )
79
+
27
80
clientDataList , err := util .GetClients (true )
28
81
if err != nil {
29
82
return c .JSON (http .StatusInternalServerError , jsonHTTPResponse {false , fmt .Sprintf ("Cannot get client list: %v" , err )})
30
83
}
31
84
32
85
return c .Render (http .StatusOK , "clients.html" , map [string ]interface {}{
33
86
"baseData" : model.BaseData {Active : "" },
87
+ "username" : currentUser (c ),
34
88
"clientDataList" : clientDataList ,
35
89
})
36
90
}
@@ -39,6 +93,9 @@ func WireGuardClients() echo.HandlerFunc {
39
93
// NewClient handler
40
94
func NewClient () echo.HandlerFunc {
41
95
return func (c echo.Context ) error {
96
+ // access validation
97
+ validSession (c )
98
+
42
99
client := new (model.Client )
43
100
c .Bind (client )
44
101
@@ -93,6 +150,9 @@ func NewClient() echo.HandlerFunc {
93
150
// SetClientStatus handler to enable / disable a client
94
151
func SetClientStatus () echo.HandlerFunc {
95
152
return func (c echo.Context ) error {
153
+ // access validation
154
+ validSession (c )
155
+
96
156
data := make (map [string ]interface {})
97
157
err := json .NewDecoder (c .Request ().Body ).Decode (& data )
98
158
@@ -125,6 +185,9 @@ func SetClientStatus() echo.HandlerFunc {
125
185
// RemoveClient handler
126
186
func RemoveClient () echo.HandlerFunc {
127
187
return func (c echo.Context ) error {
188
+ // access validation
189
+ validSession (c )
190
+
128
191
client := new (model.Client )
129
192
c .Bind (client )
130
193
@@ -148,13 +211,17 @@ func RemoveClient() echo.HandlerFunc {
148
211
// WireGuardServer handler
149
212
func WireGuardServer () echo.HandlerFunc {
150
213
return func (c echo.Context ) error {
214
+ // access validation
215
+ validSession (c )
216
+
151
217
server , err := util .GetServer ()
152
218
if err != nil {
153
219
log .Error ("Cannot get server config: " , err )
154
220
}
155
221
156
222
return c .Render (http .StatusOK , "server.html" , map [string ]interface {}{
157
223
"baseData" : model.BaseData {Active : "wg-server" },
224
+ "username" : currentUser (c ),
158
225
"serverInterface" : server .Interface ,
159
226
"serverKeyPair" : server .KeyPair ,
160
227
})
@@ -164,6 +231,9 @@ func WireGuardServer() echo.HandlerFunc {
164
231
// WireGuardServerInterfaces handler
165
232
func WireGuardServerInterfaces () echo.HandlerFunc {
166
233
return func (c echo.Context ) error {
234
+ // access validation
235
+ validSession (c )
236
+
167
237
serverInterface := new (model.ServerInterface )
168
238
c .Bind (serverInterface )
169
239
@@ -192,6 +262,9 @@ func WireGuardServerInterfaces() echo.HandlerFunc {
192
262
// WireGuardServerKeyPair handler to generate private and public keys
193
263
func WireGuardServerKeyPair () echo.HandlerFunc {
194
264
return func (c echo.Context ) error {
265
+ // access validation
266
+ validSession (c )
267
+
195
268
// gen Wireguard key pair
196
269
key , err := wgtypes .GeneratePrivateKey ()
197
270
if err != nil {
@@ -221,13 +294,17 @@ func WireGuardServerKeyPair() echo.HandlerFunc {
221
294
// GlobalSettings handler
222
295
func GlobalSettings () echo.HandlerFunc {
223
296
return func (c echo.Context ) error {
297
+ // access validation
298
+ validSession (c )
299
+
224
300
globalSettings , err := util .GetGlobalSettings ()
225
301
if err != nil {
226
302
log .Error ("Cannot get global settings: " , err )
227
303
}
228
304
229
305
return c .Render (http .StatusOK , "global_settings.html" , map [string ]interface {}{
230
306
"baseData" : model.BaseData {Active : "global-settings" },
307
+ "username" : currentUser (c ),
231
308
"globalSettings" : globalSettings ,
232
309
})
233
310
}
@@ -236,6 +313,9 @@ func GlobalSettings() echo.HandlerFunc {
236
313
// GlobalSettingSubmit handler to update the global settings
237
314
func GlobalSettingSubmit () echo.HandlerFunc {
238
315
return func (c echo.Context ) error {
316
+ // access validation
317
+ validSession (c )
318
+
239
319
globalSettings := new (model.GlobalSetting )
240
320
c .Bind (globalSettings )
241
321
@@ -264,6 +344,9 @@ func GlobalSettingSubmit() echo.HandlerFunc {
264
344
// MachineIPAddresses handler to get local interface ip addresses
265
345
func MachineIPAddresses () echo.HandlerFunc {
266
346
return func (c echo.Context ) error {
347
+ // access validation
348
+ validSession (c )
349
+
267
350
// get private ip addresses
268
351
interfaceList , err := util .GetInterfaceIPs ()
269
352
if err != nil {
@@ -287,6 +370,9 @@ func MachineIPAddresses() echo.HandlerFunc {
287
370
// SuggestIPAllocation handler to get the list of ip address for client
288
371
func SuggestIPAllocation () echo.HandlerFunc {
289
372
return func (c echo.Context ) error {
373
+ // access validation
374
+ validSession (c )
375
+
290
376
server , err := util .GetServer ()
291
377
if err != nil {
292
378
log .Error ("Cannot fetch server config from database: " , err )
@@ -317,6 +403,9 @@ func SuggestIPAllocation() echo.HandlerFunc {
317
403
// ApplyServerConfig handler to write config file and restart Wireguard server
318
404
func ApplyServerConfig () echo.HandlerFunc {
319
405
return func (c echo.Context ) error {
406
+ // access validation
407
+ validSession (c )
408
+
320
409
server , err := util .GetServer ()
321
410
if err != nil {
322
411
log .Error ("Cannot get server config: " , err )
0 commit comments