public
Created

A Recaptcha module I wrote for my Node.js app. It depends on https://github.com/keverw/HTTPRequest

  • Download Gist
gistfile1.js
JavaScript
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
var recaptcha_settings = {
api_server: 'http://www.google.com/recaptcha/api',
api_secure_server: 'https://www.google.com/recaptcha/api',
verify_server: 'www.google.com',
verify_path: '/recaptcha/api/verify'
}
 
module.exports = {
get_html: function (pubkey, callback, error, use_ssl) //returns HTML
{
 
if (pubkey == null || pubkey == '')
{
throw new Error("To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create");
}
 
if (use_ssl)
{
var server = recaptcha_settings.api_secure_server;
}
else
{
var server = recaptcha_settings.api_server;
}
 
var errorpart = '';
if (error)
{
errorpart = "&error=".error;
}
 
callback('<script type="text/javascript" src="' + server + '/challenge?k=' + pubkey + errorpart + '"></script>\
<noscript>\
<iframe src="' + server + '/noscript?k=' + pubkey + errorpart + '" height="300" width="500" frameborder="0"></iframe><br/>\
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>\
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>\
</noscript>');
},
validate: function (privkey, req, challenge, response, callback) //validates answer
{
if (privkey == null || privkey == '')
{
throw new Error('To use reCAPTCHA you must get an API key from https://www.google.com/recaptcha/admin/create');
}
 
var remoteip = (req.connection.remoteAddress ? req.connection.remoteAddress : req.remoteAddress);
 
console.log('challenge: ' + challenge);
 
//discard spam submissions
if (challenge == null || challenge.length == 0 || response == null || response.length == 0)
{
callback(false, 'incorrect-captcha-sol');
}
else //verify with server
{
var HTTPRequest = require('HTTPRequest');
HTTPRequest.post("https://" + recaptcha_settings.verify_server + recaptcha_settings.verify_path, {
privatekey: privkey,
remoteip: remoteip,
challenge: challenge,
response: response
}, function (status, headers, content)
{
if (status == 200)
{
var lines = content.split('\n');
if (lines.length >= 2)
{
if (lines[0] == 'true')
{
callback(true, lines[1]);
}
else
{
callback(false, lines[1]);
}
}
}
else
{
callback(false, 'HTTP ERROR: ' + status);
}
}, {
USERAGENT: 'Node.js reCAPTCHA'
});
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.