-
Notifications
You must be signed in to change notification settings - Fork 14
/
otp.go
342 lines (272 loc) · 8.63 KB
/
otp.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
package otp
import (
//nolint //G501: Blocklisted import crypto/md5: weak cryptographic primitive
"crypto/md5"
"errors"
"fmt"
"math"
mail2 "net/mail"
"regexp"
"strconv"
"github.com/dongri/phonenumber"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/pkg/helper"
"github.com/coretrix/hitrix/pkg/queue/streams"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/generator"
"github.com/coretrix/hitrix/service/component/mail"
)
type IOTP interface {
Send(ormService *beeorm.Engine, send Send) (string, error)
Verify(ormService *beeorm.Engine, verify Verify) (bool, bool, error)
GetSMSGatewayRegistry() map[string]IOTPSMSGateway
}
type Phone struct {
Number string
ISO3166 phonenumber.ISO3166
}
type Email struct {
Email string
}
type Send struct {
Phone *Phone
Email *Email
EmailConfig *EmailConfig
}
type EmailConfig struct {
From string
Subject string
TemplateName string
}
type Verify struct {
Phone *Phone
Email *Email
Code string
}
type Config struct {
ClockService clock.IClock
SMSConfig SMSConfig
MailConfig MailConfig
CodeLength int
}
type SMSConfig struct {
SMSGateways []IOTPSMSGateway
RetryOTP bool
}
type MailConfig struct {
Sender mail.ISender
}
type OTP struct {
ClockService clock.IClock
GeneratorService generator.IGenerator
SMSGatewayPriority []IOTPSMSGateway
SMSGatewayName map[string]IOTPSMSGateway
SMSGatewayPhonePrefixRegex map[*regexp.Regexp]IOTPSMSGateway
SMSRetryOTP bool
MailSender *mail.Sender
CodeLength int
}
func NewOTP(config Config) *OTP {
otp := &OTP{
SMSGatewayPriority: make([]IOTPSMSGateway, 0),
SMSGatewayName: map[string]IOTPSMSGateway{},
SMSGatewayPhonePrefixRegex: map[*regexp.Regexp]IOTPSMSGateway{},
SMSRetryOTP: config.SMSConfig.RetryOTP,
}
for _, gateway := range config.SMSConfig.SMSGateways {
_, has := otp.SMSGatewayName[gateway.GetName()]
if has {
panic("OTPProviders duplicated for name: " + gateway.GetName())
}
otp.SMSGatewayName[gateway.GetName()] = gateway
phonePrefixes := gateway.GetPhonePrefixes()
if phonePrefixes == nil {
otp.SMSGatewayPriority = append(otp.SMSGatewayPriority, gateway)
} else {
regex := "^"
for i, phonePrefix := range phonePrefixes {
regex += "(\\" + phonePrefix + ")"
if i < len(phonePrefixes)-1 {
regex += "|"
}
}
compiledRegex, err := regexp.Compile(regex)
if err != nil {
panic(err)
}
otp.SMSGatewayPhonePrefixRegex[compiledRegex] = gateway
}
}
otp.CodeLength = 5
if config.CodeLength > 0 {
otp.CodeLength = config.CodeLength
}
return otp
}
func (o *OTP) Send(ormService *beeorm.Engine, send Send) (string, error) {
if send.Phone != nil {
return o.sendSMS(ormService, send)
} else if send.Email != nil {
return o.sendEmail(ormService, send)
}
panic("no send method selected")
}
func (o *OTP) Verify(ormService *beeorm.Engine, verify Verify) (bool, bool, error) {
if verify.Phone != nil {
return o.verifySMS(ormService, verify)
} else if verify.Email != nil {
return o.verifyEmail(ormService, verify)
}
panic("no verify method selected")
}
func (o *OTP) GetSMSGatewayRegistry() map[string]IOTPSMSGateway {
return o.SMSGatewayName
}
func (o *OTP) sendSMS(ormService *beeorm.Engine, send Send) (string, error) {
//validate phone
phone := send.Phone
var code string
var err error
gatewayPriority := make([]IOTPSMSGateway, 0)
for regex, gateway := range o.SMSGatewayPhonePrefixRegex {
if regex.MatchString(phone.Number) {
gatewayPriority = append(gatewayPriority, gateway)
}
}
if len(gatewayPriority) == 0 {
gatewayPriority = o.SMSGatewayPriority
}
for priority, gateway := range gatewayPriority {
code = gateway.GetCode()
otpTrackerEntity := &entity.OTPTrackerEntity{
Type: entity.OTPTrackerTypeSMS,
To: phone.Number,
Code: code,
GatewayName: gateway.GetName(),
GatewayPriority: uint8(priority),
GatewaySendStatus: entity.OTPTrackerGatewaySendStatusNew,
SentAt: o.ClockService.Now(),
}
otpTrackerEntity.GatewaySendRequest, otpTrackerEntity.GatewaySendResponse, err = gateway.SendOTP(phone, code)
if err != nil {
otpTrackerEntity.GatewaySendStatus = entity.OTPTrackerGatewaySendStatusGatewayError
} else {
otpTrackerEntity.GatewaySendStatus = entity.OTPTrackerGatewaySendStatusSent
}
ormService.Flush(otpTrackerEntity)
if err == nil {
ormService.GetRedis().Set(o.getRedisKey(phone.Number), otpTrackerEntity.ID, helper.Hour)
break
} else if o.SMSRetryOTP {
ormService.GetEventBroker().Publish(streams.StreamMsgRetryOTP, &RetryDTO{
Code: code,
Phone: phone,
OTPTrackerEntityID: otpTrackerEntity.ID,
Gateway: gateway.GetName(),
})
}
}
return code, err
}
func (o *OTP) sendEmail(ormService *beeorm.Engine, send Send) (string, error) {
if o.MailSender == nil {
panic("mail sender not defined")
}
email := send.Email.Email
_, err := mail2.ParseAddress(email)
if err != nil {
return "", errors.New("mail address not valid")
}
code := o.getCode()
otpTrackerEntity := &entity.OTPTrackerEntity{
Type: entity.OTPTrackerTypeSMS,
To: email,
Code: code,
GatewaySendStatus: entity.OTPTrackerGatewaySendStatusNew,
SentAt: o.ClockService.Now(),
}
err = o.MailSender.SendTemplate(ormService, &mail.Message{
From: send.EmailConfig.From,
To: email,
Subject: send.EmailConfig.Subject,
TemplateName: send.EmailConfig.TemplateName,
TemplateData: map[string]interface{}{"code": code},
})
if err != nil {
otpTrackerEntity.GatewaySendStatus = entity.OTPTrackerGatewaySendStatusGatewayError
} else {
otpTrackerEntity.GatewaySendStatus = entity.OTPTrackerGatewaySendStatusSent
}
ormService.Flush(otpTrackerEntity)
return code, err
}
func (o *OTP) verifySMS(ormService *beeorm.Engine, verify Verify) (bool, bool, error) {
otpTrackerEntity, err := o.getOTPTrackerEntity(ormService, verify.Phone.Number)
if err != nil {
return false, false, err
}
gateway := o.SMSGatewayName[otpTrackerEntity.GatewayName]
var otpRequestValid bool
var otpCodeValid bool
otpTrackerEntity.GatewayVerifyRequest, otpTrackerEntity.GatewayVerifyResponse, otpRequestValid, otpCodeValid, err =
gateway.VerifyOTP(verify.Phone, verify.Code, otpTrackerEntity.Code)
if err != nil {
otpTrackerEntity.GatewayVerifyStatus = entity.OTPTrackerGatewayVerifyStatusGatewayError
} else if !otpRequestValid {
otpTrackerEntity.GatewayVerifyStatus = entity.OTPTrackerGatewayVerifyStatusExpired
} else if !otpCodeValid {
otpTrackerEntity.GatewayVerifyStatus = entity.OTPTrackerGatewayVerifyStatusInvalidCode
} else {
otpTrackerEntity.GatewayVerifyStatus = entity.OTPTrackerGatewayVerifyStatusSuccess
}
ormService.Flush(otpTrackerEntity)
return otpRequestValid, otpCodeValid, err
}
func (o *OTP) verifyEmail(ormService *beeorm.Engine, verify Verify) (bool, bool, error) {
otpTrackerEntity, err := o.getOTPTrackerEntity(ormService, verify.Email.Email)
if err != nil {
return false, false, err
}
if otpTrackerEntity.Code == verify.Code {
otpTrackerEntity.GatewayVerifyStatus = entity.OTPTrackerGatewayVerifyStatusSuccess
} else {
otpTrackerEntity.GatewayVerifyStatus = entity.OTPTrackerGatewayVerifyStatusInvalidCode
}
ormService.Flush(otpTrackerEntity)
return true, otpTrackerEntity.Code == verify.Code, nil
}
func (o *OTP) getOTPTrackerEntity(ormService *beeorm.Engine, verifyKey string) (*entity.OTPTrackerEntity, error) {
otpTrackerEntityIDString, has := ormService.GetRedis().Get(o.getRedisKey(verifyKey))
if !has {
return nil, errors.New("OTP: redis key expired")
}
otpTrackerEntityID, err := strconv.ParseUint(otpTrackerEntityIDString, 10, 64)
if err != nil {
return nil, errors.New("OTP: " + err.Error())
}
otpTrackerEntity := &entity.OTPTrackerEntity{}
found := ormService.LoadByID(otpTrackerEntityID, otpTrackerEntity)
if !found {
return nil, errors.New("OTP tracker not found")
}
return otpTrackerEntity, nil
}
func (o *OTP) getRedisKey(verifyKey string) string {
// #nosec
return fmt.Sprintf("otp_%x", md5.Sum([]byte(verifyKey)))
}
func (o *OTP) getCode() string {
return strconv.FormatInt(
o.GeneratorService.GenerateRandomRangeNumber(
int64(math.Pow(10, float64(o.CodeLength-1))),
int64(math.Pow(10, float64(o.CodeLength)))-1,
),
10)
}
type RetryDTO struct {
Code string
Phone *Phone
OTPTrackerEntityID uint64
Gateway string
}