-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
226 lines (204 loc) · 6.9 KB
/
index.js
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
'use strict';
var request = require('request');
module.exports = function (options) {
var digits_consumer_key = options.digitsConsumerKey;
var digits_host = options.digitsHost;
/**
* GET a web session needed for each api call
*/
var getWebSession = function () {
return new Promise(function (resolve, reject) {
request.get({
url: 'https://www.digits.com/embed?consumer_key=' + digits_consumer_key + '&host=' + digits_host
},
function (error, response, body) {
if (error) {
return reject('HTTP ERROR : Unable to parse Digits cookie response.');
}
try {
//get session token & parse html for the authenticity_token
var cookie = response.headers['set-cookie'][1].split(';')[0];
var authenticityToken = body.split('name="authenticity_token" value="')[1].slice(0,40);
} catch (error) {
return reject('Unable to parse Digits cookies or authenticityToken in html.');
}
resolve({
cookie : cookie,
authenticityToken : authenticityToken
});
}
)
})
};
/**
Send verification code to device via sms or voicecall
Get registration token to challenge the code later
Usage example :
sendVerificationCode({
phoneNumber: '0648446907',
countryCode: 'FR',
headers: {"user-agent": ...., "accept-language": .....},
method: "sms" // sms or "voicecall"
}).then(function (registrationToken) {
console.log(registrationToken);
}).then(null, function (error) {
//error
});
*/
var sendVerificationCode = function (options) {
options = options || {};
if (!options.phoneNumber) {
return Promise.reject(new Error("Missing options.phoneNumber field"));
} else if (!options.countryCode) {
return Promise.reject(new Error("Missing options.countryCode field"));
}
return getWebSession().then(function (session) {
return new Promise(function (resolve, reject) {
request.post({
har: {
"method": "POST",
"url": "https://www.digits.com/sdk/login",
"headers": [
{
"name": "cookie",
"value": session.cookie
},
{
"name": "referer",
"value": "https://www.digits.com/embed?consumer_key=" + digits_consumer_key + "&host=" + digits_host
},
{
"name": "accept-language",
"value": options.headers && options.headers["accept-language"] ? options.headers["accept-language"] : 'en-US'
},
{
"name": "user-agent",
"value": options.headers && options.headers["user-agent"] ? options.headers["user-agent"] : 'Mozilla/5.0'
},
],
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"params": [
{
"name": "authenticity_token",
"value": session.authenticityToken
},
{
"name": "verification_type",
"value": options.method ? options.method : "sms"
},
{
"name": "x_auth_country_code",
"value": options.countryCode
},
{
"name": "x_auth_phone_number",
"value": options.phoneNumber
}
]
}
}
}, function (error, response, body) {
if (error) {
return reject('HTTP ERROR : Unable to parse Digits login response.');
}
try {
var jsonBody = JSON.parse(body);
} catch (error) {
return reject('Unable to parse Digits response.');
}
if (jsonBody.errors) {
return reject(jsonBody.errors);
}
var token = new Buffer(JSON.stringify({
loginVerificationRequestId: jsonBody.login_verification_request_id,
loginVerificationUserId: jsonBody.login_verification_user_id,
phoneNumber: jsonBody.phone_number
})).toString('base64')
resolve(token);
})
})
})
};
/**
Test code validation
Usage example :
verify({
registrationToken: 'abcdefdsjfbdshfsdfsdhfjbsdhfd...',
code: '020531',
}).then(function (results) {
console.log(results);
}).then(null, function (e) {
//error
});
*/
var verify = function (options) {
options = options || {};
var token = JSON.parse(new Buffer(options.registrationToken, 'base64').toString('ascii'));
return getWebSession().then(function (session) {
return new Promise(function (resolve, reject) {
request.post({
har: {
"method": "POST",
"url": "https://www.digits.com/sdk/challenge",
"headers": [
{
"name": "cookie",
"value": session.cookie
},
{
"name": "referer",
"value": "https://www.digits.com/embed/challenge?consumer_key=" + digits_consumer_key + "&host=" + digits_host
}
],
"postData": {
"mimeType": "application/x-www-form-urlencoded",
"params": [
{
name:"authenticity_token",
value: session.authenticityToken
},
{
name:"remember_me",
value: "on"
},
{
name:"phone_number",
//value: "0648446907"
value: token.phoneNumber
},
{
name:"login_verification_user_id",
value: token.loginVerificationUserId
},
{
name: "login_verification_challenge_response",
"value": options.code
},
{
"name": "login_verification_request_id",
"value": token.loginVerificationRequestId
}
]
}
}
}, function (error, response, body) {
if (error) {
return reject(error);
}
try {
var jsonBody = JSON.parse(body);
} catch (error) {
return reject('Unable to parse Digits response.');
}
if (jsonBody.errors) {
resolve({success: false, phone: token.phoneNumber, errors: jsonBody.errors});
}
resolve({success: !!jsonBody['X-Verify-Credentials-Authorization'], phone: token.phoneNumber});
})
})
})
};
this.sendVerificationCode = sendVerificationCode;
this.verifyCode = verify;
}