-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (40 loc) · 1.32 KB
/
index.js
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
/*
* Express middleware for Google's No CAPTCHA reCAPTCHA
* (http://www.google.com/recaptcha/intro/)
*
* When form is posted with No CAPTCHA reCAPTCHA element in it, the middleware
* will check the generated token against Google API.
* If token is good, req.validnocaptcha is set as true.
*/
/* jshint node: true */
var request = require('request');
// Options
var options = null;
/*
* Middleware
*/
var middleware = function(req, res, next) {
// Only handle POST requests
if(req.method !== 'POST') { return next(); }
// If no body nor g-recaptcha-response was defined, GTFO
if(!req.body || !req.body['g-recaptcha-response']) { return next(); }
// Verify user with Google API
request.get({ url: 'https://www.google.com/recaptcha/api/siteverify?secret='+options.secret+'&response='+req.body['g-recaptcha-response']+'&remoteip='+req.ip, json: true }, function(err, response, body) {
if(err) { return next(err); }
if(body.success) { req.validnocaptcha = true; }
next();
});
};
/*
* Factory which receives the options and returns the middleware
*/
module.exports = function(opts) {
opts = opts || {};
// Make sure the secret is set
if(!opts.secret) {
throw new Error('Secret key is required for "express-nocaptcha" to work');
}
// Store options and return middleware
options = opts;
return middleware;
};