-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.go
273 lines (256 loc) · 7.13 KB
/
users.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package main
import (
"fmt"
"log/slog"
"net/http"
"time"
"github.com/devilcove/timetraced/database"
"github.com/devilcove/timetraced/models"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"golang.org/x/crypto/bcrypt"
)
const SessionAge = 60 * 60 * 8 // 8 hours in seconds
func displayLogin(c *gin.Context) {
page := populatePage("")
page.NeedsLogin = true
c.HTML(http.StatusOK, "login", page)
}
func login(c *gin.Context) {
session := sessions.Default(c)
var user models.User
if err := c.Bind(&user); err != nil {
processError(c, http.StatusBadRequest, "invalid user")
slog.Error("bind err", "error", err)
return
}
slog.Debug("login by", "user", user)
if !validateUser(&user) {
session.Clear()
_ = session.Save()
processError(c, http.StatusBadRequest, "invalid user")
slog.Warn("validation error", "user", user.Username)
return
}
session.Set("loggedin", true)
session.Set("user", user.Username)
session.Set("admin", user.IsAdmin)
session.Options(sessions.Options{MaxAge: SessionAge, Secure: false, SameSite: http.SameSiteLaxMode})
_ = session.Save()
user.Password = ""
slog.Debug("login", "user", user.Username)
page := populatePage(user.Username)
page.NeedsLogin = false
projects, err := database.GetAllProjects()
if err != nil {
slog.Error(err.Error())
} else {
for _, project := range projects {
page.Projects = append(page.Projects, project.Name)
}
}
c.HTML(http.StatusOK, "content", page)
}
func validateUser(visitor *models.User) bool {
user, err := database.GetUser(visitor.Username)
if err != nil {
slog.Error("no such user", "user", visitor.Username, "error", err)
return false
}
fmt.Println(visitor.Username, user.Username)
if visitor.Username == user.Username && checkPassword(visitor, &user) {
visitor.IsAdmin = user.IsAdmin
return true
}
return false
}
func checkPassword(plain, hash *models.User) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash.Password), []byte(plain.Password))
if err != nil {
slog.Debug("bcrypt", "error", err)
}
return err == nil
}
func logout(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")
if user != nil {
if err := stopE(user.(string)); err != nil {
slog.Error("failed to stop tracking for user on logout", "error", err)
}
}
slog.Info("logout", "user", session.Get("user"))
//delete cookie
session.Clear()
_ = session.Save()
c.HTML(http.StatusOK, "login", "")
}
func register(c *gin.Context) {
page := models.GetPage()
c.HTML(http.StatusOK, "register", page)
}
func regUser(c *gin.Context) {
var user models.User
var err error
if err := c.Bind(&user); err != nil {
processError(c, http.StatusBadRequest, err.Error())
return
}
if _, err := database.GetUser(user.Username); err == nil {
processError(c, http.StatusBadRequest, "user exists")
return
}
if user.Password == "" {
processError(c, http.StatusBadRequest, "password cannot be blank")
return
}
user.Password, err = hashPassword(user.Password)
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
if err := database.SaveUser(&user); err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
slog.Info("new user added", "user", user.Username)
displayStatus(c)
}
func addUser(c *gin.Context) {
var user models.User
var err error
session := sessions.Default(c)
admin := session.Get("admin")
if !admin.(bool) {
processError(c, http.StatusUnauthorized, "only admins can create new users")
return
}
if err := c.BindJSON(&user); err != nil {
processError(c, http.StatusBadRequest, "could not decode request into json")
return
}
if _, err := database.GetUser(user.Username); err == nil {
processError(c, http.StatusBadRequest, "user exists")
return
}
if user.Username == "" || user.Password == "" {
processError(c, http.StatusBadRequest, "username or password cannot be blank")
return
}
user.Password, err = hashPassword(user.Password)
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
if err := database.SaveUser(&user); err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
slog.Info("new user added", "user", user.Username)
c.JSON(http.StatusNoContent, nil)
}
func editUser(c *gin.Context) {
var user struct {
Password string
Verify string
}
var err error
username := c.Param("name")
session := sessions.Default(c)
admin := session.Get("admin")
visitor := session.Get("user").(string)
if err := c.Bind(&user); err != nil {
processError(c, http.StatusBadRequest, "could not decode request into json")
return
}
if username != visitor && !admin.(bool) {
processError(c, http.StatusUnauthorized, "you are not authorized to edit this user")
return
}
updatedUser, err := database.GetUser(username)
if err != nil {
processError(c, http.StatusBadRequest, "user does not exist")
return
}
updatedUser.Password, err = hashPassword(user.Password)
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
if !admin.(bool) {
updatedUser.IsAdmin = false
}
updatedUser.Updated = time.Now()
if err := database.SaveUser(&updatedUser); err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
slog.Info("user updated", "user", updatedUser.Username)
displayStatus(c)
}
func deleteUser(c *gin.Context) {
session := sessions.Default(c)
admin := session.Get("admin")
user := c.Param("name")
if !admin.(bool) {
processError(c, http.StatusUnauthorized, "you are not authorized to delete this user")
return
}
if _, err := database.GetUser(user); err != nil {
processError(c, http.StatusBadRequest, "user does not exist")
return
}
if err := database.DeleteUser(user); err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
slog.Info("deleted", "user", user)
c.JSON(http.StatusNoContent, nil)
}
func getUsers(c *gin.Context) {
session := sessions.Default(c)
admin := session.Get("admin").(bool)
if !admin {
getCurrentUser(c)
return
}
users, err := database.GetAllUsers()
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
returnedUser := []models.User{}
for _, user := range users {
user.Password = ""
returnedUser = append(returnedUser, user)
}
c.HTML(http.StatusOK, "user", returnedUser)
}
func getUser(c *gin.Context) {
session := sessions.Default(c)
editUser := c.Param("name")
if !session.Get("admin").(bool) && editUser != session.Get("user").(string) {
processError(c, http.StatusBadRequest, "non-admin cannot edit other users")
return
}
user, err := database.GetUser(editUser)
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
c.HTML(http.StatusOK, "editUser", user)
}
func getCurrentUser(c *gin.Context) {
session := sessions.Default(c)
visitor := session.Get("user").(string)
user, err := database.GetUser(visitor)
if err != nil {
processError(c, http.StatusInternalServerError, err.Error())
return
}
c.HTML(http.StatusOK, "editUser", user)
}
func hashPassword(password string) (string, error) {
bytes, err := bcrypt.GenerateFromPassword([]byte(password), 4)
return string(bytes), err
}