-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathrecord_model_auth.go
More file actions
85 lines (68 loc) · 2.89 KB
/
record_model_auth.go
File metadata and controls
85 lines (68 loc) · 2.89 KB
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
package core
import "github.com/pocketbase/pocketbase/tools/security"
// Email returns the "email" record field value (usually available with Auth collections).
func (m *Record) Email() string {
return m.GetString(FieldNameEmail)
}
// SetEmail sets the "email" record field value (usually available with Auth collections).
func (m *Record) SetEmail(email string) {
m.Set(FieldNameEmail, email)
}
// Verified returns the "emailVisibility" record field value (usually available with Auth collections).
func (m *Record) EmailVisibility() bool {
return m.GetBool(FieldNameEmailVisibility)
}
// SetEmailVisibility sets the "emailVisibility" record field value (usually available with Auth collections).
func (m *Record) SetEmailVisibility(visible bool) {
m.Set(FieldNameEmailVisibility, visible)
}
// Verified returns the "verified" record field value (usually available with Auth collections).
func (m *Record) Verified() bool {
return m.GetBool(FieldNameVerified)
}
// SetVerified sets the "verified" record field value (usually available with Auth collections).
func (m *Record) SetVerified(verified bool) {
m.Set(FieldNameVerified, verified)
}
// TokenKey returns the "tokenKey" record field value (usually available with Auth collections).
func (m *Record) TokenKey() string {
return m.GetString(FieldNameTokenKey)
}
// SetTokenKey sets the "tokenKey" record field value (usually available with Auth collections).
func (m *Record) SetTokenKey(key string) {
m.Set(FieldNameTokenKey, key)
}
// RefreshTokenKey generates and sets a new random auth record "tokenKey".
func (m *Record) RefreshTokenKey() {
m.Set(FieldNameTokenKey+autogenerateModifier, "")
}
// SetPassword sets the "password" record field value (usually available with Auth collections).
func (m *Record) SetPassword(password string) {
// note: the tokenKey will be auto changed if necessary before db write
m.Set(FieldNamePassword, password)
}
// SetRandomPassword sets the "password" auth record field to a random autogenerated value.
//
// The autogenerated password is ~30 characters and it is set directly as hash,
// aka. the field plain password value validators (length, pattern, etc.) are ignored
// (this is usually used as part of the auto created OTP or OAuth2 user flows).
func (m *Record) SetRandomPassword() string {
pass := security.RandomString(30)
m.Set(FieldNamePassword, pass)
m.RefreshTokenKey() // manually refresh the token key because the plain password is resetted
// unset the plain value to skip the field validators
if raw, ok := m.GetRaw(FieldNamePassword).(*PasswordFieldValue); ok {
raw.Plain = ""
}
return pass
}
// ValidatePassword validates a plain password against the "password" record field.
//
// Returns false if the password is incorrect.
func (m *Record) ValidatePassword(password string) bool {
pv, ok := m.GetRaw(FieldNamePassword).(*PasswordFieldValue)
if !ok {
return false
}
return pv.Validate(password)
}