-
Notifications
You must be signed in to change notification settings - Fork 13
/
twilio_gateway.go
148 lines (105 loc) · 3.48 KB
/
twilio_gateway.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
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) Call(phone *Phone, _ string, customMessage string) (string, string, error) {
createVerificationParams := &openapi.CreateVerificationParams{}
createVerificationParams.SetChannel("call")
createVerificationParams.SetTo(phone.Number)
if customMessage != "" {
createVerificationParams.SetCustomMessage(customMessage)
}
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
}