recaptcha2
Advanced tools
Comparing version 1.0.8 to 1.1.2
143
index.js
@@ -1,96 +0,93 @@ | ||
// Generated by CoffeeScript 1.9.3 | ||
(function() { | ||
var Promise, Recaptcha2, request; | ||
var DEFAULT_CONFIG, DEFAULT_REQUEST_OPTIONS, ERRORS, GOOGLE_CAPTCHA_ENDPOINT, Recaptcha2, request; | ||
Promise = require('bluebird'); | ||
request = require('request'); | ||
ERRORS = { | ||
'request-error': 'Api request failed.', | ||
'missing-input-secret': 'The secret parameter is missing.', | ||
'invalid-input-secret': 'The secret parameter is invalid or malformed.', | ||
'missing-input-response': 'The response parameter is missing.', | ||
'invalid-input-response': 'The response parameter is invalid or malformed.' | ||
}; | ||
GOOGLE_CAPTCHA_ENDPOINT = "https://www.google.com/recaptcha/api/siteverify"; | ||
DEFAULT_CONFIG = { | ||
siteKey: null, | ||
secretKey: null, | ||
ssl: true | ||
}; | ||
DEFAULT_REQUEST_OPTIONS = { | ||
uri: GOOGLE_CAPTCHA_ENDPOINT, | ||
method: "POST", | ||
json: true, | ||
form: {} | ||
}; | ||
Recaptcha2 = (function() { | ||
var errorList; | ||
Recaptcha2.prototype.apiEndpoint = GOOGLE_CAPTCHA_ENDPOINT; | ||
errorList = { | ||
'request-error': 'Api request failed .', | ||
'json-parse': 'Response JSON parse failed.', | ||
'missing-input-secret': 'The secret parameter is missing.', | ||
'invalid-input-secret': 'The secret parameter is invalid or malformed.', | ||
'missing-input-response': 'The response parameter is missing.', | ||
'invalid-input-response': 'The response parameter is invalid or malformed.' | ||
}; | ||
function Recaptcha2(config) { | ||
this.config = config; | ||
if (this.config.ssl === void 0) { | ||
this.config.ssl = true; | ||
this.config = Object.assign({}, DEFAULT_CONFIG, config); | ||
if (this.config.ssl === false) { | ||
this.apiEndpoint = this.apiEndpoint.replace("https", "http"); | ||
} | ||
if (this.config.ssl) { | ||
this.api = "https://www.google.com/recaptcha/api/siteverify"; | ||
} else { | ||
this.api = "http://www.google.com/recaptcha/api/siteverify"; | ||
} | ||
} | ||
Recaptcha2.prototype.getRequestOptions = function(body) { | ||
body.secret = this.config.secretKey; | ||
return Object.assign({}, DEFAULT_REQUEST_OPTIONS, { | ||
uri: this.apiEndpoint, | ||
form: body | ||
}); | ||
}; | ||
Recaptcha2.prototype.validate = function(response, remoteip) { | ||
var $; | ||
if (response == null) { | ||
response = ''; | ||
} | ||
$ = this; | ||
return new Promise(function(resolve, reject) { | ||
var options; | ||
if (response === '') { | ||
return reject(['missing-input-response']); | ||
} | ||
options = { | ||
url: $.api, | ||
method: 'POST', | ||
form: { | ||
secret: $.config.secretKey, | ||
response: response | ||
return new Promise((function(_this) { | ||
return function(resolve, reject) { | ||
var options; | ||
if (!response) { | ||
return reject(['missing-input-response']); | ||
} | ||
options = _this.getRequestOptions({ | ||
response: response, | ||
remoteip: remoteip | ||
}); | ||
return request(options, function(error, response, body) { | ||
if (error) { | ||
return reject(['request-error', error.toString()]); | ||
} | ||
if (body.success === true) { | ||
return resolve(true); | ||
} | ||
return reject(body['error-codes']); | ||
}); | ||
}; | ||
if (remoteip !== void 0) { | ||
options.form.remoteip = remoteip; | ||
} | ||
return request(options, function(error, response, body) { | ||
var result, tryErr; | ||
if (error) { | ||
return reject(['request-error', error.toString()]); | ||
} else { | ||
try { | ||
result = JSON.parse(body); | ||
if (result.success) { | ||
return resolve(true); | ||
} else { | ||
return reject(result['error-codes']); | ||
} | ||
} catch (_error) { | ||
tryErr = _error; | ||
return reject(['request-error', 'json-parse']); | ||
} | ||
} | ||
}); | ||
}); | ||
})(this)); | ||
}; | ||
Recaptcha2.prototype.validateRequest = function(req) { | ||
return this.validate(req.body['g-recaptcha-response']); | ||
Recaptcha2.prototype.validateRequest = function(req, ip) { | ||
return this.validate(req.body['g-recaptcha-response'], ip); | ||
}; | ||
Recaptcha2.prototype.translateErrors = function(errorCodes) { | ||
var i, key, len, ret; | ||
if (Array.isArray(errorCodes)) { | ||
ret = []; | ||
for (i = 0, len = errorCodes.length; i < len; i++) { | ||
key = errorCodes[i]; | ||
ret.push(errorList[key] || key); | ||
} | ||
return ret; | ||
} else { | ||
return errorList[key] || key; | ||
var i, key, len, readableErrors; | ||
if (!Array.isArray(errorCodes)) { | ||
return ERRORS[errorCodes] || errorCodes; | ||
} | ||
readableErrors = []; | ||
for (i = 0, len = errorCodes.length; i < len; i++) { | ||
key = errorCodes[i]; | ||
readableErrors.push(ERRORS[key] || key); | ||
} | ||
return readableErrors; | ||
}; | ||
Recaptcha2.prototype.formElement = function(htmlClass) { | ||
return "<div class=\"" + (htmlClass || 'g-recaptcha') + "\" data-sitekey=\"" + this.config.siteKey + "\"></div>"; | ||
if (htmlClass == null) { | ||
htmlClass = 'g-recaptcha'; | ||
} | ||
return '<div class="' + htmlClass + '" data-sitekey="' + this.config.siteKey + '"></div>'; | ||
}; | ||
@@ -97,0 +94,0 @@ |
{ | ||
"name": "recaptcha2", | ||
"version": "1.0.8", | ||
"version": "1.1.2", | ||
"description": "Easy api for google reCAPTCHA version 2 for node.js and express.js", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --compilers coffee:coffee-script/register --recursive tests", | ||
"compile": "./node_modules/.bin/coffee -c --no-header index.coffee" | ||
}, | ||
@@ -14,5 +15,10 @@ "repository": { | ||
"dependencies": { | ||
"bluebird":"2.x", | ||
"request":"2.x" | ||
"request": "2.x" | ||
}, | ||
"devDependencies": { | ||
"coffee-script": "^1.11.1", | ||
"mocha": "^2.5.3", | ||
"nock": "^8.0.0", | ||
"should": "^11.1.0" | ||
}, | ||
"keywords": [ | ||
@@ -30,3 +36,13 @@ "recaptcha", | ||
}, | ||
"homepage": "https://github.com/fereidani/recaptcha2#readme" | ||
"homepage": "https://github.com/fereidani/recaptcha2#readme", | ||
"contributors": [ | ||
{ | ||
"name": "Khashayar Fereidani", | ||
"email": "info@fereidani.com" | ||
}, | ||
{ | ||
"name": "Dumitru Glavan", | ||
"email": "contact@dumitruglavan.com" | ||
} | ||
] | ||
} |
@@ -27,3 +27,3 @@ # reCAPTCHA2 | ||
Config of main class is an javascript object and attributes are : | ||
Config of main class is a javascript object and attributes are : | ||
@@ -40,4 +40,4 @@ ``` | ||
reCAPTCHA2 use Promises to validate key you can easily use following methods to verify captchas : | ||
* please mention on catch response library return error codes from google which you can translate with translateErrors method | ||
reCAPTCHA2 use Promises to validate captch , you can easily use following methods to verify captchas : | ||
* please mention on catch , library passes error codes from google which you can translate with translateErrors method | ||
@@ -80,1 +80,4 @@ Simple: | ||
# Changelog | ||
Please see the [CHANGELOG.md file](https://github.com/fereidani/recaptcha2/blob/master/CHANGELOG.md). |
Sorry, the diff of this file is not supported yet
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
9260
1
6
0
80
4
85
- Removedbluebird@2.x
- Removedbluebird@2.11.0(transitive)