-
Notifications
You must be signed in to change notification settings - Fork 13
/
register.go
81 lines (76 loc) · 2.8 KB
/
register.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
package registration
import (
"context"
"errors"
"strings"
"github.com/domino14/liwords/pkg/auth"
"github.com/domino14/liwords/pkg/config"
"github.com/domino14/liwords/pkg/entity"
"github.com/domino14/liwords/pkg/user"
"github.com/domino14/liwords/pkg/utilities"
"github.com/lib/pq"
)
// RegisterUser registers a user.
func RegisterUser(ctx context.Context, username string, password string, email string,
firstName string, lastName string, birthDate string, countryCode string,
userStore user.Store, bot bool, argonConfig config.ArgonConfig) error {
// username = strings.Rep
if len(username) < 3 || len(username) > 20 {
return errors.New("username must be between 3 and 20 letters in length")
}
if strings.IndexFunc(username, func(c rune) bool {
return !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || c == '.' || c == '_')
}) != -1 {
return errors.New("username can only contain letters, digits, period, hyphen or underscore")
}
// Should we have other unacceptable usernames?
if strings.EqualFold(username, "anonymous") ||
strings.EqualFold(username, utilities.CensoredUsername) ||
strings.EqualFold(username, utilities.AnotherCensoredUsername) ||
strings.EqualFold(username, utilities.YetAnotherCensoredUsername) {
return errors.New("username is not acceptable")
}
if strings.HasPrefix(username, "-") || strings.HasPrefix(username, ".") || strings.HasPrefix(username, "_") {
return errors.New("username must start with a number or a letter")
}
if strings.HasSuffix(username, "-") || strings.HasSuffix(username, ".") || strings.HasSuffix(username, "_") {
return errors.New("username must end with a number or a letter")
}
if len(password) < 8 {
return errors.New("your new password is too short, use 8 or more characters")
}
if len(email) < 3 {
return errors.New("please use a valid email address")
}
email = strings.TrimSpace(email)
config := auth.NewPasswordConfig(argonConfig.Time, argonConfig.Memory, argonConfig.Threads, argonConfig.Keylen)
hashPass, err := auth.GeneratePassword(config, password)
if err != nil {
return err
}
err = userStore.New(ctx, &entity.User{
Username: username,
Password: hashPass,
Email: email,
Profile: &entity.Profile{
FirstName: firstName,
LastName: lastName,
BirthDate: birthDate,
CountryCode: countryCode,
},
IsBot: bot,
})
if err != nil {
if err, ok := err.(*pq.Error); ok {
// https://www.postgresql.org/docs/current/errcodes-appendix.html
if err.Code == "23505" {
if err.Constraint == "username_idx" {
return errors.New("That username has already been signed up, please log in")
} else if err.Constraint == "email_idx" {
return errors.New("That email address has already been signed up, please log in with your existing username")
}
}
}
}
return err
}