/
newuser.go
178 lines (150 loc) · 4.36 KB
/
newuser.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
package commands
import (
"encoding/hex"
"fmt"
"github.com/fonero-project/politeia/politeiawww/api/www/v1"
"github.com/fonero-project/politeia/util"
)
// NewUserCmd creates a new politeia user.
type NewUserCmd struct {
Args struct {
Email string `positional-arg-name:"email"` // Email address
Username string `positional-arg-name:"username"` // Username
Password string `positional-arg-name:"password"` // Password
} `positional-args:"true"`
Random bool `long:"random" optional:"true"` // Generate random user credentials
Paywall bool `long:"paywall" optional:"true"` // Use faucet to pay paywall (tesnet only)
Verify bool `long:"verify" optional:"true"` // Verify user email address (testnet only)
NoSave bool `long:"nosave" optional:"true"` // Don't save user identity to disk
}
// Execute executes the new user command.
func (cmd *NewUserCmd) Execute(args []string) error {
email := cmd.Args.Email
username := cmd.Args.Username
password := cmd.Args.Password
if !cmd.Random && (email == "" || username == "" || password == "") {
return fmt.Errorf("invalid credentials: you must either specify user " +
"credentials (email, username, password) or use the --random flag")
}
// Fetch CSRF tokens
_, err := client.Version()
if err != nil {
return fmt.Errorf("Version: %v", err)
}
// Fetch policy for password requirements
pr, err := client.Policy()
if err != nil {
return fmt.Errorf("Policy: %v", err)
}
// Create new user credentials if required
if cmd.Random {
b, err := util.Random(int(pr.MinPasswordLength))
if err != nil {
return err
}
email = hex.EncodeToString(b) + "@example.com"
username = hex.EncodeToString(b)
password = hex.EncodeToString(b)
}
// Validate password
if uint(len(password)) < pr.MinPasswordLength {
return fmt.Errorf("password must be %v characters long",
pr.MinPasswordLength)
}
// Create user identity and save it to disk
id, err := newIdentity()
if err != nil {
return err
}
// Setup new user request
nu := &v1.NewUser{
Email: email,
Username: username,
Password: digestSHA3(password),
PublicKey: hex.EncodeToString(id.Public.Key[:]),
}
// Print request details
err = printJSON(nu)
if err != nil {
return err
}
// Send request
nur, err := client.NewUser(nu)
if err != nil {
return fmt.Errorf("NewUser: %v", err)
}
// Save user identity to disk
if !cmd.NoSave {
err = cfg.SaveIdentity(nu.Username, id)
if err != nil {
return err
}
}
// Print response details
err = printJSON(nur)
if err != nil {
return err
}
// Verify user's email address
if cmd.Verify {
sig := id.SignMessage([]byte(nur.VerificationToken))
vnur, err := client.VerifyNewUser(&v1.VerifyNewUser{
Email: email,
VerificationToken: nur.VerificationToken,
Signature: hex.EncodeToString(sig[:]),
})
if err != nil {
return fmt.Errorf("VerifyNewUser: %v", err)
}
err = printJSON(vnur)
if err != nil {
return err
}
}
// Login to politeia
l := &v1.Login{
Email: email,
Password: digestSHA3(password),
}
lr, err := client.Login(l)
if err != nil {
return err
}
// Pays paywall fee using faucet
if cmd.Paywall {
faucet := SendFaucetTxCmd{}
faucet.Args.Address = lr.PaywallAddress
faucet.Args.Amount = lr.PaywallAmount
err = faucet.Execute(nil)
if err != nil {
return err
}
}
return nil
}
// newUserHelpMsg is the output of the help command when 'newuser' is
// specified.
const newUserHelpMsg = `newuser [flags] "email" "username" "password"
Create a new Politeia user. Users can be created by supplying all the arguments
below, or supplying the --random flag. If --random is used, Politeia will
generate a random email, username and password.
Arguments:
1. email (string, required) Email address
2. username (string, required) Username
3. password (string, required) Password
Flags:
--random (bool, optional) Generate a random email/password for the user
--paywall (bool, optional) Satisfy the paywall fee using testnet faucet
--verify (bool, optional) Verify the user's email address
--nosave (bool, optional) Do not save the user identity to disk
Request:
{
"email": (string) User email
"password": (string) Password
"publickey": (string) Active public key
"username": (string) Username
}
Response:
{
"verificationtoken": (string) Server verification token
}`