forked from zemirco/simple-recaptcha
/
index.js
57 lines (44 loc) · 1.2 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
var https = require('https');
var options = {
host: 'www.google.com',
port: 443,
path: '/recaptcha/api/siteverify?',
method: 'GET',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
module.exports = function(privateKey, remoteIP, response, cb) {
var error = null;
if (!privateKey) {
error = 'Private key is required';
} else if (!remoteIP) {
error = 'Remote IP is required';
} else if (!response) {
error = 'Response is required';
}
if (error) {
return cb(new Error(error));
}
options.path = '/recaptcha/api/siteverify?secret=' + privateKey +
'&remoteip=' + remoteIP +
'&response=' + response;
var request = https.request(options, function(response) {
var body = '';
response.on('error', function(err) {
return cb(new Error(err));
});
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var data = JSON.parse( body );
if (!data.success) return cb(new Error(data['error-codes']));
cb(null);
});
});
request.on('error', function(err) {
return cb(new Error(err));
});
request.end();
};