-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
457 lines (364 loc) · 12 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
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
// Package user provides user data handling functionality.
package user
import (
"net/http"
"regexp"
"time"
"github.com/rs/xid"
"github.com/swithek/httpflow"
"github.com/swithek/httpflow/timeutil"
"golang.org/x/crypto/bcrypt"
"gopkg.in/guregu/null.v3/zero"
)
var (
// ErrInvalidEmail is returned when email is determined to be invalid.
ErrInvalidEmail = httpflow.NewError(nil, http.StatusBadRequest, "invalid email")
// ErrInvalidPassword is returned when password is determined to be
// invalid.
ErrInvalidPassword = httpflow.NewError(nil, http.StatusBadRequest, "invalid password")
// ErrInvalidCredentials is returned when login credentials are
// determined to be incorrect.
ErrInvalidCredentials = httpflow.NewError(nil, http.StatusUnauthorized, "incorrect credentials")
)
var (
// _emailRe defines a regexp validation instance with a preset
// allowed email format.
_emailRe = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
)
var (
// VerifLifetime is the default / recommended verification token
// lifetime value.
VerifLifetime = TokenLifetime{ //nolint:gochecknoglobals // used as a constant
Interval: time.Hour * 24 * 7,
Cooldown: time.Minute,
}
// RecovLifetime is the default / recommended recovery token lifetime
// value.
RecovLifetime = TokenLifetime{ //nolint:gochecknoglobals // used as a constant
Interval: time.Hour * 3,
Cooldown: time.Minute,
}
)
// User is an interface every user data type should implement.
type User interface {
// ApplyInput should set values from provided data structure.
// If certain input fields are empty, their destination fields
// in the underlying user's structure should not be modified.
ApplyInput(i Inputer) (Summary, error)
// ExposeCore should return user's core fields.
ExposeCore() *Core
}
// Core holds core fields needed for user data types.
type Core struct {
// ID is the primary and unique user identification key.
ID xid.ID `json:"id" db:"id"`
// CreatedAt specifies the exact time when the user was created.
CreatedAt time.Time `json:"created_at" db:"created_at"`
// UpdatedAt specifies the exact time when the user was last updated.
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
// ActivatedAt specifies the exact time when user's account
// was activated.
ActivatedAt zero.Time `json:"activated_at" db:"activated_at"`
// Email is user's active email address.
Email string `json:"email" db:"email"`
// UnverifiedEmail is a new email address yet to be verified by its
// owner. When verified this field is empty.
UnverifiedEmail zero.String `json:"unverified_email" db:"unverified_email"`
// PasswordHash is already hashed version of user's password.
PasswordHash []byte `json:"-" db:"password_hash"`
// Verification holds data needed for account activation or email
// update.
Verification Token `json:"-" db:"verification"`
// Recovery holds data needed for account recovery.
Recovery Token `json:"-" db:"recovery"`
}
// NewCore initializes all the values, user specified and default, needed for
// user's core to be usable and returns it.
func NewCore(inp Inputer) (*Core, error) {
c := &Core{
ID: xid.New(),
CreatedAt: timeutil.Now(),
}
if _, err := c.ApplyInput(inp); err != nil {
return nil, err
}
return c, nil
}
// ApplyInput applies modification to user's core fields and sets new update
// time.
func (c *Core) ApplyInput(inp Inputer) (Summary, error) {
cInp := inp.ExposeCore()
eml, err := c.SetEmail(cInp.Email)
if err != nil {
return nil, err
}
pass, err := c.UpdatePassword(cInp.OldPassword, cInp.Password)
if err != nil {
return nil, err
}
c.UpdatedAt = timeutil.Now()
return CoreSummary{
Email: eml,
Password: pass,
}, nil
}
// ExposeCore returns user's core fields.
func (c *Core) ExposeCore() *Core {
return c
}
// IsActivated checks whether user's account is activated or not.
func (c *Core) IsActivated() bool {
return !c.ActivatedAt.IsZero()
}
// SetEmail checks and updates user's email address.
// First return value determines whether the email was set or not.
func (c *Core) SetEmail(e string) (bool, error) {
if e == "" {
if c.Email == "" {
return false, ErrInvalidEmail
}
return false, nil
}
if c.Email == e {
return false, nil
}
if c.Email != "" {
return c.SetUnverifiedEmail(e)
}
if err := CheckEmail(e); err != nil {
return false, err
}
c.Email = e
return true, nil
}
// SetUnverifiedEmail checks and updates user's unverified email address.
// First return value determines whether the email was set or not.
func (c *Core) SetUnverifiedEmail(e string) (bool, error) {
if e == "" {
return false, nil
}
if c.UnverifiedEmail.String == e {
return false, nil
}
if err := CheckEmail(e); err != nil {
return false, err
}
c.UnverifiedEmail = zero.StringFrom(e)
return true, nil
}
// SetPassword checks and updates user's password.
// First return value determines whether the password was set or not.
func (c *Core) SetPassword(p string) (bool, error) {
if p == "" {
if c.PasswordHash == nil {
return false, ErrInvalidPassword
}
return false, nil
}
if err := CheckPassword(p); err != nil {
return false, err
}
h, err := bcrypt.GenerateFromPassword([]byte(p), bcrypt.DefaultCost)
if err != nil {
// unlikely to happen
return false, err
}
c.PasswordHash = h
return true, nil
}
// UpdatePassword checks and update's users password.
// NOTE: it will also check the old/current password before
// setting a new one.
// First return value determines whether the password was set or not.
func (c *Core) UpdatePassword(oldPass, newPass string) (bool, error) {
if c.PasswordHash != nil && !c.IsPasswordCorrect(oldPass) {
return false, ErrInvalidPassword
}
return c.SetPassword(newPass)
}
// IsPasswordCorrect checks whether the provided password matches the hash
// or not.
func (c *Core) IsPasswordCorrect(p string) bool {
return bcrypt.CompareHashAndPassword(c.PasswordHash, []byte(p)) == nil
}
// InitVerification initializes account / email verification and returns
// combination of token and user ID in a string format to send in verification
// emails etc.
// First parameter determines how long the verification Token should be active.
// Second parameter determines how much time has to pass until another Token
// can be generated.
func (c *Core) InitVerification(tl TokenLifetime) (string, error) {
t, err := c.Verification.gen(tl)
if err != nil {
return "", err
}
return toFullToken(t, c.ID), nil
}
// Verify checks whether the provided token is valid and activates
// the account (if it wasn't already) and/or, if unverified email address
// exists, confirms it as the main email address.
// NOTE: provided Token must in its original / raw form - not combined with
// user's ID (as InitVerification method returns).
func (c *Core) Verify(t string) error {
if err := c.Verification.Check(t); err != nil {
return err
}
// New email verification and account activation is allowed at the
// same time to allow the user to change their email when the account
// is not activated. Account will be activated even during email
// verification.
if !c.IsActivated() {
c.ActivatedAt = zero.TimeFrom(timeutil.Now())
}
if c.UnverifiedEmail.String != "" {
if c.UnverifiedEmail.String != c.Email {
c.Email = c.UnverifiedEmail.String
}
c.UnverifiedEmail = zero.StringFrom("")
}
c.Verification.Clear()
return nil
}
// CancelVerification checks whether the provided Token is valid and clears
// the active verification Token data.
// NOTE: provided Token must be in its original / raw form - not combined
// with user's ID (as InitVerification method returns).
func (c *Core) CancelVerification(t string) error {
if err := c.Verification.Check(t); err != nil {
return err
}
c.Verification.Clear()
return nil
}
// InitRecovery initializes account recovery and returns a combination of
// Token and user ID in a string format to send in recovery emails etc.
// First parameter determines how long the recovery Token should be active.
// Second parameter determines how much time has to pass until another Token
// can be generated.
func (c *Core) InitRecovery(tl TokenLifetime) (string, error) {
t, err := c.Recovery.gen(tl)
if err != nil {
return "", err
}
return toFullToken(t, c.ID), nil
}
// Recover checks whether the provided Token is valid and sets the provided
// password as the new account password.
// NOTE: provided Token must be in its original / raw form - not combined
// with user's ID (as InitRecovery method returns).
func (c *Core) Recover(t, p string) error {
if err := c.Recovery.Check(t); err != nil {
return err
}
res, err := c.SetPassword(p)
if err != nil {
return err
}
if !res {
return ErrInvalidPassword
}
c.Recovery.Clear()
return nil
}
// CancelRecovery checks whether the provided Token is valid and clears all
// active recovery Token data.
// NOTE: provided Token must in its original / raw form - not combined with
// user's ID (as InitRecovery method returns).
func (c *Core) CancelRecovery(t string) error {
if err := c.Recovery.Check(t); err != nil {
return err
}
c.Recovery.Clear()
return nil
}
// CheckEmail determines whether the provided email address is of correct
// format or not.
func CheckEmail(e string) error {
if !_emailRe.MatchString(e) {
return ErrInvalidEmail
}
return nil
}
// CheckPassword determines whether the provided password is of correct
// format or not.
func CheckPassword(p string) error {
if len(p) < 8 { // TODO add more extensive checks?
return ErrInvalidPassword
}
return nil
}
// Inputer is an interface which should be implemented by every user
// input data type.
type Inputer interface {
// ExposeCore should return user's core input fields.
ExposeCore() CoreInput
}
// CoreInput holds core fields needed for every user's ApplyInput call.
type CoreInput struct {
// Email is user's email address submitted for further processing.
Email string `json:"email"`
// Password is user's plain-text password submitted for
// further processing.
Password string `json:"password"`
// OldPassword is user's plain-text password submitted for
// confirmation before password's update.
OldPassword string `json:"old_password"`
// RememberMe specifies whether a persistent session should be
// created on registration / log in or not.
RememberMe bool `json:"remember_me"`
}
// ExposeCore returns user's core input fields.
func (c CoreInput) ExposeCore() CoreInput {
return c
}
// Summary is an interface which should be implemented by every user
// data type describing modifications during updates.
type Summary interface {
// ExposeCore should return user's core fields' modification status.
ExposeCore() CoreSummary
}
// CoreSummary holds core fields' information which determines whether they
// were modified or not.
type CoreSummary struct {
// Email specifies whether the email was modified during
// input application or not.
Email bool
// Password specifies whether the password was modified
// during input application or not.
Password bool
}
// ExposeCore returns user's core input fields' modification status.
func (c CoreSummary) ExposeCore() CoreSummary {
return c
}
// Stats is an interface which should be implemented by every user statistics
// data type.
type Stats interface {
// ExposeCore should return users' core statistics.
ExposeCore() CoreStats
}
// CoreStats holds core user statistics.
type CoreStats struct {
// Total specifies the total number of users in the data store.
Total int `json:"total" db:"total"`
}
// ExposeCore returns users' core statistics.
func (c CoreStats) ExposeCore() CoreStats {
return c
}
// CheckFilterKey determines whether the filter key is valid or not.
func CheckFilterKey(k, _ string) error {
switch k {
case "", "email":
return nil
}
return httpflow.ErrInvalidFilterKey
}
// CheckSortKey determines whether the sort key is valid or not.
func CheckSortKey(k string) error {
switch k {
case "", "created_at", "updated_at", "activated_at", "email":
return nil
}
return httpflow.ErrInvalidSortKey
}