-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
passcode.go
349 lines (281 loc) Β· 7.46 KB
/
passcode.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package entity
import (
"bytes"
"errors"
"fmt"
"image"
"image/png"
"time"
"github.com/pquerna/otp/totp"
"github.com/photoprism/photoprism/pkg/authn"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/rnd"
"github.com/pquerna/otp"
)
// Passcode represents a two-factor authentication key.
type Passcode struct {
UID string `gorm:"type:VARBINARY(255);primary_key;" json:"UID"`
KeyType string `gorm:"size:64;default:'';primary_key;" json:"Type" yaml:"Type"`
KeyURL string `gorm:"size:2048;default:'';column:key_url;" json:"-" yaml:"-"`
key *otp.Key `gorm:"-" yaml:"-"`
RecoveryCode string `gorm:"size:255;default:'';" json:"-" yaml:"-"`
VerifiedAt *time.Time `json:"VerifiedAt" yaml:"-"`
ActivatedAt *time.Time `json:"ActivatedAt" yaml:"-"`
CreatedAt time.Time `json:"CreatedAt" yaml:"-"`
UpdatedAt time.Time `json:"UpdatedAt" yaml:"-"`
}
// TableName returns the entity table name.
func (Passcode) TableName() string {
return "passcodes"
}
// NewPasscode returns a new two-factor authentication key or nil if no valid entity UID was provided.
func NewPasscode(uid string, keyUrl, recoveryCode string) (*Passcode, error) {
// Create new authentication key.
m := &Passcode{
UID: uid,
KeyURL: keyUrl,
RecoveryCode: clean.Token(recoveryCode),
CreatedAt: TimeStamp(),
UpdatedAt: TimeStamp(),
VerifiedAt: nil,
ActivatedAt: nil,
}
// Return an error if the uid or key are invalid.
if rnd.InvalidUID(uid, 0) {
return m, errors.New("auth: invalid uid")
} else if keyUrl == "" {
return m, errors.New("auth: invalid url")
} else if err := m.SetKeyURL(keyUrl); err != nil {
return m, err
}
return m, nil
}
// FindPasscode returns the matching key or nil if it was not found.
func FindPasscode(find Passcode) *Passcode {
m := &Passcode{}
keyType := authn.Key(find.KeyType)
// Build query.
stmt := UnscopedDb()
if rnd.IsUID(find.UID, 0) {
stmt = stmt.Where("uid = ? AND key_type = ?", find.UID, keyType.String())
} else {
return nil
}
// Find matching record.
if err := stmt.First(m).Error; err != nil {
return nil
}
return m
}
// Create new entity in the database.
func (m *Passcode) Create() (err error) {
return Db().Create(m).Error
}
// Save updates the record in the database or inserts a new record if it does not exist yet.
func (m *Passcode) Save() (err error) {
return UnscopedDb().Save(m).Error
}
// Delete deletes the entity record.
func (m *Passcode) Delete() (err error) {
if m == nil {
return fmt.Errorf("entity is nil")
} else if m.UID == "" {
return fmt.Errorf("uid not set")
}
err = UnscopedDb().Delete(m).Error
return err
}
// Updates multiple properties in the database.
func (m *Passcode) Updates(values interface{}) error {
return UnscopedDb().Model(m).Updates(values).Error
}
// SetUID assigns a valid entity UID.
func (m *Passcode) SetUID(uid string) *Passcode {
if rnd.IsUID(uid, 0) {
m.UID = uid
}
return m
}
// InvalidUID checks if the entity UID is invalid.
func (m *Passcode) InvalidUID() bool {
if m == nil {
return true
}
return !rnd.IsUID(m.UID, 0)
}
// Key returns the parsed two-factor authentication key or nil if the KeyURL is invalid.
func (m *Passcode) Key() *otp.Key {
if m == nil {
return nil
} else if m.key != nil {
return m.key
}
key, err := otp.NewKeyFromURL(m.KeyURL)
if err != nil {
return nil
}
m.key = key
return m.key
}
// SetKey sets a new two-factor authentication key.
func (m *Passcode) SetKey(key *otp.Key) error {
if key == nil {
return errors.New("auth: key is nil")
}
if keyType := key.Type(); authn.KeyTOTP.NotEqual(keyType) {
return fmt.Errorf("auth: invalid key type %s", clean.Log(keyType))
} else if key.Secret() == "" {
return errors.New("auth: invalid key secret")
}
m.KeyType = key.Type()
m.KeyURL = key.URL()
m.key = key
return nil
}
// SetKeyURL sets a new two-factor authentication key based on the URL provided.
func (m *Passcode) SetKeyURL(keyUrl string) error {
key, err := otp.NewKeyFromURL(keyUrl)
if err != nil {
return fmt.Errorf("auth: %s", err)
} else if key == nil {
return errors.New("auth: failed to parse url")
}
return m.SetKey(key)
}
// Secret returns the key secret or an empty string if none is set.
func (m *Passcode) Secret() string {
if m == nil {
return ""
}
key := m.Key()
if key == nil {
return ""
}
return key.Secret()
}
// Type returns the normalized key type.
func (m *Passcode) Type() authn.KeyType {
if m == nil {
return ""
}
return authn.Key(m.KeyType)
}
// GenerateCode returns a valid passcode for testing.
func (m *Passcode) GenerateCode() (code string, err error) {
if m == nil {
return "", errors.New("passcode is nil")
}
// Get authentication key.
key := m.Key()
if key == nil {
return "", authn.ErrInvalidPasscodeKey
}
// Generate code depending on key type.
switch m.Type() {
case authn.KeyTOTP:
code, err = totp.GenerateCodeCustom(
key.Secret(),
time.Now().UTC(),
totp.ValidateOpts{
Period: uint(key.Period()),
Skew: 1,
Digits: key.Digits(),
Algorithm: key.Algorithm(),
},
)
default:
return "", authn.ErrInvalidPasscodeType
}
// Return result.
return code, err
}
// Valid checks if the passcode provided is valid.
func (m *Passcode) Valid(code string) (valid bool, recovery bool, err error) {
// Validate arguments.
if m == nil {
return false, false, errors.New("passcode is nil")
} else if code == "" {
return false, false, authn.ErrPasscodeRequired
} else if len(code) > 255 {
return false, false, authn.ErrInvalidPasscodeFormat
}
// Get authentication key.
key := m.Key()
if key == nil {
return false, false, authn.ErrInvalidPasscodeKey
}
// Check if recovery code has been used.
if m.RecoveryCode == code {
return true, true, nil
}
// Verify passcode.
switch m.Type() {
case authn.KeyTOTP:
valid, err = totp.ValidateCustom(
code,
key.Secret(),
time.Now().UTC(),
totp.ValidateOpts{
Period: uint(key.Period()),
Skew: 1,
Digits: key.Digits(),
Algorithm: key.Algorithm(),
},
)
default:
return false, false, authn.ErrInvalidPasscodeType
}
// Check if an error has been returned.
if err != nil {
return valid, false, err
}
// Set verified timestamp if nil.
if valid && m.VerifiedAt == nil {
m.VerifiedAt = TimePointer()
err = m.Updates(Map{"VerifiedAt": m.VerifiedAt})
}
// Return result.
return valid, false, err
}
// Activate activates the passcode.
func (m *Passcode) Activate() (err error) {
if m == nil {
return errors.New("passcode is nil")
}
if m.VerifiedAt == nil {
return authn.ErrPasscodeNotVerified
} else if m.ActivatedAt != nil {
return authn.ErrPasscodeAlreadyActivated
} else {
m.ActivatedAt = TimePointer()
err = m.Updates(Map{"ActivatedAt": m.ActivatedAt})
}
return err
}
// Image returns an image with a QR Code that can be used to initialize compatible authenticator apps.
func (m *Passcode) Image(size int) (image.Image, error) {
if m == nil {
return nil, errors.New("key is nil")
}
key := m.Key()
if key == nil {
return nil, authn.ErrPasscodeNotSetUp
}
return key.Image(size, size)
}
// PNG returns a PNG image buffer with a QR Code that can be used to initialize compatible authenticator apps.
func (m *Passcode) PNG(size int) *bytes.Buffer {
if m == nil {
return nil
}
img, err := m.Image(size)
if err != nil {
return nil
}
var buf bytes.Buffer
err = png.Encode(&buf, img)
if err != nil {
return nil
}
return &buf
}