/
resetpassword.go
102 lines (82 loc) · 2.5 KB
/
resetpassword.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
// Copyright (c) 2017-2019 The Fonero developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package commands
import (
"fmt"
"github.com/fonero-project/politeia/politeiawww/api/www/v1"
)
// ResetPasswordCmd resets the password of the specified user.
type ResetPasswordCmd struct {
Args struct {
Email string `positional-arg-name:"email"` // User email address
NewPassword string `positional-arg-name:"newpassword"` // New password
} `positional-args:"true" required:"true"`
}
// Execute executes the reset password command.
func (cmd *ResetPasswordCmd) Execute(args []string) error {
email := cmd.Args.Email
newPassword := cmd.Args.NewPassword
// Get password requirements
pr, err := client.Policy()
if err != nil {
return err
}
// Validate new password
if uint(len(newPassword)) < pr.MinPasswordLength {
return fmt.Errorf("password must be %v characters long",
pr.MinPasswordLength)
}
// The reset password command is special. It must be called twice with
// different parameters. For the 1st call, it should be called with only
// an email parameter. On success it will send an email containing a
// verification token to the email address provided. If the email server
// has been disabled, the verification token is sent back in the response
// body. The 2nd call to reset password should be called with an email,
// verification token, and new password parameters.
//
// politeiawwwcli assumes the email server is disabled.
// 1st reset password call
rp := &v1.ResetPassword{
Email: email,
NewPassword: digestSHA3(newPassword),
}
err = printJSON(rp)
if err != nil {
return err
}
rpr, err := client.ResetPassword(rp)
if err != nil {
return err
}
err = printJSON(rpr)
if err != nil {
return err
}
// 2nd reset password call
rp = &v1.ResetPassword{
Email: email,
NewPassword: digestSHA3(newPassword),
VerificationToken: rpr.VerificationToken,
}
err = printJSON(rp)
if err != nil {
return err
}
rpr, err = client.ResetPassword(rp)
if err != nil {
return err
}
return printJSON(rpr)
}
// resetPasswordHelpMsg is the output of the help command when 'resetpassword'
// is specified.
const resetPasswordHelpMsg = `resetpassword "email" "password"
Reset password for currently logged in user.
Arguments:
1. email (string, required) Email address of user
2. password (string, required) New password
Result:
{
"verificationtoken" (string) Verification token
}`