Esempio n. 1
0
    def _validate_recaptcha(self, challenge, response, remote_addr):
        """Performs the actual validation."""
        private_key = current_handler.get_config('tipfyext.wtforms',
            'recaptcha_private_key')
        result = urlfetch.fetch(url=RECAPTCHA_VERIFY_SERVER,
            method=urlfetch.POST,
            headers={'Content-Type': 'application/x-www-form-urlencoded'},
            payload=url_encode({
                'privatekey': private_key,
                'remoteip':   remote_addr,
                'challenge':  challenge,
                'response':   response
            }))

        if result.status_code != 200:
            return False

        rv = [l.strip() for l in result.content.splitlines()]

        if rv and rv[0] == 'true':
            return True

        if len(rv) > 1:
            error = rv[1]
            if error in self._error_codes:
                raise RuntimeError(self._error_codes[error])

        return False
Esempio n. 2
0
    def _validate_recaptcha(self, challenge, response, remote_addr):
        """Performs the actual validation."""
        private_key = current_handler.get_config("tipfyext.wtforms", "recaptcha_private_key")
        result = urlfetch.fetch(
            url=RECAPTCHA_VERIFY_SERVER,
            method=urlfetch.POST,
            headers={"Content-Type": "application/x-www-form-urlencoded"},
            payload=url_encode(
                {"privatekey": private_key, "remoteip": remote_addr, "challenge": challenge, "response": response}
            ),
        )

        if result.status_code != 200:
            return False

        rv = [l.strip() for l in result.content.splitlines()]

        if rv and rv[0] == "true":
            return True

        if len(rv) > 1:
            error = rv[1]
            if error in self._error_codes:
                raise RuntimeError(self._error_codes[error])

        return False
Esempio n. 3
0
    def _get_csrf_token(self, request):
        token = str(uuid.uuid4())
        token_list = self._get_session().setdefault("_csrf_token", [])
        token_list.append(token)
        # Store a maximum number of tokens.
        maximum_tokens = current_handler.get_config("tipfyext.wtforms", "csrf_tokens")
        while len(token_list) > maximum_tokens:
            token_list.pop(0)

        # Set the validation rule for the tokens.
        self._fields["csrf_token"].validators = [CsrfToken(token_list)]
        return token
Esempio n. 4
0
    def _get_csrf_token(self, request):
        token = str(uuid.uuid4())
        token_list = self._get_session().setdefault('_csrf_token', [])
        token_list.append(token)
        # Store a maximum number of tokens.
        maximum_tokens = current_handler.get_config('tipfyext.wtforms',
                                                    'csrf_tokens')
        while len(token_list) > maximum_tokens:
            token_list.pop(0)

        # Set the validation rule for the tokens.
        self._fields['csrf_token'].validators = [CsrfToken(token_list)]
        return token
Esempio n. 5
0
    def __call__(self, field, error=None, **kwargs):
        """Returns the recaptcha input HTML."""
        config = current_handler.get_config('tipfyext.wtforms')
        if config.get('recaptcha_use_ssl'):
            server = RECAPTCHA_SSL_API_SERVER
        else:
            server = RECAPTCHA_API_SERVER

        query_options = dict(k=config.get('recaptcha_public_key'))

        if field.recaptcha_error is not None:
            query_options['error'] = unicode(field.recaptcha_error)

        query = url_encode(query_options)

        # Widget default options.
        options = {
            'theme': 'clean',
            'custom_translations': {
                'visual_challenge': _('Get a visual challenge'),
                'audio_challenge': _('Get an audio challenge'),
                'refresh_btn': _('Get a new challenge'),
                'instructions_visual': _('Type the two words:'),
                'instructions_audio': _('Type what you hear:'),
                'help_btn': _('Help'),
                'play_again': _('Play sound again'),
                'cant_hear_this': _('Download sound as MP3'),
                'incorrect_try_again': _('Incorrect. Try again.'),
            }
        }
        custom_options = config.get('recaptcha_options')
        if custom_options:
            options.update(custom_options)

        return RECAPTCHA_HTML % dict(script_url='%schallenge?%s' %
                                     (server, query),
                                     frame_url='%snoscript?%s' %
                                     (server, query),
                                     options=json_encode(options))
Esempio n. 6
0
    def __call__(self, field, error=None, **kwargs):
        """Returns the recaptcha input HTML."""
        config = current_handler.get_config('tipfyext.wtforms')
        if config.get('recaptcha_use_ssl'):
            server = RECAPTCHA_SSL_API_SERVER
        else:
            server = RECAPTCHA_API_SERVER

        query_options = dict(k=config.get('recaptcha_public_key'))

        if field.recaptcha_error is not None:
            query_options['error'] = unicode(field.recaptcha_error)

        query = url_encode(query_options)

        # Widget default options.
        options = {
            'theme': 'clean',
            'custom_translations': {
                'visual_challenge':    _('Get a visual challenge'),
                'audio_challenge':     _('Get an audio challenge'),
                'refresh_btn':         _('Get a new challenge'),
                'instructions_visual': _('Type the two words:'),
                'instructions_audio':  _('Type what you hear:'),
                'help_btn':            _('Help'),
                'play_again':          _('Play sound again'),
                'cant_hear_this':      _('Download sound as MP3'),
                'incorrect_try_again': _('Incorrect. Try again.'),
            }
        }
        custom_options = config.get('recaptcha_options')
        if custom_options:
            options.update(custom_options)

        return RECAPTCHA_HTML % dict(
            script_url='%schallenge?%s' % (server, query),
            frame_url='%snoscript?%s' % (server, query),
            options=json_encode(options)
        )