Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Removed local Google Recaptcha code and replaced with more robust and… #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
40 changes: 0 additions & 40 deletions djangocms_forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from django.utils.translation import ugettext_lazy as _

from .conf import settings
from .widgets import ReCaptchaWidget

logger = logging.getLogger('djangocms_forms')

Expand Down Expand Up @@ -108,42 +107,3 @@ def __init__(self, *args, **kwargs):
def validate(self, value):
if value:
raise forms.ValidationError(_('Doh! You are a robot!'))


class ReCaptchaField(forms.CharField):
widget = ReCaptchaWidget
default_error_messages = {
'invalid': _('Error verifying input, please try again.'),
'recaptcha_error': _('Connection to reCaptcha server failed.'),
}
recaptcha_api = 'https://www.google.com/recaptcha/api/siteverify'

def __init__(self, *args, **kwargs):
super(ReCaptchaField, self).__init__(*args, **kwargs)

def clean(self, values):
super(ReCaptchaField, self).clean(values[0])
response_token = values[0]

try:
params = {
'secret': settings.DJANGOCMS_FORMS_RECAPTCHA_SECRET_KEY,
'response': response_token
}
r = requests.post(self.recaptcha_api, params=params, timeout=5)
r.raise_for_status()
except requests.RequestException as e:
logger.exception(e)
raise ValidationError(self.error_messages['recaptcha_error'])

data = r.json()

if bool(data['success']):
return values[0]
else:
if any(code in data.get('error-codes', {})
for code in ('missing-input-secret', 'invalid-input-secret', )):
logger.exception('Invalid reCaptcha secret key.')
raise ValidationError(self.error_messages['recaptcha_error'])
else:
raise ValidationError(self.error_messages['invalid'], code='invalid')
4 changes: 3 additions & 1 deletion djangocms_forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from ipware.ip import get_ip
from unidecode import unidecode

from .fields import FormBuilderFileField, HoneyPotField, MultipleChoiceAutoCompleteField, ReCaptchaField
from captcha.fields import ReCaptchaField

from .fields import FormBuilderFileField, HoneyPotField, MultipleChoiceAutoCompleteField
from .models import Form, FormDefinition, FormField, FormSubmission
from .utils import int_to_hashid
from .widgets import DateInput, TelephoneInput, TimeInput
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
errorItem: '<li></li>',
ajaxErrorMsg: 'We\'re sorry. Something Unexpected Happened. Please Try Again Later.',

reCaptchaSiteKey: '',
reCaptchaTheme: 'light',
reCaptchaSize: 'normal',

// needed in case someone overrides the template and doesn't pass
// in the value when initializing the cmsForms object
redirectDelay: 1000
Expand All @@ -45,29 +41,10 @@
e.preventDefault();
$(this).ajaxSubmit(ajaxOptions);
});

if (typeof(grecaptcha) == 'undefined') {
window.reCapctchaOnloadCallback = function() {
this.renderReCaptcha();
}.bind(this);
} else {
this.renderReCaptcha();
}
},
getForm: function() {
return $('form', this.el);
},
renderReCaptcha: function() {
var that = this;
$('.g-recaptcha').each(function() {
var widgetId = $(this).attr('id');
grecaptcha.render(widgetId, {
sitekey: that.settings.reCaptchaSiteKey,
size: that.settings.reCaptchaSize,
theme: that.settings.reCaptchaTheme
});
});
},
ajaxSuccess: function(response) {
if (response.formIsValid) this.formValid(response);
else this.formInvalid(response);
Expand Down
10 changes: 0 additions & 10 deletions djangocms_forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,3 @@ class DateInput(widgets.TextInput):

class TimeInput(widgets.TextInput):
input_type = 'time'


class ReCaptchaWidget(widgets.Widget):

def render(self, name, value, attrs=None):
template = '<div class="g-recaptcha" id="%(widget_id)s"></div>'
return mark_safe(template % {'widget_id': 'id_%s' % name})

def value_from_datadict(self, data, files, name):
return (data.get('g-recaptcha-response', None), )
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
install_requires=[
'django-appconf',
'django-ipware',
'django-recaptcha',
'jsonfield',
'unidecode',
'tablib',
Expand Down