-
Notifications
You must be signed in to change notification settings - Fork 16
/
api.go
306 lines (245 loc) · 8.28 KB
/
api.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
package authy
import (
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/gojektech/heimdall"
"github.com/gojektech/heimdall/httpclient"
)
var (
// Logger is the default logger of this package. You can override it with your own.
Logger = log.New(os.Stderr, "[authy] ", log.LstdFlags)
_Dialer = &net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}
// DefaultTransport is the default transport struct for the HTTP client
DefaultTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: _Dialer.DialContext,
MaxIdleConns: 128,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
ExpectContinueTimeout: 3 * time.Second,
}
)
const (
longPollingDelay = 2000 * time.Millisecond
)
const (
// SMS indicates the message will be delivered via SMS
SMS = "sms"
// Voice indicates the message will be delivered via phone call
Voice = "call"
)
// Details for OneTouch transaction
type Details map[string]string
// Authy contains credentials to connect to the Authy's API
type Authy struct {
APIKey string
BaseURL string
Client heimdall.Client
}
// NewAuthyAPI returns an instance of Authy pointing to production.
func NewAuthyAPI(apiKey string) *Authy {
apiURL := "https://api.authy.com"
initalTimeout := 2 * time.Millisecond
maxTimeout := 1000 * time.Millisecond
exponentFactor := 2.0
maximumJitterInterval := 2 * time.Millisecond
backoff := heimdall.NewExponentialBackoff(initalTimeout, maxTimeout, exponentFactor, maximumJitterInterval)
client := httpclient.NewClient(
httpclient.WithHTTPTimeout(1*time.Second),
httpclient.WithRetrier(heimdall.NewRetrier(backoff)),
httpclient.WithRetryCount(4),
httpclient.WithHTTPClient(&http.Client{
Transport: DefaultTransport,
}),
)
return &Authy{
APIKey: apiKey,
BaseURL: apiURL,
Client: client,
}
}
// RegisterUser register a new user given an email and phone number.
func (authy *Authy) RegisterUser(email string, countryCode int, phoneNumber string, params url.Values) (*User, error) {
Logger.Println("Creating Authy user with", email, ",", phoneNumber, "and", countryCode)
path := "/protected/json/users/new"
params.Set("user[cellphone]", phoneNumber)
params.Set("user[country_code]", strconv.Itoa(countryCode))
params.Set("user[email]", email)
response, err := authy.DoRequest("POST", path, params)
if err != nil {
return nil, err
}
userResponse, err := NewUser(response)
return userResponse, err
}
// UserStatus returns a set of data about a user.
func (authy *Authy) UserStatus(id string, params url.Values) (*UserStatus, error) {
Logger.Println("Finding Authy user with id", id)
path := fmt.Sprintf("/protected/json/users/%s/status", id)
response, err := authy.DoRequest("GET", path, params)
if err != nil {
return nil, err
}
statusResponse, err := NewUserStatus(response)
return statusResponse, err
}
// VerifyToken verifies the given token
func (authy *Authy) VerifyToken(userID string, token string, params url.Values) (*TokenVerification, error) {
path := "/protected/json/verify/" + url.QueryEscape(token) + "/" + url.QueryEscape(userID)
response, err := authy.DoRequest("GET", path, params)
if err != nil {
Logger.Println("Error while contacting the API:", err)
return nil, err
}
defer closeResponseBody(response)
tokenVerification, err := NewTokenVerification(response)
return tokenVerification, err
}
// RequestSMS requests a SMS for the given userID
func (authy *Authy) RequestSMS(userID string, params url.Values) (*SMSRequest, error) {
path := "/protected/json/sms/" + url.QueryEscape(userID)
response, err := authy.DoRequest("GET", path, params)
if err != nil {
return nil, err
}
defer closeResponseBody(response)
smsVerification, err := NewSMSRequest(response)
return smsVerification, err
}
// RequestPhoneCall requests a phone call for the given user
func (authy *Authy) RequestPhoneCall(userID string, params url.Values) (*PhoneCallRequest, error) {
path := "/protected/json/call/" + url.QueryEscape(userID)
response, err := authy.DoRequest("GET", path, params)
if err != nil {
return nil, err
}
defer closeResponseBody(response)
smsVerification, err := NewPhoneCallRequest(response)
return smsVerification, err
}
// SendApprovalRequest sends a OneTouch's approval request to the given user.
func (authy *Authy) SendApprovalRequest(userID string, message string, details Details, params url.Values) (*ApprovalRequest, error) {
addParamsForOneTouch(params, message, details)
path := fmt.Sprintf(`/onetouch/json/users/%s/approval_requests`, url.QueryEscape(userID))
response, err := authy.DoRequest("POST", path, params)
if err != nil {
return nil, err
}
defer closeResponseBody(response)
return NewApprovalRequest(response)
}
// FindApprovalRequest finds an approval request given its uuid.
func (authy *Authy) FindApprovalRequest(uuid string, params url.Values) (*ApprovalRequest, error) {
path := fmt.Sprintf("/onetouch/json/approval_requests/%s", uuid)
response, err := authy.DoRequest("GET", path, params)
if err != nil {
return nil, err
}
defer closeResponseBody(response)
approvalRequest, err := NewApprovalRequest(response)
if err != nil {
return nil, err
}
approvalRequest.UUID = uuid
return approvalRequest, nil
}
// WaitForApprovalRequest waits until the status of an approval request has changed or times out.
func (authy *Authy) WaitForApprovalRequest(uuid string, maxDuration time.Duration, params url.Values) (OneTouchStatus, error) {
for maxDuration > 0 {
request, err := authy.FindApprovalRequest(uuid, url.Values{})
if err != nil {
return OneTouchStatusPending, err
}
if request.Status != OneTouchStatusPending {
return request.Status, nil
}
maxDuration -= longPollingDelay
time.Sleep(longPollingDelay)
}
return OneTouchStatusExpired, nil
}
// StartPhoneVerification starts the phone verification process.
func (authy *Authy) StartPhoneVerification(countryCode int, phoneNumber string, via string, params url.Values) (*PhoneVerificationStart, error) {
params.Set("country_code", strconv.Itoa(countryCode))
params.Set("phone_number", phoneNumber)
params.Set("via", via)
path := fmt.Sprintf("/protected/json/phones/verification/start")
response, err := authy.DoRequest("POST", path, params)
if err != nil {
return nil, err
}
defer closeResponseBody(response)
return NewPhoneVerificationStart(response)
}
// CheckPhoneVerification checks the given verification code.
func (authy *Authy) CheckPhoneVerification(countryCode int, phoneNumber string, verificationCode string, params url.Values) (*PhoneVerificationCheck, error) {
params.Set("country_code", strconv.Itoa(countryCode))
params.Set("phone_number", phoneNumber)
params.Set("verification_code", verificationCode)
path := fmt.Sprintf("/protected/json/phones/verification/check")
response, err := authy.DoRequest("GET", path, params)
if err != nil {
return nil, err
}
defer closeResponseBody(response)
return NewPhoneVerificationCheck(response)
}
// DoRequest performs a HTTP request to the Authy API
func (authy *Authy) DoRequest(method string, path string, params url.Values) (*http.Response, error) {
apiURL := authy.buildURL(path)
// Set api_key to all requests.
params.Set("api_key", authy.APIKey)
var bodyReader io.Reader
switch method {
case "POST":
{
encodedParams := params.Encode()
bodyReader = strings.NewReader(encodedParams)
}
case "GET":
{
apiURL += "?" + params.Encode()
}
}
request, err := http.NewRequest(method, apiURL, bodyReader)
if method == "POST" {
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
if err != nil {
Logger.Println("Error creating HTTP request:", err)
return nil, err
}
response, err := authy.Client.Do(request)
return response, err
}
func (authy *Authy) buildURL(path string) string {
if path[0] != '/' {
path = "/" + path
}
url := authy.BaseURL + path
return url
}
func closeResponseBody(response *http.Response) {
err := response.Body.Close()
if err != nil {
Logger.Println("Error closing response body:", err)
}
}
func addParamsForOneTouch(params url.Values, message string, details map[string]string) url.Values {
params.Set("message", message)
for key, value := range details {
params.Set(fmt.Sprintf("details[%s]", key), value)
}
return params
}