Exemple #1
0
    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed
Exemple #2
0
    def __init__(self, request, data=None, *args, **kwargs):
        super(CaptchaForm, self).__init__(data, *args, **kwargs)
        self.fresh = False

        if (data is None or
                'captcha_id' not in data or
                'captcha' not in request.session or
                data['captcha_id'] not in request.session['captcha']):
            self.captcha = MathCaptcha()
            self.fresh = True
            request.session['captcha'] = {self.captcha.id: self.captcha.hashed}
        else:
            captcha_id = data['captcha_id']
            self.captcha = MathCaptcha.from_hash(
                request.session['captcha'].pop(captcha_id),
                captcha_id
            )

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.id
 def test_object(self):
     captcha = MathCaptcha("1 * 2")
     self.assertFalse(captcha.validate(1))
     self.assertTrue(captcha.validate(2))
     restored = MathCaptcha.from_hash(captcha.hashed)
     self.assertEqual(captcha.question, restored.question)
     self.assertRaises(ValueError, MathCaptcha.from_hash, captcha.hashed[:40])
Exemple #4
0
    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed
 def test_generate(self):
     """
     Test generating of captcha for every operator.
     """
     captcha = MathCaptcha()
     for operator in MathCaptcha.operators:
         captcha.operators = (operator,)
         self.assertIn(operator, captcha.generate_question())
Exemple #6
0
 def test_object(self):
     captcha = MathCaptcha('1 * 2')
     self.assertFalse(captcha.validate(1))
     self.assertTrue(captcha.validate(2))
     restored = MathCaptcha.from_hash(captcha.hashed)
     self.assertEqual(captcha.question, restored.question)
     self.assertRaises(ValueError, MathCaptcha.from_hash,
                       captcha.hashed[:40])
Exemple #7
0
 def test_generate(self):
     '''
     Test generating of captcha for every operator.
     '''
     captcha = MathCaptcha()
     for operator in MathCaptcha.operators:
         captcha.operators = (operator, )
         self.assertIn(operator, captcha.generate_question())
Exemple #8
0
 def generate_captcha(self):
     self.captcha = MathCaptcha()
     self.request.session['captcha'] = self.captcha.hashed
     # Set correct label
     self.fields['captcha'].label = pgettext(
         'Question for a mathematics-based CAPTCHA, '
         'the %s is an arithmetic problem',
         'What is %s?') % self.captcha.display
Exemple #9
0
 def generate_captcha(self):
     self.captcha = MathCaptcha()
     self.request.session["captcha"] = self.captcha.serialize()
     # Set correct label
     self.fields["captcha"].label = (pgettext(
         "Question for a mathematics-based CAPTCHA, "
         "the %s is an arithmetic problem",
         "What is %s?",
     ) % self.captcha.display)
Exemple #10
0
class CaptchaForm(forms.Form):
    captcha = forms.IntegerField(required=True)

    def __init__(self, request, form=None, data=None, *args, **kwargs):
        super().__init__(data, *args, **kwargs)
        self.fresh = False
        self.request = request
        self.form = form

        if data is None or "captcha" not in request.session:
            self.generate_captcha()
            self.fresh = True
        else:
            self.captcha = MathCaptcha.unserialize(request.session.pop("captcha"))

    def generate_captcha(self):
        self.captcha = MathCaptcha()
        self.request.session["captcha"] = self.captcha.serialize()
        # Set correct label
        self.fields["captcha"].label = (
            pgettext(
                "Question for a mathematics-based CAPTCHA, "
                "the %s is an arithmetic problem",
                "What is %s?",
            )
            % self.captcha.display
        )

    def clean_captcha(self):
        """Validation for CAPTCHA."""
        if self.fresh or not self.captcha.validate(self.cleaned_data["captcha"]):
            self.generate_captcha()
            rotate_token(self.request)
            raise forms.ValidationError(
                # Translators: Shown on wrong answer to the mathematics-based CAPTCHA
                _("That was not correct, please try again.")
            )

        if self.form.is_valid():
            mail = self.form.cleaned_data["email"]
        else:
            mail = "NONE"

        LOGGER.info(
            "Correct CAPTCHA for %s (%s = %s)",
            mail,
            self.captcha.question,
            self.cleaned_data["captcha"],
        )
Exemple #11
0
class CaptchaMixin(object):
    """Mixin to add Captcha to form."""
    tampering = True

    def load_captcha(self, data):
        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?') % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed

    def clean_captcha(self):
        """Validation for captcha."""
        if (self.tampering
                or not self.captcha.validate(self.cleaned_data['captcha'])):
            raise forms.ValidationError(
                _('Please check your math and try again.'))

        mail = self.cleaned_data.get('email', 'NONE')

        LOGGER.info('Passed captcha for %s (%s = %s)', mail,
                    self.captcha.question, self.cleaned_data['captcha'])
Exemple #12
0
    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(data, *args, **kwargs)

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = _('What is %s?') % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed
Exemple #13
0
 def generate_captcha(self):
     self.captcha = MathCaptcha()
     self.request.session['captcha'] = self.captcha.hashed
     # Set correct label
     self.fields['captcha'].label = pgettext(
         'Question for a mathematics-based CAPTCHA, '
         'the %s is an arithmetic problem',
         'What is %s?'
     ) % self.captcha.display
Exemple #14
0
    def load_captcha(self, data):
        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?') % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed
Exemple #15
0
class CaptchaRegistrationForm(RegistrationForm):
    '''
    Registration form with captcha protection.
    '''
    captcha = forms.IntegerField(required=True)
    captcha_id = forms.CharField(widget=forms.HiddenInput)

    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed

    def clean_captcha(self):
        '''
        Validation for captcha.
        '''
        if (self.tampering or
                not self.captcha.validate(self.cleaned_data['captcha'])):
            raise forms.ValidationError(
                _('Please check your math and try again.')
            )

        if 'email' in self.cleaned_data:
            mail = self.cleaned_data['email']
        else:
            mail = 'NONE'

        weblate.logger.info(
            'Passed captcha for %s (%s = %s)',
            mail,
            self.captcha.question,
            self.cleaned_data['captcha']
        )
Exemple #16
0
    def __init__(self, request, data=None, *args, **kwargs):
        super(CaptchaForm, self).__init__(data, *args, **kwargs)
        self.fresh = False
        self.request = request

        if data is None or 'captcha' not in request.session:
            self.generate_captcha()
            self.fresh = True
        else:
            self.captcha = MathCaptcha.from_hash(
                request.session.pop('captcha'))
Exemple #17
0
    def __init__(self, request, form=None, data=None, *args, **kwargs):
        super().__init__(data, *args, **kwargs)
        self.fresh = False
        self.request = request
        self.form = form

        if data is None or "captcha" not in request.session:
            self.generate_captcha()
            self.fresh = True
        else:
            self.captcha = MathCaptcha.unserialize(request.session.pop("captcha"))
Exemple #18
0
    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = _('What is %s?') % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed
Exemple #19
0
 def test_object(self):
     captcha = MathCaptcha('1 * 2')
     self.assertFalse(captcha.validate(1))
     self.assertTrue(captcha.validate(2))
     restored = MathCaptcha.unserialize(captcha.serialize())
     self.assertEqual(captcha.question, restored.question)
     self.assertTrue(restored.validate(2))
Exemple #20
0
    def __init__(self, request, form=None, data=None, *args, **kwargs):
        super(CaptchaForm, self).__init__(data, *args, **kwargs)
        self.fresh = False
        self.request = request
        self.form = form

        if data is None or 'captcha' not in request.session:
            self.generate_captcha()
            self.fresh = True
        else:
            self.captcha = MathCaptcha.from_hash(
                request.session.pop('captcha')
            )
Exemple #21
0
class CaptchaRegistrationForm(RegistrationForm):
    '''
    Registration form with captcha protection.
    '''
    captcha = forms.IntegerField(required=True)
    captcha_id = forms.CharField(widget=forms.HiddenInput)

    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(
            data,
            *args,
            **kwargs
        )

        # Load data
        self.tampering = False
        if data is None or 'captcha_id' not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data['captcha_id'])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.hashed

    def clean_captcha(self):
        '''
        Validation for captcha.
        '''
        if (self.tampering or
                not self.captcha.validate(self.cleaned_data['captcha'])):
            raise forms.ValidationError(
                _('Please check your math and try again.')
            )

        mail = self.cleaned_data.get('email', 'NONE')

        LOGGER.info(
            'Passed captcha for %s (%s = %s)',
            mail,
            self.captcha.question,
            self.cleaned_data['captcha']
        )
Exemple #22
0
class CaptchaForm(forms.Form):
    captcha = forms.IntegerField(required=True)

    def __init__(self, request, form=None, data=None, *args, **kwargs):
        super(CaptchaForm, self).__init__(data, *args, **kwargs)
        self.fresh = False
        self.request = request
        self.form = form

        if data is None or 'captcha' not in request.session:
            self.generate_captcha()
            self.fresh = True
        else:
            self.captcha = MathCaptcha.from_hash(
                request.session.pop('captcha')
            )

    def generate_captcha(self):
        self.captcha = MathCaptcha()
        self.request.session['captcha'] = self.captcha.hashed
        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display

    def clean_captcha(self):
        """Validation for captcha."""
        if (self.fresh or
                not self.captcha.validate(self.cleaned_data['captcha'])):
            self.generate_captcha()
            rotate_token(self.request)
            raise forms.ValidationError(
                _('Please check your math and try again with new expression.')
            )

        if self.form.is_valid():
            mail = self.form.cleaned_data['email']
        else:
            mail = 'NONE'

        LOGGER.info(
            'Passed captcha for %s (%s = %s)',
            mail,
            self.captcha.question,
            self.cleaned_data['captcha']
        )
Exemple #23
0
class CaptchaForm(forms.Form):
    captcha = forms.IntegerField(required=True)

    def __init__(self, request, form=None, data=None, *args, **kwargs):
        super(CaptchaForm, self).__init__(data, *args, **kwargs)
        self.fresh = False
        self.request = request
        self.form = form

        if data is None or 'captcha' not in request.session:
            self.generate_captcha()
            self.fresh = True
        else:
            self.captcha = MathCaptcha.from_hash(
                request.session.pop('captcha')
            )

    def generate_captcha(self):
        self.captcha = MathCaptcha()
        self.request.session['captcha'] = self.captcha.hashed
        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display

    def clean_captcha(self):
        """Validation for CAPTCHA."""
        if (self.fresh or
                not self.captcha.validate(self.cleaned_data['captcha'])):
            self.generate_captcha()
            rotate_token(self.request)
            raise forms.ValidationError(
                _('Please check your math and try again with new expression.')
            )

        if self.form.is_valid():
            mail = self.form.cleaned_data['email']
        else:
            mail = 'NONE'

        LOGGER.info(
            'Passed captcha for %s (%s = %s)',
            mail,
            self.captcha.question,
            self.cleaned_data['captcha']
        )
Exemple #24
0
class CaptchaForm(forms.Form):
    captcha = forms.IntegerField(required=True)
    captcha_id = forms.CharField(widget=forms.HiddenInput)

    def __init__(self, request, data=None, *args, **kwargs):
        super(CaptchaForm, self).__init__(data, *args, **kwargs)
        self.fresh = False

        if (data is None or
                'captcha_id' not in data or
                'captcha' not in request.session or
                data['captcha_id'] not in request.session['captcha']):
            self.captcha = MathCaptcha()
            self.fresh = True
            request.session['captcha'] = {self.captcha.id: self.captcha.hashed}
        else:
            captcha_id = data['captcha_id']
            self.captcha = MathCaptcha.from_hash(
                request.session['captcha'].pop(captcha_id),
                captcha_id
            )

        # Set correct label
        self.fields['captcha'].label = pgettext(
            'Question for a mathematics-based CAPTCHA, '
            'the %s is an arithmetic problem',
            'What is %s?'
        ) % self.captcha.display
        self.fields['captcha_id'].initial = self.captcha.id

    def clean_captcha(self):
        """Validation for captcha."""
        if (self.fresh or
                not self.captcha.validate(self.cleaned_data['captcha'])):
            raise forms.ValidationError(
                _('Please check your math and try again.')
            )

        mail = self.cleaned_data.get('email', 'NONE')

        LOGGER.info(
            'Passed captcha for %s (%s = %s)',
            mail,
            self.captcha.question,
            self.cleaned_data['captcha']
        )
Exemple #25
0
class CaptchaRegistrationForm(RegistrationForm):
    """
    Registration form with captcha protection.
    """

    captcha = forms.IntegerField(required=True)
    captcha_id = forms.CharField(widget=forms.HiddenInput)

    def __init__(self, data=None, *args, **kwargs):
        super(CaptchaRegistrationForm, self).__init__(data, *args, **kwargs)

        # Load data
        self.tampering = False
        if data is None or "captcha_id" not in data:
            self.captcha = MathCaptcha()
        else:
            try:
                self.captcha = MathCaptcha.from_hash(data["captcha_id"])
            except ValueError:
                self.captcha = MathCaptcha()
                self.tampering = True

        # Set correct label
        self.fields["captcha"].label = (
            pgettext("Question for a mathematics-based CAPTCHA, " "the %s is an arithmetic problem", "What is %s?")
            % self.captcha.display
        )
        self.fields["captcha_id"].initial = self.captcha.hashed

    def clean_captcha(self):
        """
        Validation for captcha.
        """
        if self.tampering or not self.captcha.validate(self.cleaned_data["captcha"]):
            raise forms.ValidationError(_("Please check your math and try again."))

        mail = self.cleaned_data.get("email", "NONE")

        LOGGER.info("Passed captcha for %s (%s = %s)", mail, self.captcha.question, self.cleaned_data["captcha"])