generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.go
422 lines (344 loc) · 10.4 KB
/
user.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
package oauth
import (
"context"
"errors"
"fmt"
"strings"
"time"
"github.com/litesolutions/justifay-api/model"
"github.com/litesolutions/justifay-id/log"
pass "github.com/litesolutions/justifay-id/util/password"
"github.com/mailgun/mailgun-go/v4"
"github.com/pariz/gountries"
"github.com/uptrace/bun"
)
var (
// MinPasswordLength defines minimum password length
MaxLoginLength = 50
MinLoginLength = 3
// ErrLoginTooShort ...
ErrLoginTooShort = fmt.Errorf(
"Login must be at least %d characters long",
MinLoginLength,
)
// ErrLoginTooShort ...
ErrLoginTooLong = fmt.Errorf(
"Login must be at maximum %d characters long",
MaxLoginLength,
)
// ErrLoginRequired ...
ErrLoginRequired = errors.New("Login is required")
// ErrDisplayNameRequired ...
ErrDisplayNameRequired = errors.New("Display Name is required")
// ErrUsernameRequired ...
ErrUsernameRequired = errors.New("Email is required")
// ErrUserNotFound ...
ErrUserNotFound = errors.New("User not found")
// ErrInvalidUserPassword ...
ErrInvalidUserPassword = errors.New("Invalid user password")
// ErrCannotSetEmptyUsername ...
ErrCannotSetEmptyUsername = errors.New("Cannot set empty username")
// ErrUserPasswordNotSet ...
ErrUserPasswordNotSet = errors.New("User password not set")
// ErrUsernameTaken ...
ErrUsernameTaken = errors.New("Email is not available")
// ErrEmailInvalid
ErrEmailInvalid = errors.New("Not a valid email")
// ErrEmailNotFound
ErrEmailNotFound = errors.New("We can't find an account registered with that address or username")
// ErrAccountDeletionFailed
ErrAccountDeletionFailed = errors.New("Account could not be deleted. Please reach to us now")
// ErrEmailAsLogin
ErrEmailAsLogin = errors.New("Username cannot be an email address")
// ErrCountryNotFound
ErrCountryNotFound = errors.New("Country cannot be found")
// ErrEmailNotConfirmed
ErrEmailNotConfirmed = errors.New("Please confirm your email address")
)
// UserExists returns true if user exists
func (s *Service) UserExists(username string) bool {
_, err := s.FindUserByUsername(username)
return err == nil
}
// FindUserByUsername looks up a user by username (email)
func (s *Service) FindUserByUsername(username string) (*model.User, error) {
ctx := context.Background()
// Usernames are case insensitive
user := new(model.User)
err := s.db.NewSelect().
Model(user).
Where("username = LOWER(?)", username).
Limit(1).
Scan(ctx)
if err != nil {
return nil, ErrUserNotFound
}
return user, nil
}
func (s *Service) FindUserByEmail(email string) (*model.User, error) {
ctx := context.Background()
user := new(model.User)
err := s.db.NewSelect().
Model(user).
Where("user_email = ?", email).
Limit(1).
Scan(ctx)
// Not found
if err != nil {
return nil, ErrUserNotFound
}
return user, nil
}
// SetPassword sets a user password
func (s *Service) SetPassword(user *model.User, password string) error {
return s.setPasswordCommon(s.db, user, password)
}
// SetPasswordTx sets a user password in a transaction
func (s *Service) SetPasswordTx(tx *bun.DB, user *model.User, password string) error {
return s.setPasswordCommon(tx, user, password)
}
// AuthUser authenticates user
func (s *Service) AuthUser(username, password string) (*model.User, error) {
// Fetch the user
user, err := s.FindUserByUsername(username)
if err != nil {
return nil, err
}
// Check that the password is set
if !user.Password.Valid {
return nil, ErrUserPasswordNotSet
}
// Verify the password
if pass.VerifyPassword(user.Password.String, password) != nil {
return nil, ErrInvalidUserPassword
}
return user, nil
}
// UpdateUsername ...
func (s *Service) UpdateUsername(user *model.User, username, password string) error {
return s.updateUsernameCommon(s.db, user, username, password)
}
// UpdateUsernameTx ...
func (s *Service) UpdateUsernameTx(tx *bun.DB, user *model.User, username, password string) error {
return s.updateUsernameCommon(tx, user, username, password)
}
func (s *Service) ConfirmUserEmail(email string) error {
ctx := context.Background()
user, err := s.FindUserByUsername(email)
if err != nil {
return err
}
_, err = s.db.NewUpdate().
Model(user).
Set("email_confirmed = ?", true).
WherePK().
Exec(ctx)
return err
}
// UpdateUser ...
func (s *Service) UpdateUser(user *model.User, fullName, firstName, lastName, country string, newsletter bool) error {
return s.updateUserCommon(s.db, user, fullName, firstName, lastName, country, newsletter)
}
// SetUserCountry ...
func (s *Service) SetUserCountry(user *model.User, country string) error {
return s.setUserCountryCommon(s.db, user, country)
}
// SetUserCountryTx
func (s *Service) SetUserCountryTx(tx *bun.DB, user *model.User, country string) error {
return s.setUserCountryCommon(tx, user, country)
}
// Delete user will soft delete user
func (s *Service) DeleteUser(user *model.User, password string) error {
return s.deleteUserCommon(s.db, user, password)
}
// DeleteUserTx deletes a user in a transaction
func (s *Service) DeleteUserTx(tx *bun.DB, user *model.User, password string) error {
return s.deleteUserCommon(tx, user, password)
}
func (s *Service) deleteUserCommon(db *bun.DB, user *model.User, password string) error {
ctx := context.Background()
// Check that the password is set
if !user.Password.Valid {
return ErrUserPasswordNotSet
}
// Verify the password
if pass.VerifyPassword(user.Password.String, password) != nil {
return ErrInvalidUserPassword
}
// will set deleted_at to current time using soft delete
_, err := db.NewDelete().
Model(user).
WherePK().
Exec(ctx)
if err != nil {
return ErrAccountDeletionFailed
}
// Inform user account is scheduled for deletion
mg := mailgun.NewMailgun(s.cnf.Mailgun.Domain, s.cnf.Mailgun.Key)
sender := s.cnf.Mailgun.Sender
body := ""
email := model.NewOauthEmail(
user.Username,
"Account deleted",
"account-deleted",
)
subject := email.Subject
recipient := email.Recipient
message := mg.NewMessage(sender, subject, body, recipient)
message.SetTemplate(email.Template) // set mailgun template
err = message.AddTemplateVariable("email", recipient)
if err != nil {
log.ERROR.Print(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Send the message with a 10 second timeout
_, _, err = mg.Send(ctx, message)
if err != nil {
log.ERROR.Print(err)
}
return nil
}
func (s *Service) setPasswordCommon(db *bun.DB, user *model.User, password string) error {
ctx := context.Background()
// Create a bcrypt hash
passwordHash, err := pass.HashPassword(password)
if err != nil {
return err
}
// Set the password on the user object
_, err = db.NewUpdate().
Model(user).
Set("updated_at = ?", time.Now().UTC()).
Set("last_password_change = ?", time.Now().UTC()).
Set("password = ?", string(passwordHash)).
Where("id = ?", user.IDRecord.ID).
Exec(ctx)
if err != nil {
return err
}
return nil
}
// updateUserCommon ...
func (s *Service) updateUserCommon(db *bun.DB, user *model.User, fullName, firstName, lastName, country string, newsletter bool) error {
ctx := context.Background()
update := db.NewUpdate().Model(user)
if country != "" {
// validate country code
query := gountries.New()
_, err := query.FindCountryByAlpha(strings.ToLower(country))
if err != nil {
// fallback to name
result, err := query.FindCountryByName(strings.ToLower(country))
if err != nil {
return ErrCountryNotFound
}
country = result.Codes.Alpha2
}
if country != user.Country {
update.Set("country = ?", country)
}
}
if newsletter != user.NewsletterNotification {
update.Set("newsletter_notification = ?", newsletter)
}
if fullName != user.FullName && fullName != "" {
update.Set("full_name = ?", fullName)
}
if firstName != user.FirstName && firstName != "" {
update.Set("first_name = ?", firstName)
}
if lastName != user.LastName && lastName != "" {
update.Set("last_name = ?", lastName)
}
_, err := update.Where("id = ?", user.ID).
Exec(ctx)
return err
}
// updateUserCountryCommon Update wp user country (resolve from alpha2 or alpha3 code, fallback to common name otherwise)
func (s *Service) setUserCountryCommon(db *bun.DB, user *model.User, country string) error {
ctx := context.Background()
// validate country code
query := gountries.New()
gountry, err := query.FindCountryByAlpha(strings.ToLower(country))
if err != nil {
// fallback to name
gountry, err = query.FindCountryByName(strings.ToLower(country))
if err != nil {
return ErrCountryNotFound
}
}
countryCode := gountry.Codes.Alpha2
_, err = db.NewUpdate().
Model(user).
Set("country = ?", countryCode).
Where("id = ?", user.ID).
Exec(ctx)
return err
}
// updateUsernameCommon ...
func (s *Service) updateUsernameCommon(db *bun.DB, user *model.User, username, password string) error {
ctx := context.Background()
if username == "" {
return ErrCannotSetEmptyUsername
}
// Check the email/username is available
if s.UserExists(username) {
return ErrUsernameTaken
}
// Check that the password is set
if !user.Password.Valid {
return ErrUserPasswordNotSet
}
// Verify the password
if pass.VerifyPassword(user.Password.String, password) != nil {
return ErrInvalidUserPassword
}
_, err := db.NewUpdate().
Model(user).
Set("username = ?", strings.ToLower(username)).
Set("email_confirmed = ?", false).
WherePK().
Exec(ctx)
// sends email with token for verification
email := model.NewOauthEmail(
username,
"Confirm email change",
"email-change-confirmation",
)
_, err = s.SendEmailToken(
email,
fmt.Sprintf(
"https://%s/email-confirmation",
s.cnf.Hostname,
),
)
if err != nil {
log.ERROR.Print(err)
}
// notify current email address
mg := mailgun.NewMailgun(s.cnf.Mailgun.Domain, s.cnf.Mailgun.Key)
sender := s.cnf.Mailgun.Sender
body := ""
email = model.NewOauthEmail(
user.Username,
"Email change notification",
"email-change-notification",
)
subject := email.Subject
recipient := email.Recipient
message := mg.NewMessage(sender, subject, body, recipient)
message.SetTemplate(email.Template) // set mailgun template
err = message.AddTemplateVariable("email", recipient)
if err != nil {
log.ERROR.Print(err)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Send the message with a 10 second timeout
_, _, err = mg.Send(ctx, message)
if err != nil {
log.ERROR.Print(err)
}
return nil
}