Repository navigation
[16.0][ADD] website_recaptcha_v2_form #1076
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
Open
edescalona
wants to merge
1
commit into
OCA:16.0
Choose a base branch
from
BinhexTeam:16.0-add-website_recaptcha_v2_form
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
setup/website_recaptcha_v2_form/odoo/addons/website_recaptcha_v2_form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../../../website_recaptcha_v2_form |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import setuptools | ||
|
|
||
| setuptools.setup( | ||
| setup_requires=['setuptools-odoo'], | ||
| odoo_addon=True, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| ========================= | ||
| Website reCAPTCHA v2 form | ||
| ========================= | ||
|
|
||
| .. | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! This file is generated by oca-gen-addon-readme !! | ||
| !! changes will be overwritten. !! | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! source digest: sha256:82c3625280a3850676447d821ae41a770088a52928f19dc3d67a5aa695b2e677 | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
|
||
| .. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png | ||
| :target: https://odoo-community.org/page/development-status | ||
| :alt: Beta | ||
| .. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png | ||
| :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html | ||
| :alt: License: AGPL-3 | ||
| .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fhttps://github.com/BinhexTeam/website.git-lightgray.png?logo=github | ||
| :target: https://github.com/OCA/https://github.com/BinhexTeam/website.git/tree/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form | ||
| :alt: OCA/https://github.com/BinhexTeam/website.git | ||
| .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png | ||
| :target: https://translation.odoo-community.org/projects/https://github.com/BinhexTeam/website.git-16-0-add-website_recaptcha_v2_form/https://github.com/BinhexTeam/website.git-16-0-add-website_recaptcha_v2_form-website_recaptcha_v2_form | ||
| :alt: Translate me on Weblate | ||
| .. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png | ||
| :target: https://runboat.odoo-community.org/builds?repo=OCA/https://github.com/BinhexTeam/website.git&target_branch=16.0-add-website_recaptcha_v2_form | ||
| :alt: Try me on Runboat | ||
|
|
||
| |badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
|
||
| This module allows you to use the recaptcha in its version 2 for the | ||
| login, password reset and signup views; it is also configured as needed | ||
| in the Form type snippets that appear on the Website. | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| Configuration | ||
| ============= | ||
|
|
||
| To configure this module, you need to: | ||
|
|
||
| #. Go to **Website > Configuration > Settings**. | ||
|
|
||
| #. Search 'reCAPTCHA v2' option. | ||
|
|
||
| |reCaptcha v2| | ||
|
|
||
| #. Click the link `Get reCAPTCHA v2 | ||
| keys <https://www.google.com/recaptcha/admin>`__ to generate the keys | ||
| needed to use the recaptcha. | ||
|
|
||
| |Get reCAPTCHA v2 keys| | ||
|
|
||
| #. Fill in the generated **Site Key** and **Secret key**. | ||
|
|
||
| |reCAPTCHA v2 keys| | ||
|
|
||
| #. Enable recaptcha in form website. |reCAPTCHA v2 enable website| | ||
|
|
||
| .. |reCaptcha v2| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img.png | ||
| .. |Get reCAPTCHA v2 keys| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img_1.png | ||
| .. |reCAPTCHA v2 keys| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img_2.png | ||
| .. |reCAPTCHA v2 enable website| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img_3.png | ||
|
|
||
| Bug Tracker | ||
| =========== | ||
|
|
||
| Bugs are tracked on `GitHub Issues <https://github.com/OCA/https://github.com/BinhexTeam/website.git/issues>`_. | ||
| In case of trouble, please check there if your issue has already been reported. | ||
| If you spotted it first, help us to smash it by providing a detailed and welcomed | ||
| `feedback <https://github.com/OCA/https://github.com/BinhexTeam/website.git/issues/new?body=module:%20website_recaptcha_v2_form%0Aversion:%2016.0-add-website_recaptcha_v2_form%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_. | ||
|
|
||
| Do not contact contributors directly about support or help with technical issues. | ||
|
|
||
| Credits | ||
| ======= | ||
|
|
||
| Authors | ||
| ------- | ||
|
|
||
| * Binhex | ||
|
|
||
| Contributors | ||
| ------------ | ||
|
|
||
| -``BINHEX <https://binhex.cloud>``\ \_: | ||
|
|
||
| - Edilio Escalona Almira | ||
|
|
||
| Maintainers | ||
| ----------- | ||
|
|
||
| This module is maintained by the OCA. | ||
|
|
||
| .. image:: https://odoo-community.org/logo.png | ||
| :alt: Odoo Community Association | ||
| :target: https://odoo-community.org | ||
|
|
||
| OCA, or the Odoo Community Association, is a nonprofit organization whose | ||
| mission is to support the collaborative development of Odoo features and | ||
| promote its widespread use. | ||
|
|
||
| This module is part of the `OCA/https://github.com/BinhexTeam/website.git <https://github.com/OCA/https://github.com/BinhexTeam/website.git/tree/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form>`_ project on GitHub. | ||
|
|
||
| You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import models | ||
| from . import controllers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "name": "Website reCAPTCHA v2 form", | ||
| "version": "16.0.1.0.0", | ||
| "category": "Website", | ||
| "depends": ["web", "auth_signup", "website", "website_recaptcha_v2"], | ||
| "author": """ | ||
| Binhex, | ||
| Odoo Community Association (OCA) | ||
| """, | ||
| "license": "AGPL-3", | ||
| "website": "https://github.com/OCA/website", | ||
| "summary": """ Module that allows you to use recaptcha v2 for login, password reset, | ||
| signup and snippet form on the website. | ||
| """, | ||
| "data": [ | ||
| "views/webclient_templates.xml", | ||
| "views/auth_signup_login_templates.xml", | ||
| "views/s_website_form.xml", | ||
| ], | ||
| "assets": { | ||
| "website.assets_wysiwyg": [ | ||
| "website_recaptcha_v2_form/static/src/xml/website_form_editor.xml", | ||
| "website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js", | ||
| ], | ||
| "web.assets_frontend": [ | ||
| "website_recaptcha_v2_form/static/src/css/recaptcha.css", | ||
| ], | ||
| }, | ||
| "installable": True, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from . import main | ||
| from . import form |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Part of Odoo. See LICENSE file for full copyright and licensing details. | ||
|
|
||
| import json | ||
|
|
||
| from odoo import http | ||
|
|
||
| from odoo.addons.website.controllers.form import WebsiteForm | ||
|
|
||
| from .main import RecaptchaHome | ||
|
|
||
|
|
||
| class WebsiteRecaptchaForm(WebsiteForm): | ||
| @http.route( | ||
| "/website/form/<string:model_name>", | ||
| type="http", | ||
| auth="public", | ||
| methods=["POST"], | ||
| website=True, | ||
| csrf=False, | ||
| ) | ||
| def website_form(self, model_name, **kwargs): | ||
| if kwargs.get("recaptcha_enabled", False): | ||
| valid = RecaptchaHome.verify_recaptcha_v2(self, values=kwargs) | ||
| if not isinstance(valid, bool): | ||
| return json.dumps( | ||
| { | ||
| "error": valid, | ||
| } | ||
| ) | ||
| return super().website_form(model_name, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import logging | ||
|
|
||
| from odoo import _, http | ||
| from odoo.exceptions import AccessDenied | ||
| from odoo.http import request | ||
|
|
||
| from odoo.addons.auth_signup.controllers.main import AuthSignupHome | ||
| from odoo.addons.web.controllers.home import SIGN_UP_REQUEST_PARAMS, Home | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| SIGN_UP_REQUEST_PARAMS.add("g-recaptcha-response") | ||
|
|
||
|
|
||
| class RecaptchaHome(Home): | ||
| def verify_recaptcha_v2(self, kw=None, template="", values=None): | ||
| Website = request.env["website"].sudo() | ||
| try: | ||
| request.env["ir.http"]._auth_method_public() | ||
| Website.get_current_website().valid_recaptcha(values) | ||
| if template == "web.login": | ||
| return super().web_login(values.get("redirect", ""), **kw) | ||
| return True | ||
| except AccessDenied as e: | ||
| message_error = str( | ||
| e.args[0] if len(e.args) > 0 else _("Recaptcha is not valid.") | ||
| ) | ||
| if template in ( | ||
| "web.login", | ||
| "auth_signup.reset_password", | ||
| "auth_signup.signup", | ||
| ): | ||
| values.update({"error": message_error}) | ||
| response = request.render(template, values) | ||
| response.headers["X-Frame-Options"] = "SAMEORIGIN" | ||
| response.headers["Content-Security-Policy"] = "frame-ancestors 'self'" | ||
| return response | ||
| else: | ||
| return message_error | ||
|
|
||
| @http.route("/web/login", type="http", auth="none") | ||
| def web_login(self, redirect=None, **kw): | ||
| if request.httprequest.method == "POST": | ||
| values = { | ||
| k: v for k, v in request.params.items() if k in SIGN_UP_REQUEST_PARAMS | ||
| } | ||
| # Checking that if the request comes from the creation of the account, | ||
| # that the recaptcha is not checked again to avoid errors. | ||
|
|
||
| if ( | ||
| values.get("confirm_password", "") == "" | ||
| and request.httprequest.url.find("web/signup") == -1 | ||
| ): | ||
| return self.verify_recaptcha_v2( | ||
| kw=kw, template="web.login", values=values | ||
| ) | ||
| return super().web_login(redirect, **kw) | ||
|
|
||
|
|
||
| class RecaptchaAuthSignupHome(AuthSignupHome): | ||
| @http.route( | ||
| "/web/reset_password", type="http", auth="public", website=True, sitemap=False | ||
| ) | ||
| def web_auth_reset_password(self, *args, **kw): | ||
| qcontext = self.get_auth_signup_qcontext() | ||
| if request.httprequest.method == "POST": | ||
| valid = self.verify_recaptcha_v2( | ||
| kw=kw, template="auth_signup.reset_password", values=qcontext | ||
| ) | ||
| if not isinstance(valid, bool): | ||
| return valid | ||
| return super().web_auth_reset_password(*args, **kw) | ||
|
|
||
| @http.route("/web/signup", type="http", auth="public", website=True, sitemap=False) | ||
| def web_auth_signup(self, *args, **kw): | ||
| qcontext = self.get_auth_signup_qcontext() | ||
| if request.httprequest.method == "POST": | ||
| valid = self.verify_recaptcha_v2( | ||
| template="auth_signup.signup", values=qcontext | ||
| ) | ||
| if not isinstance(valid, bool): | ||
| return valid | ||
| return super().web_auth_signup(*args, **kw) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Translation of Odoo Server. | ||
| # This file contains the translation of the following modules: | ||
| # * binhex_website_recaptcha_v2 | ||
| # | ||
| msgid "" | ||
| msgstr "" | ||
| "Project-Id-Version: Odoo Server 16.0\n" | ||
| "Report-Msgid-Bugs-To: \n" | ||
| "POT-Creation-Date: 2024-12-02 16:23+0000\n" | ||
| "PO-Revision-Date: 2024-12-02 16:23+0000\n" | ||
| "Last-Translator: \n" | ||
| "Language-Team: \n" | ||
| "MIME-Version: 1.0\n" | ||
| "Content-Type: text/plain; charset=UTF-8\n" | ||
| "Content-Transfer-Encoding: \n" | ||
| "Plural-Forms: \n" | ||
|
|
||
| #. module: binhex_website_recaptcha_v2 | ||
| #. odoo-python | ||
| #: code:addons/binhex_website_recaptcha_v2/controllers/main.py:0 | ||
| #, python-format | ||
| msgid "Recaptcha is not valid." | ||
| msgstr "Recaptcha no es válido." | ||
|
|
||
| #. module: binhex_website_recaptcha_v2 | ||
| #: model:ir.model,name:binhex_website_recaptcha_v2.model_website | ||
| msgid "Website" | ||
| msgstr "Sitio web" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import website |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| from odoo import api, models | ||
| from odoo.exceptions import AccessDenied | ||
|
|
||
|
|
||
| class Website(models.Model): | ||
| _inherit = "website" | ||
|
|
||
| def valid_recaptcha(self, values): | ||
| valid, message = self.is_recaptcha_v2_valid(values) | ||
| if not valid: | ||
| raise AccessDenied(message) | ||
| return True | ||
|
|
||
| @api.model | ||
| def get_recaptcha_v2_site_key(self): | ||
| return self.sudo().get_current_website().recaptcha_v2_site_key | ||
|
|
||
| def update_recaptcha_v2_site_key(self): | ||
| views_recaptcha = ( | ||
| self.env["ir.ui.view"] | ||
| .sudo() | ||
| .search( | ||
| [ | ||
| ("arch_db", "ilike", 'class="g-recaptcha"'), | ||
| ("website_id", "!=", False), | ||
| ] | ||
| ) | ||
| ) | ||
| if views_recaptcha: | ||
| for view in views_recaptcha: | ||
| site_key_old = view.arch_db.split('data-sitekey="') | ||
| if len(site_key_old) > 1: | ||
| site_key_old = site_key_old[1].split('"')[0] | ||
| if site_key_old: | ||
| div_start = '<div class="g-recaptcha" data-sitekey=' | ||
| div_end = 'data-callback="callback_success_recaptcha"' | ||
| div_end += ' data-expired-callback="callback_expired_recaptcha"' | ||
| updated_arch = view.arch_db.replace( | ||
| '{}"{}" {}/>'.format(div_start, site_key_old, div_end), | ||
| '{} "{}" {}/>'.format( | ||
| div_start, self.recaptcha_v2_site_key, div_end | ||
| ), | ||
| ) | ||
| view.sudo().write({"arch": updated_arch}) | ||
|
|
||
| def write(self, vals): | ||
| res = super().write(vals) | ||
| if "recaptcha_v2_site_key" in vals: | ||
| self.update_recaptcha_v2_site_key() | ||
| return res | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| To configure this module, you need to: | ||
|
|
||
| #. Go to **Website > Configuration > Settings**. | ||
|
|
||
| #. Search 'reCAPTCHA v2' option. | ||
|
|
||
|  | ||
|
|
||
| #. Click the link [Get reCAPTCHA v2 keys](https://www.google.com/recaptcha/admin) | ||
| to generate the keys needed to use the recaptcha. | ||
|
|
||
|  | ||
|
|
||
| #. Fill in the generated **Site Key** and **Secret key**. | ||
|
|
||
|  | ||
|
edescalona marked this conversation as resolved.
|
||
|
|
||
| #. Enable recaptcha in form website. | ||
|  | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| -`BINHEX <https://binhex.cloud>`_: | ||
|
|
||
| - Edilio Escalona Almira |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| This module allows you to use the recaptcha in its version 2 for the login, | ||
| password reset and signup views; it is also configured as needed in the | ||
| Form type snippets that appear on the Website. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this wrapper method is not really necessary, as it doesn't accomplish much more than link an AccessDenied exception with the error message, which should already be considered a fail state.
=> What is the reasonning behind this wrapper, that can't be fulfilled using the return tuple of is_recaptcha_v2_valid ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Honeyxilia , thanks for your comments. Basically, I want to show the message returned by the recaptcha verification as access denied, and then capture the exception in the controller.