-
Notifications
You must be signed in to change notification settings - Fork 3
/
mocks.go
706 lines (624 loc) · 17.2 KB
/
mocks.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
package test
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"time"
webauthnProto "github.com/duo-labs/webauthn/protocol"
webauthnLib "github.com/duo-labs/webauthn/webauthn"
redisLib "github.com/go-redis/redis"
"github.com/pkg/errors"
auth "github.com/fmitra/authenticator"
)
const (
// OTPCodehash is a SHA512 hash of `123456`
OTPCodeHash = "ba3253876aed6bc22d4a6ff53d8406c6ad864195ed144ab5" +
"c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd31" +
"45464e2a0bab413"
OTPCode = "123456"
)
func MockTokenHash(a, m string, t int64) string {
addr := "jane@example.com"
if a != "" {
addr = a
}
method := "email"
if m != "" {
method = m
}
o := struct {
CodeHash string `json:"code_hash"`
ExpiresAt int64 `json:"expires_at"`
Address string `json:"address"`
DeliveryMethod string `json:"delivery_method"`
}{
CodeHash: OTPCodeHash,
ExpiresAt: t,
Address: addr,
DeliveryMethod: method,
}
b, _ := json.Marshal(o)
return base64.RawURLEncoding.EncodeToString(b)
}
// OTPService mocks auth.OTPService interface.
type OTPService struct {
TOTPQRStringFn func(u *auth.User) (string, error)
TOTPSecretFn func(u *auth.User) (string, error)
OTPCodeFn func(address string, method auth.DeliveryMethod) (string, string, error)
ValidateOTPFn func(code, hash string) error
ValidateTOTPFn func(u *auth.User, code string) error
Calls struct {
TOTPQRString int
TOTPSecret int
OTPCode int
ValidateOTP int
ValidateTOTP int
}
}
// MessageRepository mocks auth.MessageRepository interface.
type MessageRepository struct {
PublishFn func(ctx context.Context, msg *auth.Message) error
RecentFn func(ctx context.Context) (<-chan *auth.Message, <-chan error)
Calls struct {
Publish int
Recent int
}
}
// MessagingService mocks auth.MessagingService interface.
type MessagingService struct {
SendFn func() error
Calls struct {
Send int
}
}
// TokenService mocks auth.TokenService interface.
type TokenService struct {
RefreshableTillFn func() time.Time
RefreshableFn func() error
CreateFn func() (*auth.Token, error)
SignFn func() (string, error)
ValidateFn func() (*auth.Token, error)
RevokeFn func() error
CookiesFn func() []*http.Cookie
Calls struct {
RefreshableTill int
Refreshable int
Create int
Sign int
Validate int
Revoke int
Cookies int
}
}
// RepositoryManager mocks auth.RepositoryManager interface.
type RepositoryManager struct {
NewWithTransactionFn func() (auth.RepositoryManager, error)
WithAtomicFn func() (interface{}, error)
LoginHistoryFn func() auth.LoginHistoryRepository
DeviceFn func() auth.DeviceRepository
UserFn func() auth.UserRepository
Calls struct {
NewWithTransaction int
WithAtomic int
LoginHistory int
Device int
User int
}
}
// LoginHistoryRepository mocks auth.LoginHistoryRepository.
type LoginHistoryRepository struct {
ByTokenIDFn func() (*auth.LoginHistory, error)
ByUserIDFn func() ([]*auth.LoginHistory, error)
CreateFn func() error
GetForUpdateFn func() (*auth.LoginHistory, error)
UpdateFn func() error
Calls struct {
ByUserID int
Create int
GetForUpdate int
Update int
ByTokenID int
}
}
// DeviceRepository mocks auth.DeviceRepository.
type DeviceRepository struct {
ByIDFn func() (*auth.Device, error)
ByClientIDFn func() (*auth.Device, error)
ByUserIDFn func() ([]*auth.Device, error)
CreateFn func() error
GetForUpdateFn func() (*auth.Device, error)
UpdateFn func() error
RemoveFn func() error
Calls struct {
ByID int
ByClientID int
ByUserID int
Create int
GetForUpdate int
Update int
Remove int
}
}
// UserRepository mocks auth.UserRepository.
type UserRepository struct {
ByIdentityFn func() (*auth.User, error)
GetForUpdateFn func() (*auth.User, error)
DisableOTPFn func() (*auth.User, error)
RemoveDeliveryMethodFn func() (*auth.User, error)
CreateFn func() error
ReCreateFn func() error
UpdateFn func() error
Calls struct {
ByIdentity int
DisableOTP int
RemoveDeliveryMethod int
GetForUpdate int
Create int
ReCreate int
Update int
}
}
// WebAuthnLib mocks duo-labs/webauthn third party library.
type WebAuthnLib struct {
BeginRegistrationFn func() (*webauthnProto.CredentialCreation, *webauthnLib.SessionData, error)
FinishRegistrationFn func() (*webauthnLib.Credential, error)
BeginLoginFn func() (*webauthnProto.CredentialAssertion, *webauthnLib.SessionData, error)
FinishLoginFn func() (*webauthnLib.Credential, error)
Calls struct {
BeginRegistration int
FinishRegistration int
BeginLogin int
FinishLogin int
}
}
// WebAuthnService mocks auth.WebAuthnService.
type WebAuthnService struct {
BeginSignUpFn func() ([]byte, error)
FinishSignUpFn func() (*auth.Device, error)
BeginLoginFn func() ([]byte, error)
FinishLoginFn func() error
Calls struct {
BeginSignUp int
FinishSignUp int
BeginLogin int
FinishLogin int
}
}
// Logger mocks a go-kit logger.
type Logger struct {
LogFn func() error
Calls struct {
Log int
}
}
// Rediser mocks go-redis client.
type Rediser struct {
GetFn func() *redisLib.StringCmd
SetFn func() *redisLib.StatusCmd
WithContextFn func() *redisLib.Client
CloseFn func() error
Calls struct {
Get int
Set int
WithContext int
Close int
}
}
// BeginRegistration mock.
func (m *WebAuthnLib) BeginRegistration(user webauthnLib.User, opts ...webauthnLib.RegistrationOption) (*webauthnProto.CredentialCreation, *webauthnLib.SessionData, error) {
m.Calls.BeginRegistration++
if m.BeginRegistrationFn != nil {
return m.BeginRegistrationFn()
}
return nil, nil, errors.New("failed to begin registration")
}
// FinishRegistration mock.
func (m *WebAuthnLib) FinishRegistration(user webauthnLib.User, session webauthnLib.SessionData, r *http.Request) (*webauthnLib.Credential, error) {
m.Calls.FinishRegistration++
if m.FinishRegistrationFn != nil {
return m.FinishRegistrationFn()
}
return nil, errors.New("failed to fnish registration")
}
// BeginLogin mock.
func (m *WebAuthnLib) BeginLogin(user webauthnLib.User, opts ...webauthnLib.LoginOption) (*webauthnProto.CredentialAssertion, *webauthnLib.SessionData, error) {
m.Calls.BeginLogin++
if m.BeginLoginFn != nil {
return m.BeginLoginFn()
}
return nil, nil, errors.New("failed to begin login")
}
// FinishLogin mock.
func (m *WebAuthnLib) FinishLogin(user webauthnLib.User, session webauthnLib.SessionData, r *http.Request) (*webauthnLib.Credential, error) {
m.Calls.FinishLogin++
if m.FinishLoginFn != nil {
return m.FinishLoginFn()
}
return nil, errors.New("failed to finish login")
}
// NewWithTransaction mock.
func (m *RepositoryManager) NewWithTransaction(ctx context.Context) (auth.RepositoryManager, error) {
m.Calls.NewWithTransaction++
if m.NewWithTransactionFn != nil {
return m.NewWithTransactionFn()
}
return m, nil
}
// WithAtomic mock.
func (m *RepositoryManager) WithAtomic(operation func() (interface{}, error)) (interface{}, error) {
m.Calls.WithAtomic++
if m.WithAtomicFn != nil {
return m.WithAtomicFn()
}
return nil, errors.New("failed to start transaction")
}
// LoginHistory mock.
func (m *RepositoryManager) LoginHistory() auth.LoginHistoryRepository {
m.Calls.LoginHistory++
if m.LoginHistoryFn != nil {
return m.LoginHistoryFn()
}
return &LoginHistoryRepository{}
}
// Device mock.
func (m *RepositoryManager) Device() auth.DeviceRepository {
m.Calls.Device++
if m.DeviceFn != nil {
return m.DeviceFn()
}
return &DeviceRepository{}
}
// User mock.
func (m *RepositoryManager) User() auth.UserRepository {
m.Calls.User++
if m.UserFn != nil {
return m.UserFn()
}
return &UserRepository{}
}
// RemoveDeliveryMethod mock.
func (m *UserRepository) RemoveDeliveryMethod(ctx context.Context, userID string, method auth.DeliveryMethod) (*auth.User, error) {
m.Calls.RemoveDeliveryMethod++
if m.RemoveDeliveryMethodFn != nil {
return m.RemoveDeliveryMethodFn()
}
return &auth.User{}, nil
}
// DisableOTP mock.
func (m *UserRepository) DisableOTP(ctx context.Context, userID string, method auth.DeliveryMethod) (*auth.User, error) {
m.Calls.DisableOTP++
if m.DisableOTPFn != nil {
return m.DisableOTPFn()
}
return &auth.User{}, nil
}
// ByIdentity mock.
func (m *UserRepository) ByIdentity(ctx context.Context, attribute, value string) (*auth.User, error) {
m.Calls.ByIdentity++
if m.ByIdentityFn != nil {
return m.ByIdentityFn()
}
return &auth.User{}, nil
}
// GetForUpdate mock.
func (m *UserRepository) GetForUpdate(ctx context.Context, userID string) (*auth.User, error) {
m.Calls.GetForUpdate++
if m.GetForUpdateFn != nil {
return m.GetForUpdateFn()
}
return &auth.User{}, nil
}
// Create mock.
func (m *UserRepository) Create(ctx context.Context, u *auth.User) error {
m.Calls.Create++
if m.CreateFn != nil {
return m.CreateFn()
}
return nil
}
// ReCreate mock.
func (m *UserRepository) ReCreate(ctx context.Context, u *auth.User) error {
m.Calls.ReCreate++
if m.ReCreateFn != nil {
return m.ReCreateFn()
}
return nil
}
// Update mock.
func (m *UserRepository) Update(ctx context.Context, u *auth.User) error {
m.Calls.Update++
if m.UpdateFn != nil {
return m.UpdateFn()
}
return nil
}
// Remove mock.
func (m *DeviceRepository) Remove(ct context.Context, deviceID, userID string) error {
m.Calls.Remove++
if m.RemoveFn != nil {
return m.RemoveFn()
}
return nil
}
// ByID mock.
func (m *DeviceRepository) ByID(ctx context.Context, deviceID string) (*auth.Device, error) {
m.Calls.ByID++
if m.ByIDFn != nil {
return m.ByIDFn()
}
return &auth.Device{}, nil
}
// ByClientID mock.
func (m *DeviceRepository) ByClientID(ctx context.Context, userID string, clientID []byte) (*auth.Device, error) {
m.Calls.ByClientID++
if m.ByClientIDFn != nil {
return m.ByClientIDFn()
}
return &auth.Device{}, nil
}
// ByUserID mock.
func (m *DeviceRepository) ByUserID(ctx context.Context, userID string) ([]*auth.Device, error) {
m.Calls.ByUserID++
if m.ByUserIDFn != nil {
return m.ByUserIDFn()
}
devices := make([]*auth.Device, 1)
devices = append(devices, &auth.Device{})
return devices, nil
}
// Create mock.
func (m *DeviceRepository) Create(ctx context.Context, device *auth.Device) error {
m.Calls.Create++
if m.CreateFn != nil {
return m.CreateFn()
}
return nil
}
// GetForUpdate mock.
func (m *DeviceRepository) GetForUpdate(ctx context.Context, deviceID string) (*auth.Device, error) {
m.Calls.GetForUpdate++
if m.GetForUpdateFn != nil {
return m.GetForUpdateFn()
}
return &auth.Device{}, nil
}
// Update mock.
func (m *DeviceRepository) Update(ctx context.Context, device *auth.Device) error {
m.Calls.Update++
if m.UpdateFn != nil {
return m.UpdateFn()
}
return nil
}
// ByUserID mock.
func (m *LoginHistoryRepository) ByUserID(ctx context.Context, userID string, limit, offset int) ([]*auth.LoginHistory, error) {
m.Calls.ByUserID++
if m.ByUserIDFn != nil {
return m.ByUserIDFn()
}
logins := make([]*auth.LoginHistory, 1)
logins = append(logins, &auth.LoginHistory{})
return logins, nil
}
// ByTokenID mock.
func (m *LoginHistoryRepository) ByTokenID(ctx context.Context, tokenID string) (*auth.LoginHistory, error) {
m.Calls.ByTokenID++
if m.ByTokenIDFn != nil {
return m.ByTokenIDFn()
}
return &auth.LoginHistory{}, nil
}
// Create mock.
func (m *LoginHistoryRepository) Create(ctx context.Context, login *auth.LoginHistory) error {
m.Calls.Create++
if m.CreateFn != nil {
return m.CreateFn()
}
return nil
}
// GetForUpdate mock.
func (m *LoginHistoryRepository) GetForUpdate(ctx context.Context, tokenID string) (*auth.LoginHistory, error) {
m.Calls.GetForUpdate++
if m.GetForUpdateFn != nil {
return m.GetForUpdateFn()
}
return &auth.LoginHistory{}, nil
}
// Update mock.
func (m *LoginHistoryRepository) Update(ctx context.Context, login *auth.LoginHistory) error {
m.Calls.Update++
if m.UpdateFn != nil {
return m.UpdateFn()
}
return nil
}
// RefreshableTill mock.
func (m *TokenService) RefreshableTill(ctx context.Context, token *auth.Token, refreshToken string) time.Time {
m.Calls.RefreshableTill++
if m.RefreshableTillFn != nil {
return m.RefreshableTillFn()
}
return time.Time{}
}
// Refreshable mock.
func (m *TokenService) Refreshable(ctx context.Context, token *auth.Token, refreshToken string) error {
m.Calls.Refreshable++
if m.RefreshableFn != nil {
return m.RefreshableFn()
}
return nil
}
// Cookies mock.
func (m *TokenService) Cookies(ctx context.Context, token *auth.Token) []*http.Cookie {
m.Calls.Cookies++
if m.CookiesFn != nil {
return m.CookiesFn()
}
return []*http.Cookie{{}}
}
// Create mock.
func (m *TokenService) Create(ctx context.Context, u *auth.User, state auth.TokenState, options ...auth.TokenOption) (*auth.Token, error) {
m.Calls.Create++
if m.CreateFn != nil {
return m.CreateFn()
}
return nil, errors.New("failed to create token")
}
// Sign mock.
func (m *TokenService) Sign(ctx context.Context, token *auth.Token) (string, error) {
m.Calls.Sign++
if m.SignFn != nil {
return m.SignFn()
}
return "", errors.New("failed to sign token")
}
// Validate mock.
func (m *TokenService) Validate(ctx context.Context, signedToken string, clientID string) (*auth.Token, error) {
m.Calls.Validate++
if m.ValidateFn != nil {
return m.ValidateFn()
}
return nil, errors.New("token is not valid")
}
// Revoke mock.
func (m *TokenService) Revoke(ctx context.Context, tokenID string) error {
m.Calls.Revoke++
if m.RevokeFn != nil {
return m.RevokeFn()
}
return errors.New("token revocation failed")
}
// BeginSignUp mock.
func (m *WebAuthnService) BeginSignUp(ctx context.Context, user *auth.User) ([]byte, error) {
m.Calls.BeginSignUp++
if m.BeginSignUpFn != nil {
return m.BeginSignUpFn()
}
return nil, errors.New("failed to start signup")
}
// FinishSignUp mock.
func (m *WebAuthnService) FinishSignUp(ctx context.Context, user *auth.User, r *http.Request) (*auth.Device, error) {
m.Calls.FinishSignUp++
if m.FinishSignUpFn != nil {
return m.FinishSignUpFn()
}
return nil, errors.New("failed to finish signup")
}
// BeginLogin mock.
func (m *WebAuthnService) BeginLogin(ctx context.Context, user *auth.User) ([]byte, error) {
m.Calls.BeginLogin++
if m.BeginLoginFn != nil {
return m.BeginLoginFn()
}
return nil, errors.New("failed to begin login")
}
// FinishLogin mock.
func (m *WebAuthnService) FinishLogin(ctx context.Context, user *auth.User, r *http.Request) error {
m.Calls.FinishLogin++
if m.FinishLoginFn != nil {
return m.FinishLoginFn()
}
return errors.New("failed to finsih login")
}
// Log mock.
func (m *Logger) Log(keyvals ...interface{}) error {
m.Calls.Log++
if m.LogFn != nil {
return m.LogFn()
}
return nil
}
// Get mock.
func (m *Rediser) Get(key string) *redisLib.StringCmd {
m.Calls.Get++
if m.GetFn != nil {
return m.GetFn()
}
return nil
}
// Set mock.
func (m *Rediser) Set(key string, v interface{}, t time.Duration) *redisLib.StatusCmd {
m.Calls.Set++
if m.SetFn != nil {
return m.SetFn()
}
return nil
}
// WithContext mock.
func (m *Rediser) WithContext(ctxt context.Context) *redisLib.Client {
m.Calls.WithContext++
if m.WithContextFn != nil {
return m.WithContextFn()
}
return nil
}
// Close mock.
func (m *Rediser) Close() error {
m.Calls.Close++
if m.CloseFn != nil {
return m.CloseFn()
}
return nil
}
// Send mock.
func (m *MessagingService) Send(ctx context.Context, message, addr string, method auth.DeliveryMethod) error {
m.Calls.Send++
if m.SendFn != nil {
return m.SendFn()
}
return nil
}
// Publish mock.
func (m *MessageRepository) Publish(ctx context.Context, msg *auth.Message) error {
m.Calls.Publish++
if m.PublishFn != nil {
return m.PublishFn(ctx, msg)
}
return nil
}
// Recent mock.
func (m *MessageRepository) Recent(ctx context.Context) (<-chan *auth.Message, <-chan error) {
m.Calls.Recent++
if m.RecentFn != nil {
return m.RecentFn(ctx)
}
msgc := make(chan *auth.Message)
errc := make(chan error, 1)
return msgc, errc
}
func (s *OTPService) TOTPQRString(u *auth.User) (string, error) {
s.Calls.TOTPQRString++
if s.TOTPQRStringFn != nil {
return s.TOTPQRStringFn(u)
}
return "", nil
}
func (s *OTPService) TOTPSecret(u *auth.User) (string, error) {
s.Calls.TOTPSecret++
if s.TOTPSecretFn != nil {
return s.TOTPSecretFn(u)
}
return "", nil
}
func (s *OTPService) OTPCode(address string, method auth.DeliveryMethod) (string, string, error) {
s.Calls.OTPCode++
if s.OTPCodeFn != nil {
return s.OTPCodeFn(address, method)
}
return "", "", nil
}
func (s *OTPService) ValidateOTP(code, hash string) error {
s.Calls.ValidateOTP++
if s.ValidateOTPFn != nil {
return s.ValidateOTPFn(code, hash)
}
return nil
}
func (s *OTPService) ValidateTOTP(u *auth.User, code string) error {
s.Calls.ValidateTOTP++
if s.ValidateTOTPFn != nil {
return s.ValidateTOTPFn(u, code)
}
return nil
}