/
users.go
170 lines (135 loc) · 4.16 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
package app
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"time"
auth "github.com/Zenika/MARCEL/auth-backend/auth/middleware"
"github.com/Zenika/MARCEL/auth-backend/users"
"github.com/Zenika/MARCEL/backend/commons"
"github.com/gorilla/mux"
)
var passwordSecretKey = []byte("This is the password secret key !")
type User struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
Login string `json:"login"`
Role string `json:"role"`
CreatedAt int64 `json:"createdAt"`
LastDisconection int64 `json:"lastDisconnection"`
Password string `json:"password"`
}
func createUserHandler(w http.ResponseWriter, r *http.Request) {
if !auth.CheckPermissions(r, nil, "admin") {
commons.WriteResponse(w, http.StatusForbidden, "")
return
}
body := getUserFromRequest(w, r)
if body.Login == "" || body.DisplayName == "" || body.Password == "" {
commons.WriteResponse(w, http.StatusBadRequest, "Malformed request, missing required fields")
return
}
hash, salt := generateHash(body.Password)
user := users.New(body.DisplayName, body.Login, hash, salt)
users.SaveUsersData()
commons.WriteJsonResponse(w, user)
}
func updateUserHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userID := vars["userID"]
if !auth.CheckPermissions(r, []string{userID}, "admin") {
commons.WriteResponse(w, http.StatusForbidden, "")
return
}
body := getUserFromRequest(w, r)
savedUser := users.GetByID(userID)
if savedUser == nil || savedUser.ID != body.ID {
commons.WriteResponse(w, http.StatusNotFound, "")
return
}
if body.Password != "" && !checkHash(body.Password, savedUser.PasswordHash, savedUser.PasswordSalt) {
savedUser.LastDisconection = time.Now().Unix()
hash, salt := generateHash(body.Password)
savedUser.PasswordHash = hash
savedUser.PasswordSalt = salt
}
savedUser.DisplayName = body.DisplayName
savedUser.Login = body.Login
if auth.CheckPermissions(r, nil, "admin") {
savedUser.Role = body.Role
}
users.SaveUsersData()
commons.WriteJsonResponse(w, savedUser)
}
func getUsersHandler(w http.ResponseWriter, r *http.Request) {
result := []*User{}
for _, user := range users.GetAll() {
result = append(result, adaptUser(user))
}
commons.WriteJsonResponse(w, result)
}
func getUserHandler(w http.ResponseWriter, r *http.Request) {
if !auth.CheckPermissions(r, nil) {
commons.WriteResponse(w, http.StatusForbidden, "")
return
}
vars := mux.Vars(r)
userID := vars["userID"]
user := users.GetByID(userID)
if user == nil {
commons.WriteResponse(w, http.StatusNotFound, "User not found")
return
}
commons.WriteJsonResponse(w, adaptUser(user))
}
func deleteUserHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
userID := vars["userID"]
if !auth.CheckPermissions(r, []string{userID}, "admin") {
commons.WriteResponse(w, http.StatusForbidden, "")
return
}
ok := users.Delete(userID)
if !ok {
commons.WriteResponse(w, http.StatusNotFound, "")
return
}
users.SaveUsersData()
commons.WriteResponse(w, http.StatusNoContent, "")
}
func getUserFromRequest(w http.ResponseWriter, r *http.Request) *User {
user := &User{}
if err := json.NewDecoder(r.Body).Decode(user); err != nil {
commons.WriteResponse(w, http.StatusBadRequest, fmt.Sprintf("Error while parsing JSON (%s)", err.Error()))
return nil
}
return user
}
func generateHash(password string) (string, string) {
salt := make([]byte, 40)
rand.Read(salt)
saltString := base64.StdEncoding.EncodeToString(salt)
return hash(password, saltString), saltString
}
func checkHash(password, hashString, saltString string) bool {
return hash(password, saltString) == hashString
}
func hash(password, salt string) string {
h := hmac.New(sha256.New, passwordSecretKey)
h.Write([]byte(password + salt))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func adaptUser(user *users.User) *User {
return &User{
ID: user.ID,
DisplayName: user.DisplayName,
Login: user.Login,
CreatedAt: user.CreatedAt,
LastDisconection: user.LastDisconection,
Role: user.Role,
}
}