Index: main/waeup.sirp/trunk/src/waeup/sirp/browser/captcha.py
===================================================================
--- main/waeup.sirp/trunk/src/waeup/sirp/browser/captcha.py	(revision 7302)
+++ main/waeup.sirp/trunk/src/waeup/sirp/browser/captcha.py	(revision 7303)
@@ -237,54 +237,38 @@
 ##
 API_SSL_SERVER = "https://www.google.com/recaptcha/api"
-VERIFY_SERVER = "http://www.google.com"
+VERIFY_SERVER = "https://www.google.com/recaptcha/api"
+# Do not use the following keys in productive environments!
+# As they are both made publicly available, they are not secure any more!
+# Use them for testing and evaluating only!
 PUBLIC_KEY = "6Lc0y8oSAAAAAHwdojrqPtcKn7Rww5qGprb0rrSk"
 PRIVATE_KEY = "6Lc0y8oSAAAAAMHVbMrGWLLjw2pm8v2Uprwm9AbR"
 
-
-
-class RecaptchaResponse(object):
-    def __init__(self, is_valid, error_code=None):
-        self.is_valid = is_valid
-        self.error_code = error_code
-        return
-
-class CaptchaTestPage(SIRPPage):
-    grok.name('captcha')
-    grok.context(IUniversity)
-    grok.require('waeup.Public')
-    title = 'Captcha Test'
-    label = title
-
-    def update(self, recaptcha_challenge_field=None,
-               recaptcha_response_field=None):
-        self.error_code = None
-        print "CHAL: ", recaptcha_challenge_field
-        print "RESP: ", recaptcha_response_field
-        print self.request.form.items()
-        verify_result = 'NONE'
-        if recaptcha_challenge_field and recaptcha_response_field:
-            verify_result = self.verify(
-                recaptcha_challenge_field, recaptcha_response_field)
-        print "VERIFY: ", verify_result
-        if verify_result != 'NONE':
-            print "  VALID: ", verify_result.is_valid
-            print "  ERR: ", verify_result.error_code
-            self.error_code = verify_result.error_code
-        pass
-
-    def verify(self, challenge, solution, private_key=PRIVATE_KEY,
-               remote_ip='127.0.0.1'):
+class ReCaptcha(StaticCaptcha):
+
+    grok.implements(ICaptcha)
+
+    #: name of solution field in HTTP request
+    sol_field = 'recaptcha_response_field'
+    #: name of challenge field in HTTP request
+    chal_field = 'recaptcha_challenge_field'
+
+    def verify(self, request):
+        form = getattr(request, 'form', {})
+        solution=form.get(self.sol_field, None)
+        challenge=form.get(self.chal_field, None)
         if not challenge or not solution:
-            return RecaptchaResponse(
-                is_valid=False, error_code='incorrect-captcha-sol')
+            # maybe first display of the captcha: not a valid solution
+            # but no error code to prevent any error messages
+            return CaptchaResponse(
+                is_valid=False)
         params = urllib.urlencode(
             {
-                'privatekey': private_key,
-                'remoteip': remote_ip,
+                'privatekey': PRIVATE_KEY,
+                'remoteip': '127.0.0.1',
                 'challenge': challenge,
                 'response': solution,
                 })
         request = urllib2.Request(
-            url = "%s/recaptcha/api/verify" % VERIFY_SERVER,
+            url = "%s/verify" % VERIFY_SERVER,
             data = params,
             headers = {
@@ -299,29 +283,58 @@
 
         if ret_code == "true":
-            return RecaptchaResponse(is_valid=True)
-        return RecaptchaResponse(is_valid=False, error_code=ret_vals[1])
+            return CaptchaResponse(is_valid=True)
+        return CaptchaResponse(is_valid=False, error_code=ret_vals[1])
+
+    def display(self, error_code=None):
+        """Display challenge and input field for solution as HTML.
+        """
+        error_param = ''
+        if error_code:
+            error_param = '&error=%s' % error_code
+
+        html = (
+            u'<script type="text/javascript" '
+            u'src="%(ApiServer)s/challenge?k=%(PublicKey)s%(ErrorParam)s">'
+            u'</script>'
+            u''
+            u'<noscript>'
+            u'<iframe'
+            u'    src="%(ApiServer)s/noscript?k=%(PublicKey)s%(ErrorParam)s"'
+            u'    height="300" width="500" frameborder="0"></iframe><br />'
+            u'<textarea name="recaptcha_challenge_field"'
+            u'          rows="3" cols="40"></textarea>'
+            u'<input type="hidden" name="recaptcha_response_field"'
+            u'       value="manual_challenge" />'
+            u'</noscript>' % {
+                'ApiServer' : API_SSL_SERVER,
+                'PublicKey' : PUBLIC_KEY,
+                'ErrorParam' : error_param,
+                }
+            )
+        return html
+
+grok.global_utility(ReCaptcha, name=u'ReCaptcha')
+
+
+class CaptchaTestPage(SIRPPage):
+    grok.name('captcha')
+    grok.context(IUniversity)
+    grok.require('waeup.Public')
+    title = 'Captcha Test'
+    label = title
+
+    def update(self, recaptcha_challenge_field=None,
+               recaptcha_response_field=None):
+        self.captcha = getUtility(ICaptcha, name='ReCaptcha')
+        result = self.captcha.verify(self.request)
+        self.captcha_error = result.error_code
+        print "VERIFY: ", result.is_valid, result.error_code
+        return
 
     def render(self):
-        error_param = ''
-        if self.error_code:
-            error_param = '&error=%s' % self.error_code
         return """
   <form method="POST" action="">
-    <script type="text/javascript"
-            src="%(ApiServer)s/challenge?k=%(PublicKey)s%(ErrorParam)s">
-    </script>
-
-    <noscript>
-      <iframe src="%(ApiServer)s/noscript?k=%(PublicKey)s%(ErrorParam)s"
-              height="300" width="500" frameborder="0"></iframe><br />
-      <textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
-      <input type='hidden' name='recaptcha_response_field'
-             value='manual_challenge' />
-    </noscript>
+    %s
     <input type="submit" name="SUBMIT" />
   </form>
-""" % {
-        'ApiServer' : API_SSL_SERVER,
-        'PublicKey' : PUBLIC_KEY,
-        'ErrorParam' : error_param,
-        }
+  """ % (self.captcha.display(self.captcha_error),)
