-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (48 loc) · 1.26 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
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) {
if( arguments.length < 4 && typeof(response) === 'function') {
cb = response;
response = remoteIP;
remoteIP = undefined;
}
var error = null;
if (!privateKey) {
error = 'Private key is required';
} else if (!response) {
error = 'Response is required';
}
if (error) {
return cb(new Error(error));
}
options.path = '/recaptcha/api/siteverify?secret=' + privateKey + '&response=' + response;
if (remoteIP) {
options.path += '&remoteip=' + remoteIP;
}
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();
};