recaptcha2
Advanced tools
Comparing version 1.3.2 to 1.3.3
87
index.js
(function() { | ||
var DEFAULT_CONFIG, DEFAULT_REQUEST_OPTIONS, ERRORS, GOOGLE_CAPTCHA_ENDPOINT, Recaptcha2, request; | ||
var DEFAULT_CONFIG, DEFAULT_REQUEST_OPTIONS, ERRORS, GOOGLE_CAPTCHA_ENDPOINT, Recaptcha2, escapeAttribute, request; | ||
@@ -29,23 +29,25 @@ request = require('request'); | ||
escapeAttribute = function(atr) { | ||
return atr.replace(/"/g, """).replace(/[\r\n]/g, " "); | ||
}; | ||
Recaptcha2 = (function() { | ||
Recaptcha2.prototype.apiEndpoint = GOOGLE_CAPTCHA_ENDPOINT; | ||
class Recaptcha2 { | ||
constructor(config) { | ||
this.config = Object.assign({}, DEFAULT_CONFIG, config); | ||
if (this.config.ssl === false) { | ||
this.apiEndpoint = this.apiEndpoint.replace("https", "http"); | ||
} | ||
} | ||
function Recaptcha2(config) { | ||
this.config = Object.assign({}, DEFAULT_CONFIG, config); | ||
if (this.config.ssl === false) { | ||
this.apiEndpoint = this.apiEndpoint.replace("https", "http"); | ||
getRequestOptions(body) { | ||
body.secret = this.config.secretKey; | ||
return Object.assign({}, DEFAULT_REQUEST_OPTIONS, { | ||
uri: this.apiEndpoint, | ||
form: body | ||
}); | ||
} | ||
} | ||
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) { | ||
return new Promise((function(_this) { | ||
return function(resolve, reject) { | ||
validate(response, remoteip) { | ||
return new Promise((resolve, reject) => { | ||
var options; | ||
@@ -55,6 +57,3 @@ if (!response) { | ||
} | ||
options = _this.getRequestOptions({ | ||
response: response, | ||
remoteip: remoteip | ||
}); | ||
options = this.getRequestOptions({response, remoteip}); | ||
return request(options, function(error, response, body) { | ||
@@ -69,33 +68,33 @@ if (error) { | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
}); | ||
} | ||
Recaptcha2.prototype.validateRequest = function(req, ip) { | ||
return this.validate(req.body['g-recaptcha-response'], ip); | ||
}; | ||
validateRequest(req, ip) { | ||
return this.validate(req.body['g-recaptcha-response'], ip); | ||
} | ||
Recaptcha2.prototype.translateErrors = function(errorCodes) { | ||
var i, key, len, readableErrors; | ||
if (!Array.isArray(errorCodes)) { | ||
return ERRORS[errorCodes] || errorCodes; | ||
translateErrors(errorCodes) { | ||
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; | ||
} | ||
readableErrors = []; | ||
for (i = 0, len = errorCodes.length; i < len; i++) { | ||
key = errorCodes[i]; | ||
readableErrors.push(ERRORS[key] || key); | ||
formElement(htmlClass = 'g-recaptcha') { | ||
return '<div class="' + escapeAttribute(htmlClass) + '" data-sitekey="' + escapeAttribute(this.config.siteKey) + '"></div>'; | ||
} | ||
return readableErrors; | ||
}; | ||
Recaptcha2.prototype.formElement = function(htmlClass) { | ||
if (htmlClass == null) { | ||
htmlClass = 'g-recaptcha'; | ||
} | ||
return '<div class="' + htmlClass + '" data-sitekey="' + this.config.siteKey + '"></div>'; | ||
}; | ||
Recaptcha2.prototype.apiEndpoint = GOOGLE_CAPTCHA_ENDPOINT; | ||
return Recaptcha2; | ||
})(); | ||
}).call(this); | ||
@@ -102,0 +101,0 @@ module.exports = Recaptcha2; |
{ | ||
"name": "recaptcha2", | ||
"version": "1.3.2", | ||
"description": "Easy api for google reCAPTCHA version 2 for node.js and express.js", | ||
"version": "1.3.3", | ||
"description": "Easy API for Google reCAPTCHA version 2 for Node.js and Express", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --compilers coffee:coffee-script/register --recursive tests", | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --require coffeescript/register --recursive tests/*.coffee", | ||
"compile": "./node_modules/.bin/coffee -c --no-header index.coffee" | ||
@@ -18,5 +18,5 @@ }, | ||
"devDependencies": { | ||
"coffee-script": "^1.11.1", | ||
"mocha": "^2.5.3", | ||
"nock": "^8.0.0", | ||
"coffeescript": "^2.3.2", | ||
"mocha": "^5.2.0", | ||
"nock": "^10.0.1", | ||
"should": "^11.1.0" | ||
@@ -23,0 +23,0 @@ }, |
104
README.md
# reCAPTCHA2 | ||
Easy verifier for google reCAPTCHA version 2 for Node.js | ||
Easy verifier for Google reCAPTCHA version 2 for Node.js | ||
# Documents : | ||
## How to use | ||
**First:** | ||
### Step 1: Setup reCAPTCHA on your site | ||
You need to receive Site Key and Secret Key for your domain from : https://www.google.com/recaptcha/intro/index.html | ||
You need to receive your site key and secret key for your domain from https://www.google.com/recaptcha/intro/. | ||
after that add this code to your html : | ||
Follow the steps on this page to include the reCAPTCHA on your website. | ||
``` | ||
<script src='https://www.google.com/recaptcha/api.js'></script> | ||
``` | ||
### Step 2: Initialize verifier | ||
```js | ||
var reCAPTCHA = require('recaptcha2'); | ||
**How to initialize :** | ||
var recaptcha = new reCAPTCHA({ | ||
siteKey: 'your-site-key', // retrieved during setup | ||
secretKey: 'your-secret-key' // retrieved during setup | ||
ssl: false // optional, defaults to true. | ||
// Disable if you don't want to access | ||
// the Google API via a secure connection | ||
}); | ||
``` | ||
reCAPTCHA=require('recaptcha2') | ||
recaptcha=new reCAPTCHA({ | ||
siteKey:'your-site-key', | ||
secretKey:'your-secret-key' | ||
}) | ||
``` | ||
**Config details :** | ||
### Step 3: Verifying the reCAPTCHA response | ||
Config of main class is a javascript object and attributes are : | ||
reCAPTCHA2 uses promises to validate the reCAPTCHA response, you can use one of the following methods: | ||
* please mention on catch, library passes error codes from google which you can translate with translateErrors method | ||
``` | ||
siteKey : your Site Key from google | ||
secretKey : your Secret Key from google | ||
ssl : use https to access google api ( boolean - default : true ) | ||
``` | ||
**How to verify captcha key :** | ||
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 | ||
Simple: | ||
``` | ||
#### Simple usage: | ||
```js | ||
recaptcha.validate(key) | ||
.then(function(){ | ||
// validated and secure | ||
}) | ||
.catch(function(errorCodes){ | ||
// invalid | ||
console.log(recaptcha.translateErrors(errorCodes));// translate error codes to human readable text | ||
}); | ||
``` | ||
you can also pass remoteip to validate method after key , for more information please read reCAPTCHA manual about remoteip . | ||
For Express (you need body-parser) : | ||
``` | ||
function submitForm(req,res){ | ||
recaptcha.validateRequest(req) | ||
.then(function(){ | ||
// validated and secure | ||
res.json({formSubmit:true}) | ||
}) | ||
.catch(function(errorCodes){ | ||
// invalid | ||
res.json({formSubmit:false,errors:recaptcha.translateErrors(errorCodes)});// translate error codes to human readable text | ||
console.log(recaptcha.translateErrors(errorCodes)); // translate error codes to human readable text | ||
}); | ||
``` | ||
**Optional:** You can also pass the clients IP address to the validate method after the key. For more information on that, please see the [reCAPTCHA documentation](https://developers.google.com/recaptcha/docs/verify). | ||
#### For use with Express (you need body-parser): | ||
```js | ||
function submitForm(req, res) { | ||
recaptcha.validateRequest(req) | ||
.then(function(){ | ||
// validated and secure | ||
res.json({formSubmit:true}) | ||
}) | ||
.catch(function(errorCodes){ | ||
// invalid | ||
res.json({ | ||
formSubmit: false, | ||
errors: recaptcha.translateErrors(errorCodes) // translate error codes to human readable text | ||
}); | ||
}); | ||
} | ||
``` | ||
**Form Element:** | ||
### Generating the reCAPTCHA widget | ||
recaptcha.formElement() returns standard form element for reCAPTCHA which you should include in end of your html form element | ||
`recaptcha.formElement()` returns standard form element for reCAPTCHA which you should include at the end of your html form element. | ||
you can also set class name like recaptcha.formElement('custom-class-for-recaptcha') | ||
You can also set CSS classes like this: `recaptcha.formElement('custom-class-for-recaptcha')`. | ||
The default class is `g-recaptcha`. | ||
# Changelog | ||
```html | ||
<div class="custom-class-for-recaptcha" data-sitekey="your-site-key"></div> | ||
``` | ||
Please see the [CHANGELOG.md file](https://github.com/fereidani/recaptcha2/blob/master/CHANGELOG.md). | ||
## Changelog | ||
Please see the [CHANGELOG.md](CHANGELOG.md). |
Sorry, the diff of this file is not supported yet
15431
7
82
75