Skip to content

Commit

Permalink
[IMP] Adding recaptcha validation in login, password reset , signup, …
Browse files Browse the repository at this point in the history
…widget form website
  • Loading branch information
edescalona committed Dec 6, 2024
1 parent b234960 commit 5425df3
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 29 deletions.
File renamed without changes.

This file was deleted.

15 changes: 13 additions & 2 deletions website_recaptcha_v2_form/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "Website reCAPTCHA v2 login",
"name": "Website reCAPTCHA v2 form",
"version": "16.0.1.0.0",
"category": "Website",
"depends": ["web", "website_recaptcha_v2"],
"depends": ["web", "auth_signup", "website", "website_recaptcha_v2"],
"author": """
Binhex,
Odoo Community Association (OCA)
Expand All @@ -12,6 +12,17 @@
"summary": "Module to add reCAPTCHA v2 to the login 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,
}
86 changes: 64 additions & 22 deletions website_recaptcha_v2_form/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,78 @@
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 BinhexHome(Home):
def verify_recaptcha_v2(self, args=None, kw=None, template="", values=None):
Website = request.env["website"].sudo()
try:
request.env["ir.http"]._auth_method_public()
valid = Website.get_current_website().valid_recaptcha(values)

Check warning on line 20 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L17-L20

Added lines #L17 - L20 were not covered by tests
if valid:
if template == "web.login":
return super().web_login(values.get("redirect", ""), **kw)

Check warning on line 23 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L23

Added line #L23 was not covered by tests
if template in ("auth_signup.reset_password", "auth_signup.signup"):
return True
except AccessDenied as e:
values.update(

Check warning on line 27 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L25-L27

Added lines #L25 - L27 were not covered by tests
{
"error": str(
e.args[0] if len(e.args) > 0 else _("Recaptcha is not valid.")
)
}
)
response = request.render(template, values)
response.headers["X-Frame-Options"] = "SAMEORIGIN"
response.headers["Content-Security-Policy"] = "frame-ancestors 'self'"
return response

Check warning on line 37 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L34-L37

Added lines #L34 - L37 were not covered by tests

@http.route("/web/login", type="http", auth="none")
def web_login(self, redirect=None, **kw):
Website = request.env["website"].sudo()
values = {
k: v for k, v in request.params.items() if k in SIGN_UP_REQUEST_PARAMS
}
if request.httprequest.method == "POST":
request.env["ir.http"]._auth_method_public()
try:
valid = Website.get_current_website().valid_recaptcha(kw)
if valid:
return super().web_login(redirect, **kw)
except AccessDenied as e:
values.update(
{
"error": str(
e.args[0]
if len(e.args) > 0
else _("Recaptcha is not valid.")
)
}
values = {

Check warning on line 42 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L42

Added line #L42 was not covered by tests
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(

Check warning on line 52 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L52

Added line #L52 was not covered by tests
kw=kw, template="web.login", values=values
)
response = request.render("web.login", values)
response.headers["X-Frame-Options"] = "SAMEORIGIN"
response.headers["Content-Security-Policy"] = "frame-ancestors 'self'"
return response
return super().web_login(redirect, **kw)

Check warning on line 55 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L55

Added line #L55 was not covered by tests


class BinhexAuthSignupHome(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()

Check warning on line 63 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L63

Added line #L63 was not covered by tests
if request.httprequest.method == "POST":
valid = self.verify_recaptcha_v2(

Check warning on line 65 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L65

Added line #L65 was not covered by tests
kw=kw, template="auth_signup.reset_password", values=qcontext, args=args
)
if not isinstance(valid, bool):
return valid
return super().web_auth_reset_password(*args, **kw)

Check warning on line 70 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L69-L70

Added lines #L69 - L70 were not covered by tests

@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()

Check warning on line 74 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L74

Added line #L74 was not covered by tests
if request.httprequest.method == "POST":
valid = self.verify_recaptcha_v2(

Check warning on line 76 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L76

Added line #L76 was not covered by tests
kw=kw, template="auth_signup.signup", values=qcontext, args=args
)
if not isinstance(valid, bool):
return valid
return super().web_auth_signup(*args, **kw)

Check warning on line 81 in website_recaptcha_v2_form/controllers/main.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/controllers/main.py#L80-L81

Added lines #L80 - L81 were not covered by tests
10 changes: 7 additions & 3 deletions website_recaptcha_v2_form/models/website.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from odoo import models
from odoo import api, models
from odoo.exceptions import AccessDenied


Expand All @@ -14,8 +14,12 @@ class Website(models.Model):
kw: Data sent from the form
"""

def valid_recaptcha(self, kw):
valid, message = self.is_recaptcha_v2_valid(kw)
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

Check warning on line 25 in website_recaptcha_v2_form/models/website.py

View check run for this annotation

Codecov / codecov/patch

website_recaptcha_v2_form/models/website.py#L25

Added line #L25 was not covered by tests
5 changes: 5 additions & 0 deletions website_recaptcha_v2_form/static/src/css/recaptcha.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website_recaptcha_v2_form/static/src/css/recaptcha.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions website_recaptcha_v2_form/static/src/scss/recaptcha.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
div.s_website_form_recaptcha_v2 {
> div.g-recaptcha {
margin-left: 18% !important;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
odoo.define("website_recaptcha_v2_form.form_editor", function (require) {
"use strict";

var options = require("web_editor.snippets.options");
const core = require("web.core");
const rpc = require("web.rpc");
const qweb = core.qweb;
require("website.form_editor");

options.registry.WebsiteFormEditor.include({
willStart: async function () {
var res = this._super(...arguments);
this.recaptcha_site_key = await rpc.query({
model: "website",
method: "get_recaptcha_v2_site_key",
});
return res;
},
toggleRecaptchaV2: async function () {
const recaptchaV2 = this.$target[0].querySelector(
".s_website_form_recaptcha_v2"
);
if (recaptchaV2) {
recaptchaV2.remove();
} else {
const legal = qweb.render("website_recaptcha_v2_form.recaptcha_v2", {
recaptcha_site_key: this.recaptcha_site_key,
});
this.$target.find(".s_website_form_submit").before(legal);
}
},
_computeWidgetState: function (methodName, params) {
switch (methodName) {
case "toggleRecaptchaV2":
return (
!this.$target[0].querySelector(
".s_website_form_recaptcha_v2"
) || ""
);
}
return this._super(...arguments);
},
});
});
11 changes: 11 additions & 0 deletions website_recaptcha_v2_form/static/src/xml/website_form_editor.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">

<t t-name="website_recaptcha_v2_form.recaptcha_v2">
<div class="col-12 s_website_form_recaptcha_v2" data-name="Recaptcha v2 Legal">
<div class="g-recaptcha" t-att-data-sitekey="recaptcha_site_key" />
<script src="https://www.recaptcha.net/recaptcha/api.js" async="1" />
</div>
</t>

</templates>
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template id="binhex_login" inherit_id="web.login">
<template id="binhex_signup" inherit_id="auth_signup.signup">
<xpath expr="//p[hasclass('alert-danger')]" position="before">
<t t-call="website_recaptcha_v2.recaptcha_widget" />
</xpath>
</template>

<template id="binhex_reset_password" inherit_id="auth_signup.reset_password">
<xpath expr="//p[hasclass('alert-danger')]" position="before">
<t t-call="website_recaptcha_v2.recaptcha_widget" />
</xpath>
Expand Down
16 changes: 16 additions & 0 deletions website_recaptcha_v2_form/views/s_website_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<odoo>
<template id="s_website_form_options" inherit_id="website.snippet_options">
<xpath expr="//div[@data-js='WebsiteFormEditor']" position="inside">
<t
t-set="recaptcha_v2_public_key"
t-value="request.env['ir.config_parameter'].sudo().get_param('recaptcha_v2_secret_key')"
/>
<we-checkbox
string="Show reCaptcha v2"
data-toggle-recaptcha-v2=""
data-no-preview="true"
/>
</xpath>
</template>

</odoo>

0 comments on commit 5425df3

Please sign in to comment.