-
Notifications
You must be signed in to change notification settings - Fork 13
/
otp.go
243 lines (188 loc) · 6.45 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
package otp
import (
//nolint //G501: Blocklisted import crypto/md5: weak cryptographic primitive
"crypto/md5"
"errors"
"fmt"
"regexp"
"strconv"
"time"
"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"
)
type IOTP interface {
SendSMS(ormService *beeorm.Engine, phone *Phone) (string, error)
VerifyOTP(ormService *beeorm.Engine, phone *Phone, code string) (bool, bool, error)
Call(ormService *beeorm.Engine, phone *Phone, customMessage string) (string, error)
GetGatewayRegistry() map[string]IOTPSMSGateway
}
type OTP struct {
GatewayPriority []IOTPSMSGateway
GatewayName map[string]IOTPSMSGateway
GatewayPhonePrefixRegex map[*regexp.Regexp]IOTPSMSGateway
RetryOTP bool
}
type Phone struct {
Number string
ISO3166 phonenumber.ISO3166
}
func NewOTP(retryOTP bool, gateways ...IOTPSMSGateway) *OTP {
otp := &OTP{
GatewayPriority: make([]IOTPSMSGateway, 0),
GatewayName: map[string]IOTPSMSGateway{},
GatewayPhonePrefixRegex: map[*regexp.Regexp]IOTPSMSGateway{},
RetryOTP: retryOTP,
}
for _, gateway := range gateways {
_, has := otp.GatewayName[gateway.GetName()]
if has {
panic("OTPProviders duplicated for name: " + gateway.GetName())
}
otp.GatewayName[gateway.GetName()] = gateway
phonePrefixes := gateway.GetPhonePrefixes()
if phonePrefixes == nil {
otp.GatewayPriority = append(otp.GatewayPriority, 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.GatewayPhonePrefixRegex[compiledRegex] = gateway
}
}
return otp
}
func (o *OTP) SendSMS(ormService *beeorm.Engine, phone *Phone) (string, error) {
var code string
var err error
gatewayPriority := make([]IOTPSMSGateway, 0)
for regex, gateway := range o.GatewayPhonePrefixRegex {
if regex.MatchString(phone.Number) {
gatewayPriority = append(gatewayPriority, gateway)
}
}
if len(gatewayPriority) == 0 {
gatewayPriority = o.GatewayPriority
}
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: time.Now(), // TODO ClockService
}
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), otpTrackerEntity.ID, helper.Hour)
break
} else if o.RetryOTP {
ormService.GetEventBroker().Publish(streams.StreamMsgRetryOTP, &RetryDTO{
Code: code,
Phone: phone,
OTPTrackerEntityID: otpTrackerEntity.ID,
Gateway: gateway.GetName(),
})
}
}
return code, err
}
func (o *OTP) Call(ormService *beeorm.Engine, phone *Phone, customMessage string) (string, error) {
var code string
var err error
for priority, gateway := range o.GatewayPriority {
code = gateway.GetCode()
otpTrackerEntity := &entity.OTPTrackerEntity{
Type: entity.OTPTrackerTypeCallout,
To: phone.Number,
Code: code,
GatewayName: gateway.GetName(),
GatewayPriority: uint8(priority),
GatewaySendStatus: entity.OTPTrackerGatewaySendStatusNew,
SentAt: time.Now(), // TODO ClockService
}
otpTrackerEntity.GatewaySendRequest, otpTrackerEntity.GatewaySendResponse, err = gateway.Call(phone, code, customMessage)
if err != nil {
otpTrackerEntity.GatewaySendStatus = entity.OTPTrackerGatewaySendStatusGatewayError
} else {
otpTrackerEntity.GatewaySendStatus = entity.OTPTrackerGatewaySendStatusSent
}
ormService.Flush(otpTrackerEntity)
if err == nil {
ormService.GetRedis().Set(o.getRedisKey(phone), otpTrackerEntity.ID, helper.Hour)
break
}
}
return code, err
}
func (o *OTP) VerifyOTP(ormService *beeorm.Engine, phone *Phone, code string) (bool, bool, error) {
otpTrackerEntity, err := o.getOTPTrackerEntity(ormService, phone)
if err != nil {
return false, false, err
}
gateway := o.GatewayName[otpTrackerEntity.GatewayName]
var otpRequestValid bool
var otpCodeValid bool
otpTrackerEntity.GatewayVerifyRequest, otpTrackerEntity.GatewayVerifyResponse, otpRequestValid, otpCodeValid, err =
gateway.VerifyOTP(phone, code, otpTrackerEntity.Code)
// TODO add error to tracker
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) GetGatewayRegistry() map[string]IOTPSMSGateway {
return o.GatewayName
}
func (o *OTP) getOTPTrackerEntity(ormService *beeorm.Engine, phone *Phone) (*entity.OTPTrackerEntity, error) {
otpTrackerEntityIDString, has := ormService.GetRedis().Get(o.getRedisKey(phone))
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(phone *Phone) string {
// #nosec
return fmt.Sprintf("%x", md5.Sum([]byte(phone.Number)))
}
type RetryDTO struct {
Code string
Phone *Phone
OTPTrackerEntityID uint64
Gateway string
}