Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
# Meteor reCAPTCHA
Google's reCAPTCHA is a free CAPTCHA service that protects your site against spam, malicious registrations and other forms of attacks where computers try to disguise themselves as a human. In addition to protecting your site, reCAPTCHA also helps digitize old books and newspapers.
# Meteor reCAPTCHA v2
This is an implementation of the Google reCAPTCHA v2 API (i.e. "I'm not a robot") for Meteor sites. reCAPTCHA is a free CAPTCHA service provided by Google that authenticates real users while attempting to block bots.

reCAPTCHA is at https://developers.google.com/recaptcha/

Package made with the help of this [StackOverflow question](http://stackoverflow.com/questions/22253196/working-example-of-recaptcha-in-meteor).
Package is an upgraded version of Meteor-reCAPTCHA provided by Altapp (https://github.com/Altapp/Meteor-reCAPTCHA).

## Installation

``` sh
$ meteor add altapp:recaptcha
$ meteor add ayue:recaptcha
```

## Setup

### Obtain reCAPTCHA Keys From Google

Register at https://www.google.com/recaptcha/admin to obtain your private and public key needed for reCAPTCHA to function.

###On The Client

Add your reCAPTCHA public key (from Google) to the package. Do this in client-side code.
Expand Down Expand Up @@ -67,12 +71,9 @@ Template.myTemplate.events({
};

//get the captcha data
var captchaData = {
captcha_challenge_id: Recaptcha.get_challenge(),
captcha_solution: Recaptcha.get_response()
};
var recaptchaResponse = grecaptcha.getResponse();

Meteor.call('formSubmissionMethod', formData, captchaData, function(error, result) {
Meteor.call('formSubmissionMethod', formData, recaptchaResponse, function(error, result) {
if (error) {
console.log('There was an error: ' + error.reason);
} else {
Expand All @@ -85,13 +86,13 @@ Template.myTemplate.events({

###On The Server

In the server method, pass the captcha data and the user's IP address to `reCAPTCHA.verifyCaptcha(clientIP, captchaData)` to verify that the user entered the correct text.
In the server method, pass the captcha data and the user's IP address to `reCAPTCHA.verifyCaptcha(recaptchaResponse, clientIP)` to verify that the user entered the correct text.

``` javascript
Meteor.methods({
formSubmissionMethod: function(formData, captchaData) {
formSubmissionMethod: function(formData, recaptchaResponse) {

var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, captchaData);
var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(recaptchaResponse, this.connection.clientAddress);

if (!verifyCaptchaResponse.success) {
console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
Expand Down
15 changes: 14 additions & 1 deletion client/client.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<template name="reCAPTCHA">
<div id="rendered-captcha-container">loading...</div>
<div id="rendered-captcha-container">loading reCAPTCHA...</div>
<script>
var onloadCallback = function() {
$( "#rendered-captcha-container" ).empty();

grecaptcha.render('rendered-captcha-container', {
sitekey: reCAPTCHA.settings.publickey,
theme: reCAPTCHA.settings.theme,
callback: function () {
return;
}
});
};
</script>
</template>
12 changes: 2 additions & 10 deletions client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ reCAPTCHA = {


Template.reCAPTCHA.rendered = function() {

$.getScript('//www.google.com/recaptcha/api/js/recaptcha_ajax.js', function() {
Recaptcha.create(reCAPTCHA.settings.publickey, 'rendered-captcha-container', {
theme: reCAPTCHA.settings.theme,
callback: function() {
return;
}
});
$.getScript('https://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit', function() {
});

}
}
8 changes: 4 additions & 4 deletions package.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package.describe({
name: "altapp:recaptcha",
summary: "Form bot protection for Meteor.",
git: "https://github.com/Altapp/Meteor-reCAPTCHA.git",
version: "1.2.0"
name: "ayue:recaptcha",
summary: "Google reCAPTCHA v2 package for Meteor",
git: "https://github.com/yuea/Meteor-reCAPTCHA.git",
version: "2.0.0"
});

Package.onUse(function(api) {
Expand Down
18 changes: 8 additions & 10 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ reCAPTCHA = {
config: function(settings) {
return _.extend(this.settings, settings);
},
verifyCaptcha: function(clientIP, data) {
verifyCaptcha: function(recaptchaResponse, clientIP) {
var captcha_data = {
privatekey: this.settings.privatekey,
secret: this.settings.privatekey, // for prod: process.env.RECAPTCHA_PRIVATE_KEY,
remoteip: clientIP,
challenge: data.captcha_challenge_id,
response: data.captcha_solution
response: recaptchaResponse
};

var serialized_captcha_data =
'privatekey=' + captcha_data.privatekey +
'secret=' + captcha_data.secret +
'&remoteip=' + captcha_data.remoteip +
'&challenge=' + captcha_data.challenge +
'&response=' + captcha_data.response;
var captchaVerificationResult = null;
var success, parts; // used to process response string

try {
captchaVerificationResult = HTTP.call("POST", "http://www.google.com/recaptcha/api/verify", {
captchaVerificationResult = HTTP.call("POST", "https://www.google.com/recaptcha/api/siteverify", {
content: serialized_captcha_data.toString('utf8'),
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Expand All @@ -36,12 +34,12 @@ reCAPTCHA = {
}

parts = captchaVerificationResult.content.split('\n');
success = parts[0];
success = parts[1];

if (success !== 'true') {
if (success.indexOf('true') < 0) {
return {
'success': false,
'error': 'Entered Text Does Not Match'
'error': 'reCAPTCHA verification failed'
};
}

Expand Down