-
Notifications
You must be signed in to change notification settings - Fork 13
/
sms_gateway_twilio.go
115 lines (82 loc) · 2.66 KB
/
sms_gateway_twilio.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
package otp
import (
"encoding/json"
"github.com/twilio/twilio-go"
"github.com/twilio/twilio-go/client"
openapi "github.com/twilio/twilio-go/rest/verify/v2"
)
const SMSOTPProviderTwilio = "Twilio"
type Twilio struct {
Client *twilio.RestClient
VerificationSID string
}
func NewTwilioSMSOTPProvider(accountSid, authToken, verificationSid string) *Twilio {
return &Twilio{
Client: twilio.NewRestClientWithParams(twilio.RestClientParams{
Username: accountSid,
Password: authToken,
}),
VerificationSID: verificationSid,
}
}
func (t *Twilio) GetName() string {
return SMSOTPProviderTwilio
}
func (t *Twilio) GetCode() string {
return ""
}
func (t *Twilio) GetPhonePrefixes() []string {
return nil
}
func (t *Twilio) SendOTP(phone *Phone, _ string) (string, string, error) {
createVerificationParams := &openapi.CreateVerificationParams{}
createVerificationParams.SetChannel("sms")
createVerificationParams.SetTo(phone.Number)
request, jsonError := t.toJSON(createVerificationParams)
if jsonError != nil {
return "", "", jsonError
}
verifyV2Verification, err := t.Client.VerifyV2.CreateVerification(t.VerificationSID, createVerificationParams)
response, jsonError := t.toJSON(verifyV2Verification)
if jsonError != nil {
return request, "", jsonError
}
if err != nil {
return request, response, err
}
//TODO check error codes
return request, response, nil
}
func (t *Twilio) VerifyOTP(phone *Phone, code, _ string) (string, string, bool, bool, error) {
createVerificationCheckParams := &openapi.CreateVerificationCheckParams{}
createVerificationCheckParams.SetTo(phone.Number)
createVerificationCheckParams.SetCode(code)
request, jsonError := t.toJSON(createVerificationCheckParams)
if jsonError != nil {
return "", "", false, false, jsonError
}
verifyV2VerificationCheck, err := t.Client.VerifyV2.CreateVerificationCheck(t.VerificationSID, createVerificationCheckParams)
response, jsonError := t.toJSON(verifyV2VerificationCheck)
if jsonError != nil {
return request, "", false, false, jsonError
}
if err != nil {
twilioRestError, ok := err.(*client.TwilioRestError)
if !ok {
return request, response, false, false, err
}
if twilioRestError.Status == 404 && twilioRestError.Code == 20404 {
//already confirmed or expired session - invalid request
return request, twilioRestError.Error(), false, false, nil
}
return request, twilioRestError.Error(), false, false, err
}
return request, response, true, *verifyV2VerificationCheck.Valid, nil
}
func (t *Twilio) toJSON(data interface{}) (string, error) {
dataJSON, err := json.Marshal(data)
if err != nil {
return "", err
}
return string(dataJSON), nil
}