Repository navigation
Expand file tree
/
Copy pathusers.go
More file actions
177 lines (155 loc) · 4.67 KB
/
Copy pathusers.go
File metadata and controls
177 lines (155 loc) · 4.67 KB
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
package api
import (
"encoding/json"
"net/http"
"regexp"
"strings"
"github.com/go-chi/chi/v5"
"github.com/kernelcode0/aptify/internal/storage"
"golang.org/x/crypto/bcrypt"
)
var usernameRe = regexp.MustCompile(`^[A-Za-z0-9_.-]{3,32}$`)
type createUserRequest struct {
Username string `json:"username"`
Password string `json:"password"`
Role string `json:"role"`
}
type updateUserRequest struct {
Role *string `json:"role"`
Password *string `json:"password"`
}
func validRole(role string) bool {
return role == "admin" || role == "member" || role == "viewer"
}
func (h *Handler) listUsers(w http.ResponseWriter, r *http.Request) {
users, err := h.db.ListUsers()
if err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
if users == nil {
users = []storage.User{}
}
jsonOK(w, users, http.StatusOK)
}
func (h *Handler) createUser(w http.ResponseWriter, r *http.Request) {
var req createUserRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, "invalid JSON", http.StatusBadRequest)
return
}
req.Username = strings.TrimSpace(req.Username)
if !usernameRe.MatchString(req.Username) {
jsonError(w, "username must be 3-32 characters and contain only letters, numbers, dots, hyphens, or underscores", http.StatusBadRequest)
return
}
if len(req.Password) < 8 {
jsonError(w, "password must be at least 8 characters", http.StatusBadRequest)
return
}
if !validRole(req.Role) {
jsonError(w, "invalid role", http.StatusBadRequest)
return
}
existing, err := h.db.GetUserByUsername(req.Username)
if err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
if existing != nil {
jsonError(w, "username already exists", http.StatusConflict)
return
}
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
if err != nil {
jsonError(w, "password error", http.StatusInternalServerError)
return
}
user, err := h.db.CreateUser(req.Username, string(hash), req.Role)
if err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
detail, _ := json.Marshal(map[string]string{"role": req.Role})
h.audit(currentUser(r), "create_user", "user:"+user.Username, string(detail))
jsonOK(w, user, http.StatusCreated)
}
func (h *Handler) updateUser(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
user, err := h.db.GetUserByID(id)
if err != nil || user == nil {
jsonError(w, "user not found", http.StatusNotFound)
return
}
var req updateUserRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
jsonError(w, "invalid JSON", http.StatusBadRequest)
return
}
current := currentUser(r)
if req.Role != nil {
role := strings.TrimSpace(*req.Role)
if !validRole(role) {
jsonError(w, "invalid role", http.StatusBadRequest)
return
}
if current != nil && current.ID == id {
jsonError(w, "cannot change your own role", http.StatusBadRequest)
return
}
if err := h.db.UpdateUserRole(id, role); err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
user.Role = role
}
if req.Password != nil {
if len(*req.Password) < 8 {
jsonError(w, "password must be at least 8 characters", http.StatusBadRequest)
return
}
hash, err := bcrypt.GenerateFromPassword([]byte(*req.Password), bcrypt.DefaultCost)
if err != nil {
jsonError(w, "password error", http.StatusInternalServerError)
return
}
if err := h.db.UpdateUserPassword(id, string(hash)); err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
}
jsonOK(w, user, http.StatusOK)
}
func (h *Handler) deleteUser(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
current := currentUser(r)
if current != nil && current.ID == id {
jsonError(w, "cannot delete yourself", http.StatusBadRequest)
return
}
user, err := h.db.GetUserByID(id)
if err != nil || user == nil {
jsonError(w, "user not found", http.StatusNotFound)
return
}
if err := h.db.DeleteUser(id); err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
h.audit(current, "delete_user", "user:"+user.Username, "")
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) resetUser2FA(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
targetUser, err := h.db.GetUserByID(id)
if err != nil || targetUser == nil {
jsonError(w, "user not found", http.StatusNotFound)
return
}
if err := h.db.ResetUser2FA(targetUser.ID); err != nil {
jsonError(w, "db error", http.StatusInternalServerError)
return
}
h.audit(currentUser(r), "reset_user_2fa", "user:"+targetUser.Username, "")
jsonOK(w, map[string]string{"status": "ok"}, http.StatusOK)
}