-
Notifications
You must be signed in to change notification settings - Fork 40
/
storer.go
455 lines (355 loc) · 11.9 KB
/
storer.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package main
import (
"context"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/pkg/errors"
"github.com/volatiletech/authboss/v3"
aboauth "github.com/volatiletech/authboss/v3/oauth2"
"github.com/volatiletech/authboss/v3/otp/twofactor/sms2fa"
"github.com/volatiletech/authboss/v3/otp/twofactor/totp2fa"
)
// User struct for authboss
type User struct {
ID int
// Non-authboss related field
Name string
// Auth
Email string
Password string
// Confirm
ConfirmSelector string
ConfirmVerifier string
Confirmed bool
// Lock
AttemptCount int
LastAttempt time.Time
Locked time.Time
// Recover
RecoverSelector string
RecoverVerifier string
RecoverTokenExpiry time.Time
// OAuth2
OAuth2UID string
OAuth2Provider string
OAuth2AccessToken string
OAuth2RefreshToken string
OAuth2Expiry time.Time
// 2fa
TOTPSecretKey string
SMSPhoneNumber string
SMSSeedPhoneNumber string
RecoveryCodes string
// Remember is in another table
}
// This pattern is useful in real code to ensure that
// we've got the right interfaces implemented.
var (
assertUser = &User{}
assertStorer = &MemStorer{}
_ authboss.User = assertUser
_ authboss.AuthableUser = assertUser
_ authboss.ConfirmableUser = assertUser
_ authboss.LockableUser = assertUser
_ authboss.RecoverableUser = assertUser
_ authboss.ArbitraryUser = assertUser
_ totp2fa.User = assertUser
_ sms2fa.User = assertUser
_ authboss.CreatingServerStorer = assertStorer
_ authboss.ConfirmingServerStorer = assertStorer
_ authboss.RecoveringServerStorer = assertStorer
_ authboss.RememberingServerStorer = assertStorer
)
// PutPID into user
func (u *User) PutPID(pid string) { u.Email = pid }
// PutPassword into user
func (u *User) PutPassword(password string) { u.Password = password }
// PutEmail into user
func (u *User) PutEmail(email string) { u.Email = email }
// PutConfirmed into user
func (u *User) PutConfirmed(confirmed bool) { u.Confirmed = confirmed }
// PutConfirmSelector into user
func (u *User) PutConfirmSelector(confirmSelector string) { u.ConfirmSelector = confirmSelector }
// PutConfirmVerifier into user
func (u *User) PutConfirmVerifier(confirmVerifier string) { u.ConfirmVerifier = confirmVerifier }
// PutLocked into user
func (u *User) PutLocked(locked time.Time) { u.Locked = locked }
// PutAttemptCount into user
func (u *User) PutAttemptCount(attempts int) { u.AttemptCount = attempts }
// PutLastAttempt into user
func (u *User) PutLastAttempt(last time.Time) { u.LastAttempt = last }
// PutRecoverSelector into user
func (u *User) PutRecoverSelector(token string) { u.RecoverSelector = token }
// PutRecoverVerifier into user
func (u *User) PutRecoverVerifier(token string) { u.RecoverVerifier = token }
// PutRecoverExpiry into user
func (u *User) PutRecoverExpiry(expiry time.Time) { u.RecoverTokenExpiry = expiry }
// PutTOTPSecretKey into user
func (u *User) PutTOTPSecretKey(key string) { u.TOTPSecretKey = key }
// PutSMSPhoneNumber into user
func (u *User) PutSMSPhoneNumber(key string) { u.SMSPhoneNumber = key }
// PutRecoveryCodes into user
func (u *User) PutRecoveryCodes(key string) { u.RecoveryCodes = key }
// PutOAuth2UID into user
func (u *User) PutOAuth2UID(uid string) { u.OAuth2UID = uid }
// PutOAuth2Provider into user
func (u *User) PutOAuth2Provider(provider string) { u.OAuth2Provider = provider }
// PutOAuth2AccessToken into user
func (u *User) PutOAuth2AccessToken(token string) { u.OAuth2AccessToken = token }
// PutOAuth2RefreshToken into user
func (u *User) PutOAuth2RefreshToken(refreshToken string) { u.OAuth2RefreshToken = refreshToken }
// PutOAuth2Expiry into user
func (u *User) PutOAuth2Expiry(expiry time.Time) { u.OAuth2Expiry = expiry }
// PutArbitrary into user
func (u *User) PutArbitrary(values map[string]string) {
if n, ok := values["name"]; ok {
u.Name = n
}
}
// GetPID from user
func (u User) GetPID() string { return u.Email }
// GetPassword from user
func (u User) GetPassword() string { return u.Password }
// GetEmail from user
func (u User) GetEmail() string { return u.Email }
// GetConfirmed from user
func (u User) GetConfirmed() bool { return u.Confirmed }
// GetConfirmSelector from user
func (u User) GetConfirmSelector() string { return u.ConfirmSelector }
// GetConfirmVerifier from user
func (u User) GetConfirmVerifier() string { return u.ConfirmVerifier }
// GetLocked from user
func (u User) GetLocked() time.Time { return u.Locked }
// GetAttemptCount from user
func (u User) GetAttemptCount() int { return u.AttemptCount }
// GetLastAttempt from user
func (u User) GetLastAttempt() time.Time { return u.LastAttempt }
// GetRecoverSelector from user
func (u User) GetRecoverSelector() string { return u.RecoverSelector }
// GetRecoverVerifier from user
func (u User) GetRecoverVerifier() string { return u.RecoverVerifier }
// GetRecoverExpiry from user
func (u User) GetRecoverExpiry() time.Time { return u.RecoverTokenExpiry }
// GetTOTPSecretKey from user
func (u User) GetTOTPSecretKey() string { return u.TOTPSecretKey }
// GetSMSPhoneNumber from user
func (u User) GetSMSPhoneNumber() string { return u.SMSPhoneNumber }
// GetSMSPhoneNumberSeed from user
func (u User) GetSMSPhoneNumberSeed() string { return u.SMSSeedPhoneNumber }
// GetRecoveryCodes from user
func (u User) GetRecoveryCodes() string { return u.RecoveryCodes }
// IsOAuth2User returns true if the user was created with oauth2
func (u User) IsOAuth2User() bool { return len(u.OAuth2UID) != 0 }
// GetOAuth2UID from user
func (u User) GetOAuth2UID() (uid string) { return u.OAuth2UID }
// GetOAuth2Provider from user
func (u User) GetOAuth2Provider() (provider string) { return u.OAuth2Provider }
// GetOAuth2AccessToken from user
func (u User) GetOAuth2AccessToken() (token string) { return u.OAuth2AccessToken }
// GetOAuth2RefreshToken from user
func (u User) GetOAuth2RefreshToken() (refreshToken string) { return u.OAuth2RefreshToken }
// GetOAuth2Expiry from user
func (u User) GetOAuth2Expiry() (expiry time.Time) { return u.OAuth2Expiry }
// GetArbitrary from user
func (u User) GetArbitrary() map[string]string {
return map[string]string{
"name": u.Name,
}
}
// MemStorer stores users in memory
type MemStorer struct {
Users map[string]User
Tokens map[string][]string
}
// NewMemStorer constructor
func NewMemStorer() *MemStorer {
return &MemStorer{
Users: map[string]User{
"[email protected]": {
ID: 1,
Name: "Rick",
Password: "$2a$10$XtW/BrS5HeYIuOCXYe8DFuInetDMdaarMUJEOg/VA/JAIDgw3l4aG", // pass = 1234
Email: "[email protected]",
Confirmed: true,
SMSSeedPhoneNumber: "(777)-123-4567",
},
},
Tokens: make(map[string][]string),
}
}
// Save the user
func (m MemStorer) Save(_ context.Context, user authboss.User) error {
u := user.(*User)
m.Users[u.Email] = *u
debugln("Saved user:", u.Name)
return nil
}
// Load the user
func (m MemStorer) Load(_ context.Context, key string) (user authboss.User, err error) {
// Check to see if our key is actually an oauth2 pid
provider, uid, err := authboss.ParseOAuth2PID(key)
if err == nil {
for _, u := range m.Users {
if u.OAuth2Provider == provider && u.OAuth2UID == uid {
debugln("Loaded OAuth2 user:", u.Email)
return &u, nil
}
}
return nil, authboss.ErrUserNotFound
}
u, ok := m.Users[key]
if !ok {
return nil, authboss.ErrUserNotFound
}
debugln("Loaded user:", u.Name)
return &u, nil
}
// New user creation
func (m MemStorer) New(_ context.Context) authboss.User {
return &User{}
}
// Create the user
func (m MemStorer) Create(_ context.Context, user authboss.User) error {
u := user.(*User)
if _, ok := m.Users[u.Email]; ok {
return authboss.ErrUserFound
}
debugln("Created new user:", u.Name)
m.Users[u.Email] = *u
return nil
}
// LoadByConfirmSelector looks a user up by confirmation token
func (m MemStorer) LoadByConfirmSelector(_ context.Context, selector string) (user authboss.ConfirmableUser, err error) {
for _, v := range m.Users {
if v.ConfirmSelector == selector {
debugln("Loaded user by confirm selector:", selector, v.Name)
return &v, nil
}
}
return nil, authboss.ErrUserNotFound
}
// LoadByRecoverSelector looks a user up by confirmation selector
func (m MemStorer) LoadByRecoverSelector(_ context.Context, selector string) (user authboss.RecoverableUser, err error) {
for _, v := range m.Users {
if v.RecoverSelector == selector {
debugln("Loaded user by recover selector:", selector, v.Name)
return &v, nil
}
}
return nil, authboss.ErrUserNotFound
}
// AddRememberToken to a user
func (m MemStorer) AddRememberToken(_ context.Context, pid, token string) error {
m.Tokens[pid] = append(m.Tokens[pid], token)
debugf("Adding rm token to %s: %s\n", pid, token)
spew.Dump(m.Tokens)
return nil
}
// DelRememberTokens removes all tokens for the given pid
func (m MemStorer) DelRememberTokens(_ context.Context, pid string) error {
delete(m.Tokens, pid)
debugln("Deleting rm tokens from:", pid)
spew.Dump(m.Tokens)
return nil
}
// UseRememberToken finds the pid-token pair and deletes it.
// If the token could not be found return ErrTokenNotFound
func (m MemStorer) UseRememberToken(_ context.Context, pid, token string) error {
tokens, ok := m.Tokens[pid]
if !ok {
debugln("Failed to find rm tokens for:", pid)
return authboss.ErrTokenNotFound
}
for i, tok := range tokens {
if tok == token {
tokens[len(tokens)-1] = tokens[i]
m.Tokens[pid] = tokens[:len(tokens)-1]
debugf("Used remember for %s: %s\n", pid, token)
return nil
}
}
return authboss.ErrTokenNotFound
}
// NewFromOAuth2 creates an oauth2 user (but not in the database, just a blank one to be saved later)
func (m MemStorer) NewFromOAuth2(_ context.Context, provider string, details map[string]string) (authboss.OAuth2User, error) {
switch provider {
case "google":
email := details[aboauth.OAuth2Email]
var user *User
if u, ok := m.Users[email]; ok {
user = &u
} else {
user = &User{}
}
// Google OAuth2 doesn't allow us to fetch real name without more complicated API calls
// in order to do this properly in your own app, look at replacing the authboss oauth2.GoogleUserDetails
// method with something more thorough.
user.Name = "Unknown"
user.Email = details[aboauth.OAuth2Email]
user.OAuth2UID = details[aboauth.OAuth2UID]
user.Confirmed = true
return user, nil
}
return nil, errors.Errorf("unknown provider %s", provider)
}
// SaveOAuth2 user
func (m MemStorer) SaveOAuth2(_ context.Context, user authboss.OAuth2User) error {
u := user.(*User)
m.Users[u.Email] = *u
return nil
}
/*
func (s MemStorer) PutOAuth(uid, provider string, attr authboss.Attributes) error {
return s.Create(uid+provider, attr)
}
func (s MemStorer) GetOAuth(uid, provider string) (result interface{}, err error) {
user, ok := s.Users[uid+provider]
if !ok {
return nil, authboss.ErrUserNotFound
}
return &user, nil
}
func (s MemStorer) AddToken(key, token string) error {
s.Tokens[key] = append(s.Tokens[key], token)
fmt.Println("AddToken")
spew.Dump(s.Tokens)
return nil
}
func (s MemStorer) DelTokens(key string) error {
delete(s.Tokens, key)
fmt.Println("DelTokens")
spew.Dump(s.Tokens)
return nil
}
func (s MemStorer) UseToken(givenKey, token string) error {
toks, ok := s.Tokens[givenKey]
if !ok {
return authboss.ErrTokenNotFound
}
for i, tok := range toks {
if tok == token {
toks[i], toks[len(toks)-1] = toks[len(toks)-1], toks[i]
s.Tokens[givenKey] = toks[:len(toks)-1]
return nil
}
}
return authboss.ErrTokenNotFound
}
func (s MemStorer) ConfirmUser(tok string) (result interface{}, err error) {
fmt.Println("==============", tok)
for _, u := range s.Users {
if u.ConfirmToken == tok {
return &u, nil
}
}
return nil, authboss.ErrUserNotFound
}
func (s MemStorer) RecoverUser(rec string) (result interface{}, err error) {
for _, u := range s.Users {
if u.RecoverToken == rec {
return &u, nil
}
}
return nil, authboss.ErrUserNotFound
}
*/