-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathfield_password.go
More file actions
325 lines (269 loc) · 8.85 KB
/
field_password.go
File metadata and controls
325 lines (269 loc) · 8.85 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
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
package core
import (
"context"
"database/sql/driver"
"fmt"
"regexp"
"strings"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/core/validators"
"github.com/spf13/cast"
"golang.org/x/crypto/bcrypt"
)
func init() {
Fields[FieldTypePassword] = func() Field {
return &PasswordField{}
}
}
const FieldTypePassword = "password"
var (
_ Field = (*PasswordField)(nil)
_ GetterFinder = (*PasswordField)(nil)
_ SetterFinder = (*PasswordField)(nil)
_ DriverValuer = (*PasswordField)(nil)
_ RecordInterceptor = (*PasswordField)(nil)
)
// PasswordField defines "password" type field for storing bcrypt hashed strings
// (usually used only internally for the "password" auth collection system field).
//
// If you want to set a direct bcrypt hash as record field value you can use the SetRaw method, for example:
//
// // generates a bcrypt hash of "123456" and set it as field value
// // (record.GetString("password") returns the plain password until persisted, otherwise empty string)
// record.Set("password", "123456")
//
// // set directly a bcrypt hash of "123456" as field value
// // (record.GetString("password") returns empty string)
// record.SetRaw("password", "$2a$10$.5Elh8fgxypNUWhpUUr/xOa2sZm0VIaE0qWuGGl9otUfobb46T1Pq")
//
// The following additional getter keys are available:
//
// - "fieldName:hash" - returns the bcrypt hash string of the record field value (if any). For example:
// record.GetString("password:hash")
type PasswordField struct {
// Name (required) is the unique name of the field.
Name string `form:"name" json:"name"`
// Id is the unique stable field identifier.
//
// It is automatically generated from the name when adding to a collection FieldsList.
Id string `form:"id" json:"id"`
// System prevents the renaming and removal of the field.
System bool `form:"system" json:"system"`
// Hidden hides the field from the API response.
Hidden bool `form:"hidden" json:"hidden"`
// ---
// @todo remove
//
// Presentable hints the Dashboard UI to use the underlying
// field record value in the relation preview label.
Presentable bool `form:"presentable" json:"presentable"`
// Help is an extra text explaining what the field is about.
// It is usually shown in Dashboard UI under the field input.
Help string `form:"help" json:"help"`
// Pattern specifies an optional regex pattern to match against the field value.
//
// Leave it empty to skip the pattern check.
Pattern string `form:"pattern" json:"pattern"`
// Min specifies an optional required field string length.
Min int `form:"min" json:"min"`
// Max specifies an optional required field string length.
//
// If zero, fallback to max 71 bytes.
Max int `form:"max" json:"max"`
// Cost specifies the cost/weight/iteration/etc. bcrypt factor.
//
// If zero, fallback to [bcrypt.DefaultCost].
//
// If explicitly set, must be between [bcrypt.MinCost] and [bcrypt.MaxCost].
Cost int `form:"cost" json:"cost"`
// Required will require the field value to be non-empty string.
Required bool `form:"required" json:"required"`
}
// Type implements [Field.Type] interface method.
func (f *PasswordField) Type() string {
return FieldTypePassword
}
// GetId implements [Field.GetId] interface method.
func (f *PasswordField) GetId() string {
return f.Id
}
// SetId implements [Field.SetId] interface method.
func (f *PasswordField) SetId(id string) {
f.Id = id
}
// GetName implements [Field.GetName] interface method.
func (f *PasswordField) GetName() string {
return f.Name
}
// SetName implements [Field.SetName] interface method.
func (f *PasswordField) SetName(name string) {
f.Name = name
}
// GetSystem implements [Field.GetSystem] interface method.
func (f *PasswordField) GetSystem() bool {
return f.System
}
// SetSystem implements [Field.SetSystem] interface method.
func (f *PasswordField) SetSystem(system bool) {
f.System = system
}
// GetHidden implements [Field.GetHidden] interface method.
func (f *PasswordField) GetHidden() bool {
return f.Hidden
}
// SetHidden implements [Field.SetHidden] interface method.
func (f *PasswordField) SetHidden(hidden bool) {
f.Hidden = hidden
}
// ColumnType implements [Field.ColumnType] interface method.
func (f *PasswordField) ColumnType(app App) string {
return "TEXT DEFAULT '' NOT NULL"
}
// DriverValue implements the [DriverValuer] interface.
func (f *PasswordField) DriverValue(record *Record) (driver.Value, error) {
fp := f.getPasswordValue(record)
return fp.Hash, fp.LastError
}
// PrepareValue implements [Field.PrepareValue] interface method.
func (f *PasswordField) PrepareValue(record *Record, raw any) (any, error) {
return &PasswordFieldValue{
Hash: cast.ToString(raw),
}, nil
}
// ValidateValue implements [Field.ValidateValue] interface method.
func (f *PasswordField) ValidateValue(ctx context.Context, app App, record *Record) error {
fp, ok := record.GetRaw(f.Name).(*PasswordFieldValue)
if !ok {
return validators.ErrUnsupportedValueType
}
if fp.LastError != nil {
return fp.LastError
}
if f.Required {
if err := validation.Required.Validate(fp.Hash); err != nil {
return err
}
}
if fp.Plain == "" {
return nil // nothing to check
}
// note: casted to []rune to count multi-byte chars as one for the
// sake of more intuitive UX and clearer user error messages
//
// note2: technically multi-byte strings could produce bigger length than the bcrypt limit
// but it should be fine as it will be just truncated (even if it cuts a byte sequence in the middle)
length := len([]rune(fp.Plain))
if length < f.Min {
return validation.NewError("validation_min_text_constraint", fmt.Sprintf("Must be at least %d character(s)", f.Min))
}
maxLength := f.Max
if maxLength <= 0 {
maxLength = 71
}
if length > maxLength {
return validation.NewError("validation_max_text_constraint", fmt.Sprintf("Must be less than %d character(s)", maxLength))
}
if f.Pattern != "" {
match, _ := regexp.MatchString(f.Pattern, fp.Plain)
if !match {
return validation.NewError("validation_invalid_format", "Invalid value format")
}
}
return nil
}
// ValidateSettings implements [Field.ValidateSettings] interface method.
func (f *PasswordField) ValidateSettings(ctx context.Context, app App, collection *Collection) error {
return validation.ValidateStruct(f,
validation.Field(&f.Id, validation.By(DefaultFieldIdValidationRule)),
validation.Field(&f.Name, validation.By(DefaultFieldNameValidationRule)),
validation.Field(&f.Help, validation.By(DefaultFieldHelpValidationRule)),
validation.Field(&f.Min, validation.Min(1), validation.Max(71)),
validation.Field(&f.Max, validation.Min(f.Min), validation.Max(71)),
validation.Field(&f.Cost, validation.Min(bcrypt.MinCost), validation.Max(bcrypt.MaxCost)),
validation.Field(&f.Pattern, validation.By(validators.IsRegex)),
)
}
func (f *PasswordField) getPasswordValue(record *Record) *PasswordFieldValue {
raw := record.GetRaw(f.Name)
switch v := raw.(type) {
case *PasswordFieldValue:
return v
case string:
// we assume that any raw string starting with $2 is bcrypt hash
if strings.HasPrefix(v, "$2") {
return &PasswordFieldValue{Hash: v}
}
}
return &PasswordFieldValue{}
}
// Intercept implements the [RecordInterceptor] interface.
func (f *PasswordField) Intercept(
ctx context.Context,
app App,
record *Record,
actionName string,
actionFunc func() error,
) error {
switch actionName {
case InterceptorActionAfterCreate, InterceptorActionAfterUpdate:
// unset the plain field value after successful create/update
fp := f.getPasswordValue(record)
fp.Plain = ""
}
return actionFunc()
}
// FindGetter implements the [GetterFinder] interface.
func (f *PasswordField) FindGetter(key string) GetterFunc {
switch key {
case f.Name:
return func(record *Record) any {
return f.getPasswordValue(record).Plain
}
case f.Name + ":hash":
return func(record *Record) any {
return f.getPasswordValue(record).Hash
}
default:
return nil
}
}
// FindSetter implements the [SetterFinder] interface.
func (f *PasswordField) FindSetter(key string) SetterFunc {
switch key {
case f.Name:
return f.setValue
default:
return nil
}
}
func (f *PasswordField) setValue(record *Record, raw any) {
fv := &PasswordFieldValue{
Plain: cast.ToString(raw),
}
// hash the password
if fv.Plain != "" {
cost := f.Cost
if cost <= 0 {
cost = bcrypt.DefaultCost
}
hash, err := bcrypt.GenerateFromPassword([]byte(fv.Plain), cost)
if err != nil {
fv.LastError = err
}
fv.Hash = string(hash)
}
record.SetRaw(f.Name, fv)
}
// -------------------------------------------------------------------
type PasswordFieldValue struct {
LastError error
Hash string
Plain string
}
func (pv PasswordFieldValue) Validate(pass string) bool {
if pv.Hash == "" || pv.LastError != nil {
return false
}
err := bcrypt.CompareHashAndPassword([]byte(pv.Hash), []byte(pass))
return err == nil
}