-
Notifications
You must be signed in to change notification settings - Fork 14
/
sinch_gateway.go
162 lines (135 loc) · 3.51 KB
/
sinch_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package otp
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/coretrix/hitrix/pkg/helper"
)
const SMSOTPProviderSinch = "Sinch"
type Sinch struct {
AppID string
AppSecret string
VerificationURL string
}
func NewSinchSMSOTPProvider(appID, appSecret, verificationURL string) *Sinch {
return &Sinch{
AppID: appID,
AppSecret: appSecret,
VerificationURL: verificationURL,
}
}
func (s *Sinch) GetName() string {
return SMSOTPProviderSinch
}
func (s *Sinch) GetCode() string {
return ""
}
func (s *Sinch) GetPhonePrefixes() []string {
return nil
}
func (s *Sinch) SendOTP(phone *Phone, _ string) (string, string, error) {
body := &struct {
Identity *struct {
Type string `json:"type"`
Endpoint string `json:"endpoint"`
} `json:"identity"`
Method string `json:"method"`
}{
Identity: &struct {
Type string `json:"type"`
Endpoint string `json:"endpoint"`
}{
Type: "number",
Endpoint: phone.Number,
},
Method: "sms",
}
request, err := s.toJSON(body)
if err != nil {
return request, "", err
}
headers := s.getSinchHeaders()
responseBody, _, code, err := helper.Call(
context.Background(),
"POST",
s.VerificationURL,
headers,
time.Duration(5)*time.Second,
body,
nil)
if err != nil {
return request, string(responseBody), err
}
if code != http.StatusOK {
return request, string(responseBody), fmt.Errorf("expected status code 200, but got %v", code)
}
return request, string(responseBody), nil
}
func (s *Sinch) Call(_ *Phone, _ string, _ string) (string, string, error) {
// not implemented
return "", "", nil
}
func (s *Sinch) VerifyOTP(phone *Phone, code, _ string) (string, string, bool, bool, error) {
body := &struct {
Method string `json:"method"`
SMS *struct {
Code string `json:"code"`
} `json:"sms"`
}{
Method: "sms",
SMS: &struct {
Code string `json:"code"`
}{Code: code},
}
request, err := s.toJSON(body)
if err != nil {
return request, "", false, false, err
}
headers := s.getSinchHeaders()
responseBody, _, respCode, err := helper.Call(
context.Background(),
"PUT",
s.VerificationURL+"/number/"+phone.Number,
headers,
time.Duration(5)*time.Second,
body,
nil)
if err != nil {
return request, string(responseBody), false, false, err
}
if respCode != http.StatusOK && strings.Contains(string(responseBody), "Invalid identity or code") {
return request, string(responseBody), false, false, nil
} else if respCode != http.StatusOK {
return request, string(responseBody), false, false, fmt.Errorf("expected status code 200, but got %v", respCode)
}
respStruct := &struct {
ID string `json:"id"`
Method string `json:"method"`
Status string `json:"status"`
Reason string `json:"reason"`
Reference string `json:"reference"`
Source string `json:"source"`
}{}
if err := json.Unmarshal(responseBody, respStruct); err != nil {
return request, string(responseBody), false, false, err
}
return request, string(responseBody), true, respStruct.Status == "SUCCESSFUL", nil
}
func (s *Sinch) getSinchHeaders() map[string]string {
base64Credentials := base64.StdEncoding.EncodeToString([]byte(s.AppID + ":" + s.AppSecret))
return map[string]string{
"Content-Type": "application/json",
"Authorization": "Basic " + base64Credentials,
}
}
func (s *Sinch) toJSON(data interface{}) (string, error) {
dataJSON, err := json.Marshal(data)
if err != nil {
return "", err
}
return string(dataJSON), nil
}