Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recaptcha Version 2.0 & Nodejs callback convention #14

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 24 additions & 35 deletions lib/recaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
* Module dependencies.
*/

var http = require('http'),
var https = require('https'),
querystring = require('querystring');

/**
* Constants.
*/

var API_HOST = 'www.google.com',
API_END_POINT = '/recaptcha/api/verify',
SCRIPT_SRC = API_HOST + '/recaptcha/api/challenge',
NOSCRIPT_SRC = API_HOST + '/recaptcha/api/noscript';
var API_HOST = 'www.google.com',
API_END_POINT = '/recaptcha/api/siteverify'

/**
* Initialize Recaptcha with given `public_key`, `private_key` and optionally
Expand Down Expand Up @@ -65,19 +63,19 @@ var Recaptcha = exports.Recaptcha = function Recaptcha(public_key, private_key,
*/

Recaptcha.prototype.toHTML = function() {
var query_string = 'k=' + this.public_key;
if (this.error_code) {
query_string += '&error=' + this.error_code;
}

var script_src = (this.is_secure ? "https://" : "http://") + SCRIPT_SRC + '?' + query_string;
var noscript_src = (this.is_secure ? "https://" : "http://") + NOSCRIPT_SRC + '?' + query_string;

return '<script type="text/javascript" src="' + script_src + '"></script>' +
'<noscript><iframe src="' + noscript_src + '" 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>';
return "<script src='https://www.google.com/recaptcha/api.js' async defer></script>" +
"<div class='g-recaptcha' data-sitekey='" + this.public_key + "'></div>" +
"<noscript><div style='width: 302px; height: 352px;'>" +
"<div style='width: 302px; height: 352px; position: relative;'>" +
"<div style='width: 302px; height: 352px; position: absolute;'>" +
"<iframe src='https://www.google.com/recaptcha/api/fallback?k=" + this.public_key + "'" +
"frameborder='0' scrolling='no'" +
"style='width: 302px; height:352px; border-style: none;'></iframe></div>" +
"<div style='width: 250px; height: 80px; position: absolute; border-style: none;" +
"bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;'>" +
"<textarea id='g-recaptcha-response' name='g-recaptcha-response'" +
"class='g-recaptcha-response' style='width: 250px; height: 80px; border: 1px solid #c1c1c1; " +
"margin: 0px; padding: 0px; resize: none;' value=''></textarea></div></div></div></noscript>";
};

/**
Expand Down Expand Up @@ -106,58 +104,49 @@ Recaptcha.prototype.verify = function(callback) {
// See if we can declare this invalid without even contacting Recaptcha.
if (typeof(this.data) === 'undefined') {
this.error_code = 'verify-params-incorrect';
return callback(false, 'verify-params-incorrect');
return callback('verify-params-incorrect', false);
}
if (!('remoteip' in this.data &&
'challenge' in this.data &&
'response' in this.data))
{
this.error_code = 'verify-params-incorrect';
return callback(false, 'verify-params-incorrect');
}
if (this.data.response === '') {
this.error_code = 'incorrect-captcha-sol';
return callback(false, 'incorrect-captcha-sol');
return callback('incorrect-captcha-sol', false);
}

// Add the private_key to the request.
this.data['privatekey'] = this.private_key;
this.data['secret'] = this.private_key;
var data_qs = querystring.stringify(this.data);

var req_options = {
host: API_HOST,
path: API_END_POINT,
port: 80,
port: 443,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': data_qs.length
}
};

var request = http.request(req_options, function(response) {
var request = https.request(req_options, function(response) {
var body = '';

response.on('error', function(err) {
self.error_code = 'recaptcha-not-reachable';
callback(false, 'recaptcha-not-reachable');
callback('recaptcha-not-reachable', false);
});

response.on('data', function(chunk) {
body += chunk;
});

response.on('end', function() {
var success, error_code, parts;

parts = body.split('\n');
success = parts[0];
error_code = parts[1];

if (success !== 'true') {
self.error_code = error_code;
}
return callback(success === 'true', error_code);
var result = JSON.parse(body);
return callback(result["error-codes"], result.success);
});
});
request.write(data_qs, 'utf8');
Expand Down