forked from evilpacket/node-authy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
135 lines (122 loc) · 3.06 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
var request = require('request');
module.exports = function (api_key, api_url) {
return new Authy(api_key, api_url);
};
function Authy(apiKey, api_url) {
this.apiKey = apiKey;
this.apiURL = api_url || "https://api.authy.com";
}
Authy.prototype.register_user = function (email, cellphone, country_code, callback) {
var country = "1";
if (arguments.length > 3) {
country = country_code;
} else {
callback = country_code;
}
request.post({
url: this.apiURL + "/protected/json/users/new",
form: {
"user[email]": email,
"user[cellphone]": cellphone,
"user[country_code]": country
},
qs: {
api_key: this.apiKey
}
}, function (err, res, body) {
if (!err) {
if(res.statusCode === 200) {
callback(null, JSON.parse(body));
} else {
callback(body);
}
} else {
throw new Error(err);
}
});
};
Authy.prototype.delete_user = function (id, callback) {
request.post({
url: this.apiURL + "/protected/json/users/delete/" + id,
qs: {
api_key: this.apiKey
}
}, function (err, res, body) {
if (!err) {
if(res.statusCode === 200) {
callback(null, body);
} else {
callback(body);
}
} else {
throw new Error(err);
}
});
};
Authy.prototype.verify = function (id, token, force, callback) {
var qs = {
api_key: this.apiKey
};
if (arguments.length > 3) {
qs.force = force;
} else {
callback = force;
}
request.get({
url: this.apiURL + "/protected/json/verify/" + token + "/" + id,
qs: qs
}, function (err, res, body) {
if (!err) {
if (res.statusCode === 200) {
callback(null, toJSON(body));
} else {
callback(toJSON(body));
}
} else {
throw new Error(err);
}
});
};
Authy.prototype.request_sms = function (id, force, callback) {
var qs = {
api_key: this.apiKey
};
if (arguments.length > 2) {
qs.force = force;
} else {
callback = force;
}
request.get({
url: this.apiURL + "/protected/json/sms/" + id,
qs: qs
}, function (err, res, body) {
if (!err) {
if (res.statusCode === 200) {
callback(null, toJSON(body));
} else {
callback(body);
}
} else {
throw new Error(err);
}
});
};
// Utility functions. Should live somebody else probably.
function isJSON(data) {
if (typeof data === 'object') return true;
try {
data = JSON.parse(data);
return true;
} catch (e) {
return false;
}
}
function toJSON(data) {
if (typeof data === 'object') return data;
try {
data = JSON.parse(data);
return data;
} catch (e) {
return data;
}
}