File: sec_recaptcha.py

package info (click to toggle)
moin 1.9.8-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 75,060 kB
  • ctags: 43,795
  • sloc: python: 118,348; java: 10,704; php: 2,374; perl: 1,572; xml: 371; makefile: 213; sh: 79; sed: 5
file content (73 lines) | stat: -rw-r--r-- 2,248 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - recaptcha support

    Based heavily on the textcha support in textcha.py

    @copyright: 2011 by Steve McIntyre
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin import log
from recaptcha.client import captcha
import sys

logging = log.getLogger(__name__)

from MoinMoin import wikiutil

class ReCaptcha(object):
    """ Recaptcha support """

    def __init__(self, request):
        """ Initialize the Recaptcha setup.

            @param request: the request object
        """
        self.request = request
        self.user_info = request.user.valid and request.user.name or request.remote_addr
        cfg = request.cfg

        try:
            if cfg.recaptcha_public_key:
                self.public_key = cfg.recaptcha_public_key
            if cfg.recaptcha_private_key:
                self.private_key = cfg.recaptcha_private_key
        except:
            self.public_key = None
            self.private_key = None

    def is_enabled(self):
        """ check if we're configured, i.e. we have a key
        """
        if (self.public_key and self.private_key):
            return True
        return False

    def check_answer_from_form(self, form=None):
        if self.is_enabled():
            if form is None:
                form = self.request.form
            challenge = form.get('recaptcha_challenge_field')
            response = form.get('recaptcha_response_field')
            captcha_result = captcha.submit(challenge, response, self.private_key, self.request.remote_addr)
            if captcha_result.is_valid:
                logging.info(u"ReCaptcha: OK.")
                return True
            else:
                logging.info(u"ReCaptcha: failed, error code %s." % captcha_result.error_code)
                return False
        else:
            return True

    def render(self, form=None):
        """ Checks if ReCaptchas are enabled and returns HTML for one,
            or an empty string if they are not enabled.

            @return: unicode result html
        """
        if self.is_enabled():
            result = captcha.displayhtml(self.public_key, use_ssl = True)
        else:
            result = u''
        return result