/
user.go
129 lines (116 loc) · 2.7 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
package main
import (
"crypto/md5"
"fmt"
"golang.org/x/crypto/bcrypt"
)
type User struct {
ID string
Email string
HashedPassword string
Username string
}
const (
passwordLength = 8
hashCost = 10
userIDLength
)
func NewUser(username, email, password string) (User, error) {
user := User{
Email: email,
Username: username,
}
if username == "" {
return user, errNoUsername
}
if email == "" {
return user, errNoEmail
}
if password == "" {
return user, errNoPassword
}
if len(password) < passwordLength {
return user, errPasswordTooShort
}
// Check if the username exists
existingUser, err := globalUserStore.FindByUsername(username)
if err != nil {
return user, err
}
if existingUser != nil {
return user, errUsernameExists
}
// Check if the email exists
existingUser, err = globalUserStore.FindByEmail(email)
if err != nil {
return user, err
}
if existingUser != nil {
return user, errEmailExists
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), hashCost)
user.HashedPassword = string(hashedPassword)
user.ID = GenerateID("usr", userIDLength)
return user, err
}
func FindUser(username, password string) (*User, error) {
out := &User{
Username: username,
}
existingUser, err := globalUserStore.FindByUsername(username)
if err != nil {
return out, err
}
if existingUser == nil {
return out, errCredentialsIncorrect
}
if bcrypt.CompareHashAndPassword(
[]byte(existingUser.HashedPassword),
[]byte(password),
) != nil {
return out, errCredentialsIncorrect
}
return existingUser, nil
}
func UpdateUser(user *User, email, currentPassword, newPassword string) (User, error) {
out := *user
out.Email = email
// Check if the email exists
existingUser, err := globalUserStore.FindByEmail(email)
if err != nil {
return out, err
}
if existingUser != nil && existingUser.ID != user.ID {
return out, errEmailExists
}
// At this point, we can update the email address
user.Email = email
// No current password? Don't try update the password.
if currentPassword == "" {
return out, nil
}
if bcrypt.CompareHashAndPassword(
[]byte(user.HashedPassword),
[]byte(currentPassword),
) != nil {
return out, errPasswordIncorrect
}
if newPassword == "" {
return out, errNoPassword
}
if len(newPassword) < passwordLength {
return out, errPasswordTooShort
}
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), hashCost)
user.HashedPassword = string(hashedPassword)
return out, err
}
func (user *User) AvatarURL() string {
return fmt.Sprintf(
"//www.gravatar.com/avatar/%x",
md5.Sum([]byte(user.Email)),
)
}
func (user *User) ImagesRoute() string {
return "/user/" + user.ID
}