-
Notifications
You must be signed in to change notification settings - Fork 4
/
verifier.go
431 lines (358 loc) · 10 KB
/
verifier.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
package twilio
import (
"context"
"net/http"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/twilio/twilio-go"
"github.com/twilio/twilio-go/client"
lookupsv1 "github.com/twilio/twilio-go/rest/lookups/v1"
verifyv2 "github.com/twilio/twilio-go/rest/verify/v2"
grpc_client "github.com/code-payments/code-server/pkg/grpc/client"
"github.com/code-payments/code-server/pkg/metrics"
"github.com/code-payments/code-server/pkg/phone"
)
const (
metricsStructName = "phone.twilio.verifier"
)
var (
androidAppHash = "+8j1B159Xfs"
)
const (
// https://www.twilio.com/docs/api/errors/60200
invalidParameterCode = 60200
// https://www.twilio.com/docs/api/errors/60202
maxCheckAttemptsCode = 60202
// https://www.twilio.com/docs/api/errors/60203
maxSendAttemptsCode = 60203
// https://www.twilio.com/docs/api/errors/60220
useCaseVettingCode = 60220
// https://www.twilio.com/docs/api/errors/60410
fraudDetectionCode = 60410
)
var (
defaultChannel = "sms"
)
var (
carrierMapKey = "carrier"
mobileCountryCodeMapKey = "mobile_country_code"
mobileNetworkCodeMapKey = "mobile_network_code"
phoneTypeMapKey = "type"
)
var (
statusApproved = "approved"
statusCanceled = "canceled"
statusPending = "pending"
)
// https://www.twilio.com/docs/lookup/api#phone-number-type-values
var (
phoneTypeLandline = "landline"
phoneTypeMobile = "mobile"
phoneTypeVoip = "voip"
)
type verifier struct {
client *twilio.RestClient
serviceSid string
}
// NewVerifier returns a new phone verifier backed by Twilio
func NewVerifier(accountSid, serviceSid, authToken string) phone.Verifier {
client := twilio.NewRestClientWithParams(twilio.ClientParams{
Username: accountSid,
Password: authToken,
})
return &verifier{
client: client,
serviceSid: serviceSid,
}
}
// SendCode implements phone.Verifier.SendCode
func (v *verifier) SendCode(ctx context.Context, phoneNumber string) (string, *phone.Metadata, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "SendCode")
defer tracer.End()
err := v.checkValidPhoneNumber(phoneNumber)
if err != nil {
tracer.OnError(err)
return "", nil, err
}
var appHash *string
userAgent, err := grpc_client.GetUserAgent(ctx)
if err == nil && userAgent.DeviceType == grpc_client.DeviceTypeAndroid {
appHash = &androidAppHash
}
resp, err := v.client.VerifyV2.CreateVerification(v.serviceSid, &verifyv2.CreateVerificationParams{
To: &phoneNumber,
Channel: &defaultChannel,
AppHash: appHash,
})
if err != nil {
err = checkInvalidToParameterError(err, phone.ErrInvalidNumber)
err = checkMaxSendAttemptsError(err, phone.ErrRateLimited)
err = checkFraudDetectionError(err, phone.ErrRateLimited)
err = checkUseCaseVettingError(err, phone.ErrRateLimited)
tracer.OnError(err)
return "", nil, err
}
if resp.Sid == nil {
err = errors.New("sid not provided")
tracer.OnError(err)
return "", nil, err
}
metadata := getMetadataFromLookupMap(phoneNumber, resp.Lookup)
return *resp.Sid, metadata, nil
}
// Check implements phone.Verifier.Check
func (v *verifier) Check(ctx context.Context, phoneNumber, code string) error {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "Check")
defer tracer.End()
if !phone.IsVerificationCode(code) {
err := phone.ErrInvalidVerificationCode
tracer.OnError(err)
return err
}
resp, err := v.client.VerifyV2.CreateVerificationCheck(v.serviceSid, &verifyv2.CreateVerificationCheckParams{
To: &phoneNumber,
Code: &code,
})
if err != nil {
err = check404Error(err, phone.ErrNoVerification)
err = checkMaxCheckAttemptsError(err, phone.ErrNoVerification)
tracer.OnError(err)
return err
}
if resp.Status == nil {
err = errors.New("status not provided")
tracer.OnError(err)
return err
}
switch strings.ToLower(*resp.Status) {
case statusApproved:
err = nil
case statusCanceled:
err = phone.ErrNoVerification
default:
err = phone.ErrInvalidVerificationCode
}
tracer.OnError(err)
return err
}
// Cancel implements phone.Verifier.Cancel
func (v *verifier) Cancel(ctx context.Context, id string) error {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "Cancel")
defer tracer.End()
_, err := v.client.VerifyV2.UpdateVerification(v.serviceSid, id, &verifyv2.UpdateVerificationParams{
Status: &statusCanceled,
})
if err != nil {
err := check404Error(err, nil)
tracer.OnError(err)
return err
}
return nil
}
// IsVerificationActive implements phone.Verifier.IsVerificationActive
func (v *verifier) IsVerificationActive(ctx context.Context, id string) (bool, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "IsVerificationActive")
defer tracer.End()
resp, err := v.client.VerifyV2.FetchVerification(v.serviceSid, id)
if is404Error(err) {
return false, nil
} else if err != nil {
tracer.OnError(err)
return false, err
}
if resp.Status == nil {
err = errors.New("status is not provided")
tracer.OnError(err)
return false, err
}
return *resp.Status == statusPending, nil
}
// IsValidPhoneNumber implements phone.Verifier.IsValidPhoneNumber
func (v *verifier) IsValidPhoneNumber(ctx context.Context, phoneNumber string) (bool, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "IsValidPhoneNumber")
defer tracer.End()
err := v.checkValidPhoneNumber(phoneNumber)
if err == phone.ErrInvalidNumber || err == phone.ErrUnsupportedPhoneType {
return false, nil
} else if err != nil {
tracer.OnError(err)
return false, err
}
return true, nil
}
// todo: Use the new V2 API when we get access. It has some cool features like
// SIM swap detection.
func (v *verifier) checkValidPhoneNumber(phoneNumber string) error {
if !phone.IsE164Format(phoneNumber) {
return errors.New("phone number is not in E.164 format")
}
resp, err := v.client.LookupsV1.FetchPhoneNumber(phoneNumber, &lookupsv1.FetchPhoneNumberParams{
Type: &[]string{carrierMapKey},
})
if err != nil {
return check404Error(err, phone.ErrInvalidNumber)
}
if resp.Carrier == nil {
return nil
}
carrierInfoMap, ok := (*resp.Carrier).(map[string]interface{})
if !ok {
return nil
}
metadata := getMetadataFromCarrierInfoMap(phoneNumber, carrierInfoMap)
// This is not guaranteed to be available
if metadata.Type == nil {
return nil
}
// todo: It's safest to block all virtual numbers, but confirm with Twilio
// if there are cases where we'd want this to go through.
if *metadata.Type != phone.TypeMobile {
return phone.ErrUnsupportedPhoneType
}
return nil
}
func check404Error(inError, outError error) error {
if is404Error(inError) {
return outError
}
return inError
}
func is404Error(err error) bool {
twilioError, ok := err.(*client.TwilioRestError)
if !ok {
return false
}
return twilioError.Status == http.StatusNotFound
}
func checkMaxSendAttemptsError(inError, outError error) error {
twilioError, ok := inError.(*client.TwilioRestError)
if !ok {
return inError
}
if twilioError.Status != http.StatusTooManyRequests {
return inError
}
if twilioError.Code == maxSendAttemptsCode {
return outError
}
return inError
}
func checkMaxCheckAttemptsError(inError, outError error) error {
twilioError, ok := inError.(*client.TwilioRestError)
if !ok {
return inError
}
if twilioError.Status != http.StatusTooManyRequests {
return inError
}
if twilioError.Code == maxCheckAttemptsCode {
return outError
}
return inError
}
func checkInvalidToParameterError(inError, outError error) error {
twilioError, ok := inError.(*client.TwilioRestError)
if !ok {
return inError
}
if twilioError.Status != http.StatusBadRequest {
return inError
}
if twilioError.Code != invalidParameterCode {
return inError
}
expectedMessage := strings.ToLower("Invalid parameter `To`")
if strings.Contains(strings.ToLower(twilioError.Message), expectedMessage) {
return outError
}
return inError
}
func checkUseCaseVettingError(inError, outError error) error {
twilioError, ok := inError.(*client.TwilioRestError)
if !ok {
return inError
}
if twilioError.Code == useCaseVettingCode {
return outError
}
return inError
}
func checkFraudDetectionError(inError, outError error) error {
twilioError, ok := inError.(*client.TwilioRestError)
if !ok {
return inError
}
if twilioError.Code == fraudDetectionCode {
return outError
}
return inError
}
// I'm soo sorry about the below code. The Twilio client is ridiculous with typing.
// Note: Can't use "ok" variable because it appears to actually set a key with a nil
// value
func getMetadataFromLookupMap(phoneNumber string, lookup *interface{}) *phone.Metadata {
metadata := &phone.Metadata{
PhoneNumber: phoneNumber,
}
if lookup == nil {
return metadata
}
carrierInfo := (*lookup).(map[string]interface{})[carrierMapKey]
if carrierInfo == nil {
return metadata
}
carrierInfoMap, ok := carrierInfo.(map[string]interface{})
if !ok {
return metadata
}
return getMetadataFromCarrierInfoMap(phoneNumber, carrierInfoMap)
}
func getMetadataFromCarrierInfoMap(phoneNumber string, carrierInfo map[string]interface{}) *phone.Metadata {
metadata := &phone.Metadata{
PhoneNumber: phoneNumber,
}
if carrierInfo == nil {
return metadata
}
phoneType := carrierInfo[phoneTypeMapKey]
if phoneType != nil {
strValue, ok := phoneType.(string)
if ok {
metadata.SetType(getPhoneTypeFromString(strValue))
}
}
mobileCountryCode := carrierInfo[mobileCountryCodeMapKey]
if mobileCountryCode != nil {
strValue, ok := mobileCountryCode.(string)
if ok {
intValue, err := strconv.Atoi(strValue)
if err == nil {
metadata.SetMobileCountryCode(intValue)
}
}
}
mobileNetworkCode := carrierInfo[mobileNetworkCodeMapKey]
if mobileNetworkCode != nil {
strValue, ok := mobileNetworkCode.(string)
if ok {
intValue, err := strconv.Atoi(strValue)
if err == nil {
metadata.SetMobileNetworkCode(intValue)
}
}
}
return metadata
}
func getPhoneTypeFromString(value string) phone.Type {
switch value {
case phoneTypeMobile:
return phone.TypeMobile
case phoneTypeVoip:
return phone.TypeVoip
case phoneTypeLandline:
return phone.TypeLandline
default:
return phone.TypeUnknown
}
}