From 96fa3c249b8f443d0063f07b9273f1eaca3314fe Mon Sep 17 00:00:00 2001 From: carderm Date: Tue, 5 Feb 2019 16:40:13 +1100 Subject: [PATCH] Removed local Google Recaptcha code and replaced with more robust and simple Django-Recaptcha. --- djangocms_forms/fields.py | 40 ------------------- djangocms_forms/forms.py | 4 +- .../djangocms_forms/jquery.djangocms-forms.js | 23 ----------- djangocms_forms/widgets.py | 10 ----- setup.py | 1 + 5 files changed, 4 insertions(+), 74 deletions(-) diff --git a/djangocms_forms/fields.py b/djangocms_forms/fields.py index f856969..09ebe97 100644 --- a/djangocms_forms/fields.py +++ b/djangocms_forms/fields.py @@ -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') @@ -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') diff --git a/djangocms_forms/forms.py b/djangocms_forms/forms.py index 1f514e4..81166db 100644 --- a/djangocms_forms/forms.py +++ b/djangocms_forms/forms.py @@ -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 diff --git a/djangocms_forms/static/js/djangocms_forms/jquery.djangocms-forms.js b/djangocms_forms/static/js/djangocms_forms/jquery.djangocms-forms.js index 636a673..23b41df 100644 --- a/djangocms_forms/static/js/djangocms_forms/jquery.djangocms-forms.js +++ b/djangocms_forms/static/js/djangocms_forms/jquery.djangocms-forms.js @@ -15,10 +15,6 @@ errorItem: '
  • ', 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 @@ -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); diff --git a/djangocms_forms/widgets.py b/djangocms_forms/widgets.py index 39b6d27..97542ea 100644 --- a/djangocms_forms/widgets.py +++ b/djangocms_forms/widgets.py @@ -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 = '
    ' - return mark_safe(template % {'widget_id': 'id_%s' % name}) - - def value_from_datadict(self, data, files, name): - return (data.get('g-recaptcha-response', None), ) diff --git a/setup.py b/setup.py index 26e850a..b97dcc9 100755 --- a/setup.py +++ b/setup.py @@ -37,6 +37,7 @@ install_requires=[ 'django-appconf', 'django-ipware', + 'django-recaptcha', 'jsonfield', 'unidecode', 'tablib',