From 2a074785b5bf158376bb6f1d794e303c2a33b748 Mon Sep 17 00:00:00 2001 From: Vincent Hatakeyama Date: Fri, 18 Oct 2024 15:57:47 +0200 Subject: [PATCH] [ADD] auth_oauth_autoredirect --- auth_oauth_autoredirect/__init__.py | 1 + auth_oauth_autoredirect/__manifest__.py | 17 +++++ .../controllers/__init__.py | 3 + auth_oauth_autoredirect/controllers/main.py | 69 +++++++++++++++++++ auth_oauth_autoredirect/models/__init__.py | 1 + .../models/auth_oauth_provider.py | 15 ++++ auth_oauth_autoredirect/readme/CONFIGURE.rst | 6 ++ .../readme/CONTRIBUTORS.rst | 3 + .../readme/DESCRIPTION.rst | 1 + .../views/auth_oauth_provider.xml | 13 ++++ .../odoo/addons/auth_oauth_autoredirect | 1 + setup/auth_oauth_autoredirect/setup.py | 6 ++ 12 files changed, 136 insertions(+) create mode 100644 auth_oauth_autoredirect/__init__.py create mode 100644 auth_oauth_autoredirect/__manifest__.py create mode 100644 auth_oauth_autoredirect/controllers/__init__.py create mode 100644 auth_oauth_autoredirect/controllers/main.py create mode 100644 auth_oauth_autoredirect/models/__init__.py create mode 100644 auth_oauth_autoredirect/models/auth_oauth_provider.py create mode 100644 auth_oauth_autoredirect/readme/CONFIGURE.rst create mode 100644 auth_oauth_autoredirect/readme/CONTRIBUTORS.rst create mode 100644 auth_oauth_autoredirect/readme/DESCRIPTION.rst create mode 100644 auth_oauth_autoredirect/views/auth_oauth_provider.xml create mode 120000 setup/auth_oauth_autoredirect/odoo/addons/auth_oauth_autoredirect create mode 100644 setup/auth_oauth_autoredirect/setup.py diff --git a/auth_oauth_autoredirect/__init__.py b/auth_oauth_autoredirect/__init__.py new file mode 100644 index 0000000000..72d3ea60a8 --- /dev/null +++ b/auth_oauth_autoredirect/__init__.py @@ -0,0 +1 @@ +from . import controllers, models diff --git a/auth_oauth_autoredirect/__manifest__.py b/auth_oauth_autoredirect/__manifest__.py new file mode 100644 index 0000000000..e4e91d2cba --- /dev/null +++ b/auth_oauth_autoredirect/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright (C) 2024 XCG Consulting +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "OAuth2 Authentication Autoredirect", + "version": "16.0.1.0.0", + "category": "Hidden/Tools", + "author": "XCG Consulting, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/server-auth", + "license": "AGPL-3", + "depends": ["auth_oauth"], + "data": [ + "views/auth_oauth_provider.xml", + ], + "installable": True, + "auto_install": False, +} diff --git a/auth_oauth_autoredirect/controllers/__init__.py b/auth_oauth_autoredirect/controllers/__init__.py new file mode 100644 index 0000000000..2a3e5d5654 --- /dev/null +++ b/auth_oauth_autoredirect/controllers/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import main diff --git a/auth_oauth_autoredirect/controllers/main.py b/auth_oauth_autoredirect/controllers/main.py new file mode 100644 index 0000000000..dc75fa87c7 --- /dev/null +++ b/auth_oauth_autoredirect/controllers/main.py @@ -0,0 +1,69 @@ +# Copyright (C) 2020 GlodoUK +# Copyright (C) 2010-2016, 2022-2024 XCG Consulting +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import werkzeug.utils + +from odoo import http +from odoo.http import request + +from odoo.addons.auth_oauth.controllers.main import OAuthLogin +from odoo.addons.web.controllers.utils import ensure_db + + +# ---------------------------------------------------------- +# Controller +# ---------------------------------------------------------- +class OAuthAutoredirectLogin(OAuthLogin): + """OAuth controller with autoredirect added""" + + def list_providers_with_autoredirect(self): + providers = self.list_providers() + saml_providers = { + search_read["id"] + for search_read in request.env["auth.oauth.provider"] + .sudo() + .search_read([("autoredirect", "=", True)], ["id"]) + } + return [provider for provider in providers if provider["id"] in saml_providers] + + def _oauth_autoredirect(self): + # automatically redirect if any provider is set up to do that + autoredirect_providers = self.list_providers_with_autoredirect() + # do not redirect if asked too or if an error has been found + disable_autoredirect = ( + "disable_autoredirect" in request.params or "error" in request.params + ) + if autoredirect_providers and not disable_autoredirect: + return werkzeug.utils.redirect( + autoredirect_providers[0]["auth_link"], + 303, + ) + return None + + @http.route() + def web_client(self, s_action=None, **kw): + if not request.session.uid: + result = self._oauth_autoredirect() + if result: + return result + return super().web_client(s_action, **kw) + + @http.route() + def web_login(self, *args, **kw): + ensure_db() + # copied from super + if ( + request.httprequest.method == "GET" + and request.session.uid + and request.params.get("redirect") + ): + # Redirect if already logged in and redirect param is present + return request.redirect(request.params.get("redirect")) + + if request.httprequest.method == "GET": + result = self._oauth_autoredirect() + if result: + return result + + return super().web_login(*args, **kw) diff --git a/auth_oauth_autoredirect/models/__init__.py b/auth_oauth_autoredirect/models/__init__.py new file mode 100644 index 0000000000..4bc62d3ab5 --- /dev/null +++ b/auth_oauth_autoredirect/models/__init__.py @@ -0,0 +1 @@ +from . import auth_oauth_provider diff --git a/auth_oauth_autoredirect/models/auth_oauth_provider.py b/auth_oauth_autoredirect/models/auth_oauth_provider.py new file mode 100644 index 0000000000..5cc4558501 --- /dev/null +++ b/auth_oauth_autoredirect/models/auth_oauth_provider.py @@ -0,0 +1,15 @@ +# Copyright 2024 XCG Consulting +# License: AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import fields, models + + +class AuthOauthProvider(models.Model): + _inherit = "auth.oauth.provider" + + autoredirect = fields.Boolean( + "Automatic Redirection", + default=False, + help="Only the provider with the higher priority will be automatically " + "redirected", + ) diff --git a/auth_oauth_autoredirect/readme/CONFIGURE.rst b/auth_oauth_autoredirect/readme/CONFIGURE.rst new file mode 100644 index 0000000000..47d624f345 --- /dev/null +++ b/auth_oauth_autoredirect/readme/CONFIGURE.rst @@ -0,0 +1,6 @@ +If all the users have a oauth id in a single provider, you can set automatic redirection +in the provider settings. The autoredirection will only be done on the active provider +with the highest priority. It is still possible to access the login without redirection +by using the query parameter ``disable_autoredirect``, as in +``https://example.com/web/login?disable_autoredirect=`` The login is also displayed if +there is an error with login, in order to display any error message. diff --git a/auth_oauth_autoredirect/readme/CONTRIBUTORS.rst b/auth_oauth_autoredirect/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..37addc6417 --- /dev/null +++ b/auth_oauth_autoredirect/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `XCG Consulting `__: + + * Vincent Hatakeyama diff --git a/auth_oauth_autoredirect/readme/DESCRIPTION.rst b/auth_oauth_autoredirect/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..134d9fbb0d --- /dev/null +++ b/auth_oauth_autoredirect/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module adds autoredirection to an oauth provider. diff --git a/auth_oauth_autoredirect/views/auth_oauth_provider.xml b/auth_oauth_autoredirect/views/auth_oauth_provider.xml new file mode 100644 index 0000000000..ba6df843d8 --- /dev/null +++ b/auth_oauth_autoredirect/views/auth_oauth_provider.xml @@ -0,0 +1,13 @@ + + + + auth.oidc.provider.form + auth.oauth.provider + + + + + + + + diff --git a/setup/auth_oauth_autoredirect/odoo/addons/auth_oauth_autoredirect b/setup/auth_oauth_autoredirect/odoo/addons/auth_oauth_autoredirect new file mode 120000 index 0000000000..b95497b6aa --- /dev/null +++ b/setup/auth_oauth_autoredirect/odoo/addons/auth_oauth_autoredirect @@ -0,0 +1 @@ +../../../../auth_oauth_autoredirect \ No newline at end of file diff --git a/setup/auth_oauth_autoredirect/setup.py b/setup/auth_oauth_autoredirect/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/auth_oauth_autoredirect/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)