-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontract_test.go
More file actions
194 lines (174 loc) · 5.63 KB
/
Copy pathcontract_test.go
File metadata and controls
194 lines (174 loc) · 5.63 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
package authflow_test
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/aatuh/authflow"
)
func TestPublicContractEmailOTPAndStepUpContinuation(t *testing.T) {
store := newContractStore()
sender := &contractSender{}
service, err := authflow.NewService(store, store, store, sender, authflow.Config{
OTPSecret: "test-secret",
StepUpPolicy: authflow.StaticPolicy{authflow.FactorEmailOTP, "totp"},
StepUpContinuations: store,
})
if err != nil {
t.Fatalf("NewService: %v", err)
}
challenge, err := service.StartEmailOTP(context.Background(), "user@example.com")
if err != nil {
t.Fatalf("StartEmailOTP: %v", err)
}
_, err = service.VerifyEmailOTP(context.Background(), challenge.ID, sender.code)
var stepUp authflow.StepUpRequiredError
if !errors.As(err, &stepUp) {
t.Fatalf("expected StepUpRequiredError, got %v", err)
}
result, err := service.CompleteStepUp(context.Background(), stepUp.ContinuationToken, []authflow.Factor{"totp"})
if err != nil {
t.Fatalf("CompleteStepUp: %v", err)
}
if result.Token == "" {
t.Fatal("expected token")
}
}
func TestPublicContractCustomPrimaryMethod(t *testing.T) {
store := newContractStore()
service, err := authflow.NewService(store, store, store, &contractSender{}, authflow.Config{OTPSecret: "test-secret"})
if err != nil {
t.Fatalf("NewService: %v", err)
}
method := authflow.PrimaryMethodFunc(func(context.Context, authflow.Credential) (authflow.Identity, []authflow.Factor, error) {
return authflow.Identity{Provider: "magic_link", Subject: "user@example.com", Email: "user@example.com"}, []authflow.Factor{"magic_link"}, nil
})
result, err := service.LoginWithMethod(context.Background(), method, authflow.Credential{Value: "token"})
if err != nil {
t.Fatalf("LoginWithMethod: %v", err)
}
if result.User.Email != "user@example.com" {
t.Fatalf("email = %q", result.User.Email)
}
}
type contractSender struct {
code string
}
func (s *contractSender) SendOTP(_ context.Context, _ string, code string, _ time.Time) error {
s.code = code
return nil
}
type contractStore struct {
mu sync.Mutex
nextID int64
users map[string]authflow.User
identity map[string]authflow.User
otps map[string]authflow.OTPChallengeRecord
stepUps map[string]authflow.StepUpContinuationRecord
}
func newContractStore() *contractStore {
return &contractStore{
nextID: 1,
users: map[string]authflow.User{},
identity: map[string]authflow.User{},
otps: map[string]authflow.OTPChallengeRecord{},
stepUps: map[string]authflow.StepUpContinuationRecord{},
}
}
func (s *contractStore) FindUserByEmail(_ context.Context, email string) (authflow.User, error) {
s.mu.Lock()
defer s.mu.Unlock()
user, ok := s.users[email]
if !ok {
return authflow.User{}, authflow.ErrNotFound
}
return user, nil
}
func (s *contractStore) CreateUser(_ context.Context, email string) (authflow.User, error) {
s.mu.Lock()
defer s.mu.Unlock()
user := authflow.User{ID: s.nextID, Email: email, CreatedAt: time.Now().UTC()}
s.nextID++
s.users[email] = user
return user, nil
}
func (s *contractStore) FindUserByIdentity(_ context.Context, provider string, subject string) (authflow.User, error) {
s.mu.Lock()
defer s.mu.Unlock()
user, ok := s.identity[provider+"|"+subject]
if !ok {
return authflow.User{}, authflow.ErrNotFound
}
return user, nil
}
func (s *contractStore) UpsertIdentity(_ context.Context, userID int64, identity authflow.Identity) error {
s.mu.Lock()
defer s.mu.Unlock()
for _, user := range s.users {
if user.ID == userID {
s.identity[identity.Provider+"|"+identity.Subject] = user
return nil
}
}
return authflow.ErrNotFound
}
func (s *contractStore) SaveOTPChallenge(_ context.Context, challenge authflow.OTPChallengeRecord) error {
s.mu.Lock()
defer s.mu.Unlock()
s.otps[challenge.ID] = challenge
return nil
}
func (s *contractStore) VerifyOTPChallenge(_ context.Context, id string, codeHash string, now time.Time, maxAttempts int) (authflow.OTPChallengeRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
challenge, ok := s.otps[id]
if !ok {
return authflow.OTPChallengeRecord{}, authflow.ErrNotFound
}
if challenge.ConsumedAt != nil {
return authflow.OTPChallengeRecord{}, authflow.ErrChallengeConsumed
}
if !challenge.ExpiresAt.After(now) {
return authflow.OTPChallengeRecord{}, authflow.ErrExpiredChallenge
}
if challenge.Attempts >= maxAttempts {
return authflow.OTPChallengeRecord{}, authflow.ErrTooManyAttempts
}
if challenge.CodeHash != codeHash {
challenge.Attempts++
s.otps[id] = challenge
return authflow.OTPChallengeRecord{}, authflow.ErrInvalidOTP
}
consumedAt := now
challenge.ConsumedAt = &consumedAt
s.otps[id] = challenge
return challenge, nil
}
func (s *contractStore) SaveStepUpContinuation(_ context.Context, continuation authflow.StepUpContinuationRecord) error {
s.mu.Lock()
defer s.mu.Unlock()
s.stepUps[continuation.TokenHash] = continuation
return nil
}
func (s *contractStore) ConsumeStepUpContinuation(_ context.Context, tokenHash string, now time.Time) (authflow.StepUpContinuationRecord, error) {
s.mu.Lock()
defer s.mu.Unlock()
continuation, ok := s.stepUps[tokenHash]
if !ok {
return authflow.StepUpContinuationRecord{}, authflow.ErrNotFound
}
if continuation.ConsumedAt != nil {
return authflow.StepUpContinuationRecord{}, authflow.ErrChallengeConsumed
}
if !continuation.ExpiresAt.After(now) {
return authflow.StepUpContinuationRecord{}, authflow.ErrExpiredContinuation
}
consumedAt := now
continuation.ConsumedAt = &consumedAt
s.stepUps[tokenHash] = continuation
return continuation, nil
}
func (s *contractStore) SaveSession(context.Context, authflow.SessionRecord) error {
return nil
}