Repository: ckan/ckan

Issue:
recaptcha v1 will stop working 2018-3-31
### CKAN Version if known (or site URL)
All since 2.0ish

### The problem
Users will not be able to register, due to the re-captcha being switched off by Google on 2018-3-31.

Google's deprecation info: https://developers.google.com/recaptcha/docs/versions#v1

This affects those sites that:
* have setup recaptcha (i.e. registered with Google and [added their keys to the CKAN config](http://docs.ckan.org/en/latest/maintaining/configuration.html?highlight=captcha#ckan-recaptcha-publickey)). This is not part of the default CKAN install. Most installs only use the 'user' functionality for admins, so the impact should be limited.
* AND they use v1 of recaptcha. This is the default in the CKAN config, but Google deprecated v1 May 2016 and prevented new sites using it (I imagine it was the same time), so the issue only affects sites set-up before then.

### How to check if you have this problem
Show the relevant bits of your CKAN config:
```
grep recaptcha /etc/ckan/default/production.ini
```
IF `ckan.recaptcha.publickey` has a value (i.e. not blank or unset)
AND (`ckan.recaptcha.version = 1` OR `ckan.recaptcha.version` is blank or unset)
THEN you need to upgrade to recaptcha 2 before 2018-03-31.

### Action
I think we should change the default to v2 and warn the community.


Discussion:
@davidread sounds good, do you want a submit a PR chaging the default? This we can backport. And then we can remove support completely for v1 in a separate PR.

Resolving patch:
diff --git a/ckan/lib/app_globals.py b/ckan/lib/app_globals.py
--- a/ckan/lib/app_globals.py
+++ b/ckan/lib/app_globals.py
@@ -42,7 +42,6 @@
         # has been setup in load_environment():
     'ckan.site_id': {},
     'ckan.recaptcha.publickey': {'name': 'recaptcha_publickey'},
-    'ckan.recaptcha.version': {'name': 'recaptcha_version', 'default': '1'},
     'ckan.template_title_deliminater': {'default': '-'},
     'ckan.template_head_end': {},
     'ckan.template_footer_end': {},
diff --git a/ckan/lib/captcha.py b/ckan/lib/captcha.py
--- a/ckan/lib/captcha.py
+++ b/ckan/lib/captcha.py
@@ -13,48 +13,29 @@ def check_recaptcha(request):
     if not recaptcha_private_key:
         # Recaptcha not enabled
         return
-    
+
     client_ip_address = request.environ.get('REMOTE_ADDR', 'Unknown IP Address')
-    
-    recaptcha_version = config.get('ckan.recaptcha.version', '1')
-    if recaptcha_version is '1':
-        recaptcha_response_field = request.params.get('recaptcha_response_field', '')
-        recaptcha_server_name = 'http://api-verify.recaptcha.net/verify'
-        recaptcha_challenge_field = request.params.get('recaptcha_challenge_field')
-
-        # recaptcha_response_field will be unicode if there are foreign chars in
-        # the user input. So we need to encode it as utf8 before urlencoding or
-        # we get an exception (#1431).
-        params = urllib.urlencode(dict(privatekey=recaptcha_private_key,
-                                       remoteip=client_ip_address,
-                                       challenge=recaptcha_challenge_field,
-                                       response=recaptcha_response_field.encode('utf8')))
-        f = urllib2.urlopen(recaptcha_server_name, params)
-        data = f.read()
-        f.close()
-        
-        if not data.lower().startswith('true'):
-            raise CaptchaError()
-    elif recaptcha_version is '2':
-        recaptcha_response_field = request.params.get('g-recaptcha-response', '')
-        recaptcha_server_name = 'https://www.google.com/recaptcha/api/siteverify'
-
-        # recaptcha_response_field will be unicode if there are foreign chars in
-        # the user input. So we need to encode it as utf8 before urlencoding or
-        # we get an exception (#1431).
-        params = urllib.urlencode(dict(secret=recaptcha_private_key,
-                                       remoteip=client_ip_address,
-                                       response=recaptcha_response_field.encode('utf8')))
-        f = urllib2.urlopen(recaptcha_server_name, params)
-        data = json.load(f)   
-        f.close()
-    
-        try:
-            if not data['success']:
-                raise CaptchaError()
-        except IndexError:
-            # Something weird with recaptcha response
+
+    # reCAPTCHA v2
+    recaptcha_response_field = request.params.get('g-recaptcha-response', '')
+    recaptcha_server_name = 'https://www.google.com/recaptcha/api/siteverify'
+
+    # recaptcha_response_field will be unicode if there are foreign chars in
+    # the user input. So we need to encode it as utf8 before urlencoding or
+    # we get an exception (#1431).
+    params = urllib.urlencode(dict(secret=recaptcha_private_key,
+                                   remoteip=client_ip_address,
+                                   response=recaptcha_response_field.encode('utf8')))
+    f = urllib2.urlopen(recaptcha_server_name, params)
+    data = json.load(f)
+    f.close()
+
+    try:
+        if not data['success']:
             raise CaptchaError()
+    except IndexError:
+        # Something weird with recaptcha response
+        raise CaptchaError()
 
 class CaptchaError(ValueError):
-    pass
\ No newline at end of file
+    pass


Test patch: