-
Notifications
You must be signed in to change notification settings - Fork 75
/
wwwuser.go
620 lines (525 loc) · 16.7 KB
/
wwwuser.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright (c) 2018-2020 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package legacy
import (
"encoding/json"
"errors"
"fmt"
"net/http"
www "github.com/decred/politeia/politeiawww/api/www/v1"
"github.com/decred/politeia/politeiawww/legacy/sessions"
"github.com/decred/politeia/util"
"github.com/google/uuid"
"github.com/gorilla/mux"
)
// handleSecret is a mock handler to test privileged routes.
func (p *Politeiawww) handleSecret(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleSecret")
fmt.Fprintf(w, "secret sauce")
}
// handleNewUser handles the incoming new user command. It verifies that the
// new user doesn't already exist, and then creates a new user in the db and
// generates a random code used for verification. The code is intended to be
// sent to the specified email.
func (p *Politeiawww) handleNewUser(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleNewUser")
// Get the new user command.
var u www.NewUser
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&u); err != nil {
RespondWithError(w, r, 0, "handleNewUser: unmarshal", www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
reply, err := p.processNewUser(u)
if err != nil {
RespondWithError(w, r, 0, "handleNewUser: processNewUser %v", err)
return
}
// Reply with the verification token.
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleVerifyNewUser handles the incoming new user verify command. It
// verifies that the user with the provided email has a verification token that
// matches the provided token and that the verification token has not yet
// expired.
func (p *Politeiawww) handleVerifyNewUser(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleVerifyNewUser")
// Get the new user verify command.
var vnu www.VerifyNewUser
err := util.ParseGetParams(r, &vnu)
if err != nil {
RespondWithError(w, r, 0, "handleVerifyNewUser: ParseGetParams",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
_, err = p.processVerifyNewUser(vnu)
if err != nil {
RespondWithError(w, r, 0, "handleVerifyNewUser: "+
"processVerifyNewUser %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, www.VerifyNewUserReply{})
}
// handleResendVerification sends another verification email for new user
// signup, if there is an existing verification token and it is expired.
func (p *Politeiawww) handleResendVerification(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleResendVerification")
// Get the resend verification command.
var rv www.ResendVerification
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&rv); err != nil {
RespondWithError(w, r, 0, "handleResendVerification: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
rvr, err := p.processResendVerification(&rv)
if err != nil {
var usrErr www.UserError
if errors.As(err, &usrErr) {
switch usrErr.ErrorCode {
case www.ErrorStatusUserNotFound, www.ErrorStatusEmailAlreadyVerified,
www.ErrorStatusVerificationTokenUnexpired:
// We do not return these errors because we do not want
// the caller to be able to ascertain whether an email
// address has an acount.
util.RespondWithJSON(w, http.StatusOK, &www.ResendVerificationReply{})
return
}
}
RespondWithError(w, r, 0, "handleResendVerification: "+
"processResendVerification %v", err)
return
}
// Reply with the verification token.
util.RespondWithJSON(w, http.StatusOK, *rvr)
}
// handleLogin handles the incoming login command. It verifies that the user
// exists and the accompanying password. On success a cookie is added to the
// gorilla sessions that must be returned on subsequent calls.
func (p *Politeiawww) handleLogin(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleLogin")
// Get the login command.
var l www.Login
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&l); err != nil {
RespondWithError(w, r, 0, "handleLogin: failed to decode: %v",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
reply, err := p.processLogin(l)
if err != nil {
RespondWithError(w, r, http.StatusUnauthorized,
"handleLogin: processLogin: %v", err)
return
}
// Initialize a session for the logged in user
err = p.sessions.NewSession(w, r, reply.UserID)
if err != nil {
RespondWithError(w, r, 0,
"handleLogin: initSession: %v", err)
return
}
// Set session max age
reply.SessionMaxAge = sessions.SessionMaxAge
// Reply with the user information.
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleLogout logs the user out.
func (p *Politeiawww) handleLogout(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleLogout")
_, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0, "handleLogout: getSessionUser", www.UserError{
ErrorCode: www.ErrorStatusNotLoggedIn,
})
return
}
err = p.sessions.DelSession(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleLogout: removeSession %v", err)
return
}
// Reply with the user information.
var reply www.LogoutReply
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleMe returns logged in user information.
func (p *Politeiawww) handleMe(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleMe")
user, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleMe: getSessionUser %v", err)
return
}
reply, err := p.createLoginReply(user, user.LastLoginTime)
if err != nil {
RespondWithError(w, r, 0,
"handleMe: createLoginReply %v", err)
return
}
// Set session max age
reply.SessionMaxAge = sessions.SessionMaxAge
util.RespondWithJSON(w, http.StatusOK, *reply)
}
// handleResetPassword handles the reset password command.
func (p *Politeiawww) handleResetPassword(w http.ResponseWriter, r *http.Request) {
log.Trace("handleResetPassword")
// Get the reset password command.
var rp www.ResetPassword
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&rp); err != nil {
RespondWithError(w, r, 0, "handleResetPassword: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
rpr, err := p.processResetPassword(rp)
if err != nil {
RespondWithError(w, r, 0,
"handleResetPassword: processResetPassword %v", err)
return
}
// Reply with the error code.
util.RespondWithJSON(w, http.StatusOK, rpr)
}
// handleVerifyResetPassword handles the verify reset password command.
func (p *Politeiawww) handleVerifyResetPassword(w http.ResponseWriter, r *http.Request) {
log.Trace("handleVerifyResetPassword")
var vrp www.VerifyResetPassword
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&vrp); err != nil {
RespondWithError(w, r, 0, "handleVerifyResetPassword: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
reply, err := p.processVerifyResetPassword(vrp)
if err != nil {
RespondWithError(w, r, 0,
"handleVerifyResetPassword: processVerifyResetPassword %v", err)
return
}
// Delete all existing sessions for the user. Return a 200 if
// either of these calls fail since the password was verified
// correctly.
user, err := p.db.UserGetByUsername(vrp.Username)
if err != nil {
log.Errorf("handleVerifyResetPassword: UserGetByUsername(%v): %v",
vrp.Username, err)
util.RespondWithJSON(w, http.StatusOK, reply)
return
}
err = p.db.SessionsDeleteByUserID(user.ID, []string{})
if err != nil {
log.Errorf("handleVerifyResetPassword: SessionsDeleteByUserID(%v, %v): %v",
user.ID, []string{}, err)
}
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleUserDetails handles fetching user details by user id.
func (p *Politeiawww) handleUserDetails(w http.ResponseWriter, r *http.Request) {
// Add the path param to the struct.
log.Tracef("handleUserDetails")
pathParams := mux.Vars(r)
var ud www.UserDetails
ud.UserID = pathParams["userid"]
userID, err := uuid.Parse(ud.UserID)
if err != nil {
RespondWithError(w, r, 0, "handleUserDetails: ParseUint",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
// Get session user. This is a public route so one might not exist.
user, err := p.sessions.GetSessionUser(w, r)
if err != nil && !errors.Is(err, sessions.ErrSessionNotFound) {
RespondWithError(w, r, 0,
"handleUserDetails: getSessionUser %v", err)
return
}
udr, err := p.processUserDetails(&ud,
user != nil && user.ID == userID,
user != nil && user.Admin,
)
if err != nil {
RespondWithError(w, r, 0,
"handleUserDetails: processUserDetails %v", err)
return
}
// Reply with the proposal details.
util.RespondWithJSON(w, http.StatusOK, udr)
}
// handleEditUser handles editing a user's preferences.
func (p *Politeiawww) handleEditUser(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleEditUser")
var eu www.EditUser
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&eu); err != nil {
RespondWithError(w, r, 0, "handleEditUser: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
adminUser, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0, "handleEditUser: getSessionUser %v",
err)
return
}
eur, err := p.processEditUser(&eu, adminUser)
if err != nil {
RespondWithError(w, r, 0,
"handleEditUser: processEditUser %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, eur)
}
// handleUpdateUserKey handles the incoming update user key command. It
// generates a random code used for verification. The code is intended to be
// sent to the email of the logged in user.
func (p *Politeiawww) handleUpdateUserKey(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleUpdateUserKey")
// Get the update user key command.
var u www.UpdateUserKey
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&u); err != nil {
RespondWithError(w, r, 0, "handleUpdateUserKey: unmarshal", www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
user, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleUpdateUserKey: getSessionUser %v", err)
return
}
reply, err := p.processUpdateUserKey(user, u)
if err != nil {
RespondWithError(w, r, 0, "handleUpdateUserKey: processUpdateUserKey %v", err)
return
}
// Reply with the verification token.
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleVerifyUpdateUserKey handles the incoming update user key verify
// command. It verifies that the user with the provided email has a
// verification token that matches the provided token and that the verification
// token has not yet expired.
func (p *Politeiawww) handleVerifyUpdateUserKey(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleVerifyUpdateUserKey")
// Get the new user verify command.
var vuu www.VerifyUpdateUserKey
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&vuu); err != nil {
RespondWithError(w, r, 0, "handleVerifyUpdateUserKey: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
user, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleVerifyUpdateUserKey: getSessionUser %v", err)
return
}
_, err = p.processVerifyUpdateUserKey(user, vuu)
if err != nil {
RespondWithError(w, r, 0, "handleVerifyUpdateUserKey: "+
"processVerifyUpdateUserKey %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, www.VerifyUpdateUserKeyReply{})
}
// handleChangeUsername handles the change user name command.
func (p *Politeiawww) handleChangeUsername(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleChangeUsername")
// Get the change username command.
var cu www.ChangeUsername
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&cu); err != nil {
RespondWithError(w, r, 0, "handleChangeUsername: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
user, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleChangeUsername: getSessionUser %v", err)
return
}
reply, err := p.processChangeUsername(user.Email, cu)
if err != nil {
RespondWithError(w, r, 0,
"handleChangeUsername: processChangeUsername %v", err)
return
}
// Reply with the error code.
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleChangePassword handles the change password command.
func (p *Politeiawww) handleChangePassword(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleChangePassword")
// Get the change password command.
var cp www.ChangePassword
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&cp); err != nil {
RespondWithError(w, r, 0, "handleChangePassword: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
session, err := p.sessions.GetSession(r)
if err != nil {
RespondWithError(w, r, 0,
"handleChangePassword: getSession %v", err)
return
}
user, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleChangePassword: getSessionUser %v", err)
return
}
reply, err := p.processChangePassword(user.Email, cp)
if err != nil {
RespondWithError(w, r, 0,
"handleChangePassword: processChangePassword %v", err)
return
}
// Delete all existing sessions for the user except the current.
// Return a 200 if this call fails since the password was changed
// correctly.
err = p.db.SessionsDeleteByUserID(user.ID, []string{session.ID})
if err != nil {
log.Errorf("handleChangePassword: SessionsDeleteByUserID(%v, %v): %v",
user.ID, []string{session.ID}, err)
}
// Reply with the error code.
util.RespondWithJSON(w, http.StatusOK, reply)
}
// handleUsers handles fetching a list of users.
func (p *Politeiawww) handleUsers(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleUsers")
var u www.Users
err := util.ParseGetParams(r, &u)
if err != nil {
RespondWithError(w, r, 0, "handleUsers: ParseGetParams",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
// Get session user. This is a public route so one might not exist.
user, err := p.sessions.GetSessionUser(w, r)
if err != nil && !errors.Is(err, sessions.ErrSessionNotFound) {
RespondWithError(w, r, 0,
"handleUsers: getSessionUser %v", err)
return
}
isAdmin := (user != nil && user.Admin)
ur, err := p.processUsers(&u, isAdmin)
if err != nil {
RespondWithError(w, r, 0,
"handleUsers: processUsers %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, ur)
}
// handleManageUser handles editing a user's details.
func (p *Politeiawww) handleManageUser(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleManageUser")
var mu www.ManageUser
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&mu); err != nil {
RespondWithError(w, r, 0, "handleManageUser: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
adminUser, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0, "handleManageUser: getSessionUser %v",
err)
return
}
mur, err := p.processManageUser(&mu, adminUser)
if err != nil {
RespondWithError(w, r, 0,
"handleManageUser: processManageUser %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, mur)
}
// handleSetTOTP handles the setting of TOTP Key
func (p *Politeiawww) handleSetTOTP(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleSetTOTP")
var st www.SetTOTP
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&st); err != nil {
RespondWithError(w, r, 0, "handleSetTOTP: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
u, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleSetTOTP: getSessionUser %v", err)
return
}
str, err := p.processSetTOTP(st, u)
if err != nil {
RespondWithError(w, r, 0,
"handleSetTOTP: processSetTOTP %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, str)
}
// handleVerifyTOTP handles the request to verify a set TOTP Key.
func (p *Politeiawww) handleVerifyTOTP(w http.ResponseWriter, r *http.Request) {
log.Tracef("handleVerifyTOTP")
var vt www.VerifyTOTP
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&vt); err != nil {
RespondWithError(w, r, 0, "handleVerifyTOTP: unmarshal",
www.UserError{
ErrorCode: www.ErrorStatusInvalidInput,
})
return
}
u, err := p.sessions.GetSessionUser(w, r)
if err != nil {
RespondWithError(w, r, 0,
"handleVerifyTOTP: getSessionUser %v", err)
return
}
vtr, err := p.processVerifyTOTP(vt, u)
if err != nil {
RespondWithError(w, r, 0,
"handleVerifyTOTP: processVerifyTOTP %v", err)
return
}
util.RespondWithJSON(w, http.StatusOK, vtr)
}