diff --git a/lib/recaptcha.js b/lib/recaptcha.js index 17f2555..75b96d3 100644 --- a/lib/recaptcha.js +++ b/lib/recaptcha.js @@ -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 @@ -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 '' + - ''; + return "" + + "
" + + ""; }; /** @@ -106,10 +104,9 @@ 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'; @@ -117,17 +114,17 @@ Recaptcha.prototype.verify = function(callback) { } 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', @@ -135,12 +132,12 @@ Recaptcha.prototype.verify = function(callback) { } }; - 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) { @@ -148,16 +145,8 @@ Recaptcha.prototype.verify = function(callback) { }); 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');