forked from netlify/gotrue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
322 lines (272 loc) · 9.83 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
package models
import (
"database/sql"
"strings"
"time"
"github.com/markbates/pop"
"github.com/netlify/gotrue/storage"
"github.com/pkg/errors"
"github.com/satori/go.uuid"
"golang.org/x/crypto/bcrypt"
)
const SystemUserID = "0"
var SystemUserUUID = uuid.Nil
// User respresents a registered user with email/password authentication
type User struct {
InstanceID uuid.UUID `json:"-" db:"instance_id"`
ID uuid.UUID `json:"id" db:"id"`
Aud string `json:"aud" db:"aud"`
Role string `json:"role" db:"role"`
Email string `json:"email" db:"email"`
EncryptedPassword string `json:"-" db:"encrypted_password"`
ConfirmedAt *time.Time `json:"confirmed_at,omitempty" db:"confirmed_at"`
InvitedAt *time.Time `json:"invited_at,omitempty" db:"invited_at"`
ConfirmationToken string `json:"-" db:"confirmation_token"`
ConfirmationSentAt *time.Time `json:"confirmation_sent_at,omitempty" db:"confirmation_sent_at"`
RecoveryToken string `json:"-" db:"recovery_token"`
RecoverySentAt *time.Time `json:"recovery_sent_at,omitempty" db:"recovery_sent_at"`
EmailChangeToken string `json:"-" db:"email_change_token"`
EmailChange string `json:"new_email,omitempty" db:"email_change"`
EmailChangeSentAt *time.Time `json:"email_change_sent_at,omitempty" db:"email_change_sent_at"`
LastSignInAt *time.Time `json:"last_sign_in_at,omitempty" db:"last_sign_in_at"`
AppMetaData JSONMap `json:"app_metadata" db:"raw_app_meta_data"`
UserMetaData JSONMap `json:"user_metadata" db:"raw_user_meta_data"`
IsSuperAdmin bool `json:"-" db:"is_super_admin"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// NewUser initializes a new user from an email, password and user data.
func NewUser(instanceID uuid.UUID, email, password, aud string, userData map[string]interface{}) (*User, error) {
id, err := uuid.NewV4()
if err != nil {
return nil, errors.Wrap(err, "Error generating unique id")
}
pw, err := hashPassword(password)
if err != nil {
return nil, err
}
user := &User{
InstanceID: instanceID,
ID: id,
Aud: aud,
Email: email,
UserMetaData: userData,
EncryptedPassword: pw,
}
return user, nil
}
func NewSystemUser(instanceID uuid.UUID, aud string) *User {
return &User{
InstanceID: instanceID,
ID: SystemUserUUID,
Aud: aud,
IsSuperAdmin: true,
}
}
func (u *User) BeforeCreate(tx *pop.Connection) error {
return u.BeforeUpdate(tx)
}
func (u *User) BeforeUpdate(tx *pop.Connection) error {
if u.ID == SystemUserUUID {
return errors.New("Cannot persist system user")
}
return nil
}
func (u *User) BeforeSave(tx *pop.Connection) error {
if u.ID == SystemUserUUID {
return errors.New("Cannot persist system user")
}
if u.ConfirmedAt != nil && u.ConfirmedAt.IsZero() {
u.ConfirmedAt = nil
}
if u.InvitedAt != nil && u.InvitedAt.IsZero() {
u.InvitedAt = nil
}
if u.ConfirmationSentAt != nil && u.ConfirmationSentAt.IsZero() {
u.ConfirmationSentAt = nil
}
if u.RecoverySentAt != nil && u.RecoverySentAt.IsZero() {
u.RecoverySentAt = nil
}
if u.EmailChangeSentAt != nil && u.EmailChangeSentAt.IsZero() {
u.EmailChangeSentAt = nil
}
if u.LastSignInAt != nil && u.LastSignInAt.IsZero() {
u.LastSignInAt = nil
}
return nil
}
// IsConfirmed checks if a user has already being
// registered and confirmed.
func (u *User) IsConfirmed() bool {
return u.ConfirmedAt != nil
}
// SetRole sets the users Role to roleName
func (u *User) SetRole(tx *storage.Connection, roleName string) error {
u.Role = strings.TrimSpace(roleName)
return tx.UpdateOnly(u, "role")
}
// HasRole returns true when the users role is set to roleName
func (u *User) HasRole(roleName string) bool {
return u.Role == roleName
}
// UpdateUserMetaData sets all user data from a map of updates,
// ensuring that it doesn't override attributes that are not
// in the provided map.
func (u *User) UpdateUserMetaData(tx *storage.Connection, updates map[string]interface{}) error {
if u.UserMetaData == nil {
u.UserMetaData = updates
} else if updates != nil {
for key, value := range updates {
if value != nil {
u.UserMetaData[key] = value
} else {
delete(u.UserMetaData, key)
}
}
}
return tx.UpdateOnly(u, "raw_user_meta_data")
}
// UpdateAppMetaData updates all app data from a map of updates
func (u *User) UpdateAppMetaData(tx *storage.Connection, updates map[string]interface{}) error {
if u.AppMetaData == nil {
u.AppMetaData = updates
} else if updates != nil {
for key, value := range updates {
if value != nil {
u.AppMetaData[key] = value
} else {
delete(u.AppMetaData, key)
}
}
}
return tx.UpdateOnly(u, "raw_app_meta_data")
}
func (u *User) SetEmail(tx *storage.Connection, email string) error {
u.Email = email
return tx.UpdateOnly(u, "email")
}
// hashPassword generates a hashed password from a plaintext string
func hashPassword(password string) (string, error) {
pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(pw), nil
}
func (u *User) UpdatePassword(tx *storage.Connection, password string) error {
pw, err := hashPassword(password)
if err != nil {
return err
}
u.EncryptedPassword = pw
return tx.UpdateOnly(u, "encrypted_password")
}
// Authenticate a user from a password
func (u *User) Authenticate(password string) bool {
err := bcrypt.CompareHashAndPassword([]byte(u.EncryptedPassword), []byte(password))
return err == nil
}
// Confirm resets the confimation token and the confirm timestamp
func (u *User) Confirm(tx *storage.Connection) error {
u.ConfirmationToken = ""
now := time.Now()
u.ConfirmedAt = &now
return tx.UpdateOnly(u, "confirmation_token", "confirmed_at")
}
// ConfirmEmailChange confirm the change of email for a user
func (u *User) ConfirmEmailChange(tx *storage.Connection) error {
u.Email = u.EmailChange
u.EmailChange = ""
u.EmailChangeToken = ""
return tx.UpdateOnly(u, "email", "email_change", "email_change_token")
}
// Recover resets the recovery token
func (u *User) Recover(tx *storage.Connection) error {
u.RecoveryToken = ""
return tx.UpdateOnly(u, "recovery_token")
}
// CountOtherUsers counts how many other users exist besides the one provided
func CountOtherUsers(tx *storage.Connection, instanceID, id uuid.UUID) (int, error) {
userCount, err := tx.Q().Where("instance_id = ? and id != ?", instanceID, id).Count(&User{})
return userCount, errors.Wrap(err, "error finding registered users")
}
func findUser(tx *storage.Connection, query string, args ...interface{}) (*User, error) {
obj := &User{}
if err := tx.Q().Where(query, args...).First(obj); err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, UserNotFoundError{}
}
return nil, errors.Wrap(err, "error finding user")
}
return obj, nil
}
// FindUserByConfirmationToken finds users with the matching confirmation token.
func FindUserByConfirmationToken(tx *storage.Connection, token string) (*User, error) {
return findUser(tx, "confirmation_token = ?", token)
}
// FindUserByEmailAndAudience finds a user with the matching email and audience.
func FindUserByEmailAndAudience(tx *storage.Connection, instanceID uuid.UUID, email, aud string) (*User, error) {
return findUser(tx, "instance_id = ? and email = ? and aud = ?", instanceID, email, aud)
}
// FindUserByID finds a user matching the provided ID.
func FindUserByID(tx *storage.Connection, id uuid.UUID) (*User, error) {
return findUser(tx, "id = ?", id)
}
// FindUserByInstanceIDAndID finds a user matching the provided ID.
func FindUserByInstanceIDAndID(tx *storage.Connection, instanceID, id uuid.UUID) (*User, error) {
return findUser(tx, "instance_id = ? and id = ?", instanceID, id)
}
// FindUserByRecoveryToken finds a user with the matching recovery token.
func FindUserByRecoveryToken(tx *storage.Connection, token string) (*User, error) {
return findUser(tx, "recovery_token = ?", token)
}
// FindUserWithRefreshToken finds a user from the provided refresh token.
func FindUserWithRefreshToken(tx *storage.Connection, token string) (*User, *RefreshToken, error) {
refreshToken := &RefreshToken{}
if err := tx.Where("token = ?", token).First(refreshToken); err != nil {
if errors.Cause(err) == sql.ErrNoRows {
return nil, nil, RefreshTokenNotFoundError{}
}
return nil, nil, errors.Wrap(err, "error finding refresh token")
}
user, err := findUser(tx, "id = ?", refreshToken.UserID)
if err != nil {
return nil, nil, err
}
return user, refreshToken, nil
}
// FindUsersInAudience finds users with the matching audience.
func FindUsersInAudience(tx *storage.Connection, instanceID uuid.UUID, aud string, pageParams *Pagination, sortParams *SortParams, filter string) ([]*User, error) {
users := []*User{}
q := tx.Q().Where("instance_id = ? and aud = ?", instanceID, aud)
if filter != "" {
lf := "%" + filter + "%"
// we must specify the collation in order to get case insensitive search for the JSON column
q = q.Where("email LIKE ? OR raw_user_meta_data->>'$.full_name' COLLATE utf8mb4_unicode_ci LIKE ?", lf, lf)
}
if sortParams != nil && len(sortParams.Fields) > 0 {
for _, field := range sortParams.Fields {
q = q.Order(field.Name + " " + string(field.Dir))
}
}
var err error
if pageParams != nil {
err = q.Paginate(int(pageParams.Page), int(pageParams.PerPage)).All(&users)
pageParams.Count = uint64(q.Paginator.TotalEntriesSize)
} else {
err = q.All(&users)
}
return users, err
}
// IsDuplicatedEmail returns whether a user exists with a matching email and audience.
func IsDuplicatedEmail(tx *storage.Connection, instanceID uuid.UUID, email, aud string) (bool, error) {
_, err := FindUserByEmailAndAudience(tx, instanceID, email, aud)
if err != nil {
if IsNotFoundError(err) {
return false, nil
}
return false, err
}
return true, nil
}