-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge.go
204 lines (170 loc) · 5.65 KB
/
challenge.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
package iam
import (
"context"
"errors"
"fmt"
"strconv"
"time"
"github.com/dannypaul/go-skeleton/internal/config"
"github.com/dannypaul/go-skeleton/internal/exception"
"github.com/dannypaul/go-skeleton/internal/kit"
"github.com/dannypaul/go-skeleton/internal/primitive"
"github.com/dannypaul/go-skeleton/internal/repository"
"github.com/dannypaul/go-skeleton/internal/validation"
)
type Challenge struct {
Id primitive.Id `bson:"_id,omitempty" json:"id"`
CreatedAt time.Time `bson:"createdAt" json:"-"`
UpdatedAt time.Time `bson:"updatedAt" json:"-"`
IdentityType IdentityType `bson:"identityType" json:"identityType"`
OTP string `bson:"otp" json:"-"`
FailedVerificationCount int `bson:"failedVerificationCount" json:"-"`
// Phone
Phone Phone `bson:"phone,omitempty" json:"phone"`
// Email
EmailId string `bson:"emailId,omitempty" json:"emailId"`
}
func (c Challenge) Validate() error {
if c.IdentityType == PHONE {
if err := validation.ValidatePhone(c.Phone.Number); err != nil {
return err
}
c.EmailId = ""
}
if c.IdentityType == EMAIL {
if err := validation.ValidateEmailId(c.EmailId); err != nil {
return err
}
c.Phone = Phone{}
}
return nil
}
func (s svc) FindChallengeByIdentity(ctx context.Context, identity Identity) (Challenge, error) {
if identity.Type != PHONE && identity.Type != EMAIL {
return Challenge{}, errors.New(exception.IdentityTypeNotFound)
}
var copier repository.Copier
var err error
if identity.Type == PHONE {
copier, err = s.challengeRepo.FindSingle(ctx, []repository.Filter{{Key: "phone.number", Value: identity.Phone.Number}})
}
if identity.Type == EMAIL {
copier, err = s.challengeRepo.FindSingle(ctx, []repository.Filter{{Key: "emailId", Value: identity.EmailId}})
}
if err != nil {
return Challenge{}, err
}
var challenge Challenge
err = copier.Copy(&challenge)
return challenge, err
}
func (s svc) Challenge(ctx context.Context, req Challenge) (Challenge, error) {
if err := req.Validate(); err != nil {
return Challenge{}, err
}
conf, _ := config.Get()
now := time.Now()
otp, err := kit.GenerateOTP(6)
if err != nil {
return Challenge{}, err
}
identity := Identity{
Type: req.IdentityType,
EmailId: req.EmailId,
Phone: &req.Phone,
}
challenge, err := s.FindChallengeByIdentity(ctx, identity)
if errors.Is(err, exception.ErrNotFound) {
req.CreatedAt = now
req.UpdatedAt = now
req.OTP = otp
copier, err := s.challengeRepo.Create(ctx, req)
if err != nil {
return Challenge{}, fmt.Errorf("could not save the challenge request to persistence %w", err)
}
var createdChallenge Challenge
err = copier.Copy(&createdChallenge)
if err != nil {
return Challenge{}, fmt.Errorf("could not copy the challenge to local variable %w", err)
}
challenge = createdChallenge
}
if !challenge.UpdatedAt.Equal(challenge.CreatedAt) {
duration := now.Sub(challenge.UpdatedAt)
if duration.Seconds() < conf.ChallengeTTL.Seconds() {
return Challenge{}, errors.New(exception.TooManyChallengeRequests)
}
}
err = s.challengeRepo.SetAllById(ctx, challenge.Id, []repository.KeyValue{
{"updatedAt", time.Now()},
{"otp", otp},
})
if err != nil {
return Challenge{}, err
}
if req.IdentityType == PHONE {
err = s.notificationService.VerifyPhone(ctx, challenge.Phone.Number, otp)
if err != nil {
return Challenge{}, fmt.Errorf("could not send the verification OTP to device %w", err)
}
}
if req.IdentityType == EMAIL {
err = s.notificationService.VerifyEmailId(ctx, challenge.EmailId, otp)
if err != nil {
return Challenge{}, fmt.Errorf("could not send the verification OTP to emailId %w", err)
}
}
return challenge, nil
}
func (s svc) Verify(ctx context.Context, req VerifyReq) (Session, error) {
identity := Identity{
Type: req.IdentityType,
EmailId: req.EmailId,
Phone: &req.Phone,
}
challenge, err := s.FindChallengeByIdentity(ctx, identity)
if err != nil {
if errors.Is(err, exception.ErrNotFound) {
return Session{}, errors.New(exception.ChallengeNotFound)
}
return Session{}, fmt.Errorf("could not find challenge request for the given emailId %w", err)
}
if challenge.FailedVerificationCount >= 3 {
_, err = s.challengeRepo.Delete(ctx, challenge.Id)
if err != nil {
return Session{}, fmt.Errorf("could not delete the challenge request after failed verification limit exceeded %w", err)
}
return Session{}, errors.New(exception.FailedVerificationLimitExceeded)
}
if challenge.OTP != req.OTP {
err = s.challengeRepo.IncrementById(ctx, challenge.Id, "failedVerificationCount", 1)
return Session{}, errors.New(exception.VerificationFailed)
}
user, err := s.FindUserByIdentity(ctx, identity)
if err != nil {
if errors.Is(err, exception.ErrNotFound) {
return Session{}, errors.New(exception.UserNotRegistered)
}
return Session{}, fmt.Errorf("could not find the user by identity, %w", err)
}
user.Version += 1
_, identityIndex, err := user.Identities.getIdentity(req.IdentityType)
patchers := []repository.Patch{
{"$set", "identities." + strconv.Itoa(identityIndex) + ".verified", true},
{"$set", "failedAuthAttempts", 0},
{"$inc", "version", 1},
}
err = s.userRepo.Patch(ctx, user.Id, patchers)
if err != nil {
return Session{}, fmt.Errorf("could not update the user %w", err)
}
token, err := user.createToken(true)
if err != nil {
return Session{}, fmt.Errorf("could not create token for the user %w", err)
}
_, err = s.challengeRepo.Delete(ctx, challenge.Id)
if err != nil {
return Session{}, fmt.Errorf("could not delete the challenge request %w", err)
}
return Session{User: user, Token: token}, err
}