Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support: added recaptcha #98

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ APP_DEFAULT_SECURE_HEADERS = {
"blob:", # for pdf preview
# Add your own policies here (e.g. analytics)
],
"frame-src": [
"'self'",
"https://www.google.com/recaptcha/",
"https://recaptcha.google.com/recaptcha/",
],
"script-src": [
"'self'",
"https://www.google.com/recaptcha/",
"https://www.gstatic.com/recaptcha/",
],
},
'content_security_policy_report_only': False,
'content_security_policy_report_uri': None,
Expand Down Expand Up @@ -283,3 +293,6 @@ RDM_CUSTOM_FIELDS = [
# obo
RelationshipListCF(name='obo:RO_0002453'),
]

# Google recaptcha client key
RECAPTCHA_CLIENT_KEY = "CHANGE ME"
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { Form as SemanticForm, Button, Modal } from "semantic-ui-react";
import { http, TextField, TextAreaField, ToggleField } from "react-invenio-forms";
import { remove, set } from "lodash";
import CategoryDropdown from "./CategoryDropdown";
import ReCAPTCHA from "react-google-recaptcha";
import FileUploader from './FileUploader';

// TODO translations
// TODO implement error notification
// TODO implement recaptcha
// TODO required fields are highlighted by the component and not Formik

const requestConfig = {
Expand Down Expand Up @@ -76,7 +76,7 @@ class SupportForm extends Component {
if (apiResponse) {
const apiErrors = apiResponse.errors || [];
const deserializedErrors = this.deserializeFieldErrors(apiErrors);

// Highlight errors using formik
formikBag.setErrors(deserializedErrors);
errorMessage = apiResponse.message || errorMessage;
Expand All @@ -87,7 +87,6 @@ class SupportForm extends Component {
}
}


render() {
const {
categories,
Expand All @@ -96,7 +95,8 @@ class SupportForm extends Component {
userBrowser,
userPlatform,
userMail,
maxFileSize
maxFileSize,
recaptchaClientKey
} = this.props;

const defaultCategory = categories.length > 0 ? categories[0].key : '';
Expand All @@ -106,7 +106,8 @@ class SupportForm extends Component {
name: name,
category: defaultCategory,
sysInfo: false,
files: []
files: [],
recaptcha: recaptchaClientKey ? false : true
}

const sysInfo = `Browser: ${userBrowser} Operating System: ${userPlatform}`;
Expand Down Expand Up @@ -202,7 +203,17 @@ class SupportForm extends Component {
<label className="helptext">{sysInfo}</label>
</div>
</div>

{
recaptchaClientKey &&
<div className="field flex">
<ReCAPTCHA
sitekey={recaptchaClientKey}
onChange={(value) => {
setFieldValue('recaptcha', true);
}}
/>
</div>
}
<Modal.Actions className="label-padding">
<Button type="submit" positive>
Send Request
Expand All @@ -227,7 +238,8 @@ SupportForm.propTypes = {
maxFileSize: PropTypes.number,
descriptionMaxLength: PropTypes.number,
descriptionMinLength: PropTypes.number,
apiEndpoint: PropTypes.string.isRequired
apiEndpoint: PropTypes.string.isRequired,
recaptchaClientKey: PropTypes.string.isRequired
};

SubmitEvent.defaultProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const maxFileSize = parseInt(domContainer.dataset.maxFileSize);
const descriptionMaxLength = parseInt(domContainer.dataset.descriptionMaxLength);
const descriptionMinLength = parseInt(domContainer.dataset.descriptionMinLength);
const apiEndpoint = domContainer.dataset.apiEndpoint;
const recaptchaClientKey = domContainer.dataset.recaptchaClientKey;


ReactDOM.render(
<SupportForm
Expand All @@ -27,6 +29,7 @@ ReactDOM.render(
descriptionMaxLength={descriptionMaxLength}
descriptionMinLength={descriptionMinLength}
apiEndpoint={apiEndpoint}
recaptchaClientKey={recaptchaClientKey || ''}
>
</SupportForm>,
rootContainer
Expand Down
10 changes: 8 additions & 2 deletions site/zenodo_rdm/support/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ class Meta:
category = fields.String(required=True)
sysInfo = fields.Boolean()
files = fields.List(fields.Raw(type="file"))
recaptcha = fields.Boolean()

@validates("description")
def validate_description(self, data, **kwargs):
if len(data) < min_length_config:
raise ValidationError(
"Description length must be bigger than {} characters".format(
"Description length must be longer than {} characters".format(
min_length_config
)
)
if len(data) > max_length_config:
raise ValidationError(
"Description length must be smaller than {} characters".format(
"Description length must be shorter than {} characters".format(
max_length_config
)
)
Expand All @@ -50,6 +51,11 @@ def validate_category(self, data, **kwargs):
if data not in self.allowed_categories:
raise ValidationError("{} is not a valid support category".format(data))

@validates("recaptcha")
def validate_recaptcha(self, data, **kwards):
if data == False:
raise ValidationError("Recaptcha not completed")

@cached_property
def allowed_categories(self):
return [c.get("key") for c in loaded_categories]
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ <h2 class="ui header mb-10">Contact us</h2>
data-description-min-length = '{{ config.SUPPORT_DESCRIPTION_MIN_LENGTH }}';
data-description-max-length = '{{ config.SUPPORT_DESCRIPTION_MAX_LENGTH }}';
data-api-endpoint = '{{ config.SUPPORT_ENDPOINT }}';
data-recaptcha-client-key = '{{ config.RECAPTCHA_CLIENT_KEY }}';
>
</div>
{% endblock %}
Expand Down
1 change: 1 addition & 0 deletions site/zenodo_rdm/webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"react": "^16.13.0",
"react-dom": "^16.13.0",
"lodash": "^4.17.0",
"react-google-recaptcha": "^2.1.0"
},
# aliases={
# "@translations/zenodo_rdm": "translations",
Expand Down