Skip to content

Commit

Permalink
Merge pull request #22 from datopian/feature/recaptcha
Browse files Browse the repository at this point in the history
reCaptcha implemented on report dataset form
  • Loading branch information
Mikanebu committed Jun 12, 2023
2 parents 12259a1 + 52c671d commit 40227e2
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 10 deletions.
29 changes: 25 additions & 4 deletions ckanext/nhs/controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging

import requests
from six import string_types
from urllib import urlencode
from flask.views import MethodView
Expand Down Expand Up @@ -265,6 +265,18 @@ def followed_organizations():
_datasets_or_groups_followed_by_user('organization')
return render('user/followed_organizations.html', extra_vars={'user_dict':c.user_dict})

def _reCapatcha_verify(response_token):
secret_key = config.get('ckanext.nhs.recaptcha_secret_key')

response = requests.post('https://www.google.com/recaptcha/api/siteverify', data={
'secret': secret_key,
'response': response_token
})

return response.json()



class SelfDelete(MethodView):
'''Delete self account'''

Expand Down Expand Up @@ -318,18 +330,27 @@ def post(self, id):
'user': c.user,
'auth_user_obj': c.userobj
}

data_dict = {'id': id}

recaptcha = request.form.get('g-recaptcha-token')
if recaptcha:
try:
captcha_result = _reCapatcha_verify(recaptcha)
log.info('Captcha Result: {0}'.format(captcha_result))
if captcha_result['success'] == False:
raise Exception
except Exception as e:
h.flash_error(_('Unable to report dataset, Please verify that you are not a robot.'))
return h.redirect_to(controller='package', action='read', id=data_dict['id'])


report_dict = {
'issue_type' : request.form.get('type'),
'issue_description' : request.form.get('description'),
'email' : request.form.get('email', False)
}

try:
#if not c.user:
# raise NotAuthorized
mail_dataset_report(data_dict['id'], report_dict)
h.flash_success(_('Thank you for reporting your issue. We will review and respond shortly'))
return h.redirect_to(controller='package', action='read', id=data_dict['id'])
Expand Down
17 changes: 14 additions & 3 deletions ckanext/nhs/fanstatic/css/nhs.css
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,19 @@ a.btn-primary.active.focus {
height: 40px;
}

.captcha-disclaimer {
font-size: 14px !important;
color: #686868;
padding-bottom: 10px;
}
.captcha-disclaimer a {
font-size: 14px !important;
}

.grecaptcha-badge {
visibility: hidden;
}

.hero {
position: relative;
color: #fff;
Expand Down Expand Up @@ -2342,6 +2355,4 @@ blockquote {
padding-top: 20px;
align-content: flex-start !important;
}
}

/*# sourceMappingURL=nhs.css.map */
}
8 changes: 7 additions & 1 deletion ckanext/nhs/fanstatic/css/nhs.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ckanext/nhs/fanstatic/css/nhs.min.css

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions ckanext/nhs/fanstatic/js/reCaptcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
ckan.module('reCaptcha', function (jQuery) {
return {
options: {
sitekey: null,
},
initialize: function () {
var recaptcha = document.createElement('script');
recaptcha.src = 'https://www.google.com/recaptcha/api.js?render=' + this.options.sitekey;
recaptcha.async = true;
recaptcha.defer = true;
document.body.appendChild(recaptcha);
$.proxyAll(this, /_on/);

// onClick event
this.el.on('click', this._onClick);

// hidden input field with the token
this.el.before('<input type="hidden" name="g-recaptcha-token" value="">');

},
_onClick: function (event) {
event.preventDefault();
var module = this;
grecaptcha.ready(function (module) {
return function () {
grecaptcha.execute(module.options.sitekey, { action: 'submit' }).then(function (token) {
jQuery('input[name="g-recaptcha-token"]').val(token);
module.el.closest('form').submit();
});
};
}(module));
}
};
});
1 change: 1 addition & 0 deletions ckanext/nhs/fanstatic/resource.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ main =
nhs_js =
js/nhsuk.js
js/nhs.js
js/reCaptcha.js
js/activity-stream.js
js/resource-reorder.js
js/vendors/jquery-dataTables-min.js
Expand Down
15 changes: 15 additions & 0 deletions ckanext/nhs/fanstatic/sass/theme/_general.scss
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,18 @@ a.btn-primary.active.focus {
}
}
}

// reCAPTCHA styling

.captcha-disclaimer {
font-size: 14px !important;
color: #686868;
padding-bottom: 10px;
a {
font-size: 14px !important;
}
}

.grecaptcha-badge {
visibility: hidden;
}
6 changes: 6 additions & 0 deletions ckanext/nhs/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,10 @@ def get_foi_org_id():
return foi_group.id
except:
return None

def get_recaptcha_site_key():
try:
return config.get('ckanext.nhs.recaptcha_site_key', '')
except:
return None

1 change: 1 addition & 0 deletions ckanext/nhs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def get_helpers(self):
'get_dataset_report_type' : helpers.get_dataset_report_type,
'API_enabled': helpers.API_enabled,
'get_foi_org_id': helpers.get_foi_org_id,
'get_recaptcha_site_key': helpers.get_recaptcha_site_key
}

# IRoutes
Expand Down
7 changes: 6 additions & 1 deletion ckanext/nhs/templates/package/snippets/report_pop.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,13 @@ <h2 class="modal-title" id="reportDatasetModal">Report an issue</h2>
address.')) }}
{% endcall %}

<p class="captcha-disclaimer">This site is protected by reCAPTCHA and the Google <a href="https://policies.google.com/privacy" target="_blank">Privacy Policy</a> and <a href="https://policies.google.com/terms" target="_blank">Terms of Service</a> apply.</p>

<div class="action" style="">
<button type="submit" class="btn btn-primary" style="text-align: right;" aria-label="Submit">
<button type="submit" class="btn btn-primary" style="text-align: right;" aria-label="Submit"
data-module="reCaptcha"
data-module-sitekey="{{ h.get_recaptcha_site_key() }}"
>
{{ _('Submit') }}</button>
<button type="button" class="btn btn-default btn-cancel clsoe" data-dismiss="modal"
aria-label="Close" style="text-align: right;">Cancel</button>
Expand Down

0 comments on commit 40227e2

Please sign in to comment.