diff --git a/auth_oauth_autologin/README.rst b/auth_oauth_autologin/README.rst new file mode 100644 index 0000000000..0d1fe627aa --- /dev/null +++ b/auth_oauth_autologin/README.rst @@ -0,0 +1,111 @@ +==================== +Auth Oauth Autologin +==================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:e76d4fe47a295d53f5a9c461e4e99e8aba62dd3f1841ab3c243b4a0b840b295a + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fserver--auth-lightgray.png?logo=github + :target: https://github.com/OCA/server-auth/tree/14.0/auth_oauth_autologin + :alt: OCA/server-auth +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/server-auth-14-0/server-auth-14-0-auth_oauth_autologin + :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/server-auth&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This modules implements an automatic redirection to the configured OAuth +provider login page, if there is one and only one enabled. This effectively +makes the regular Odoo login screen invisible in normal circumstances. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +Configure OAuth providers in Settings > Users and Companies, and make sure +there is one and only one that has both the enabled and automatic login flags +set. + +When this is done, users visiting the login page (/web/login), or being +redirected to it because they are not authenticated yet, will be redirected to +the identity provider login page instead of the regular Odoo login page. + +Be aware that this module does not actively prevent users from authenticating +with an login and password stored in the Odoo database. In some unusual +circumstances (such as identity provider errors), the regular Odoo login may +still be displayed. Securely disabling Odoo login and password, if needed, +should be the topic of another module. + +Also be aware that this has a possibly surprising effect on the logout menu +item. When the user logs out of Odoo, a redirect to the login page happens. The +login page in turn redirects to the identity provider, which, if the user is +already authenticated there, automatically logs the user back in Odoo, in a +fresh session. + +Usage +===== + +When configured, the Odoo login page redirects to the OAuth identify provider +for authentication and login in Odoo. To access the regular Odoo login page, +visit ``/web/login?no_autologin``. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ACSONE SA/NV + +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. + +.. |maintainer-sbidoul| image:: https://github.com/sbidoul.png?size=40px + :target: https://github.com/sbidoul + :alt: sbidoul + +Current `maintainer `__: + +|maintainer-sbidoul| + +This module is part of the `OCA/server-auth `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/auth_oauth_autologin/__init__.py b/auth_oauth_autologin/__init__.py new file mode 100644 index 0000000000..91c5580fed --- /dev/null +++ b/auth_oauth_autologin/__init__.py @@ -0,0 +1,2 @@ +from . import controllers +from . import models diff --git a/auth_oauth_autologin/__manifest__.py b/auth_oauth_autologin/__manifest__.py new file mode 100644 index 0000000000..3ae0e85d34 --- /dev/null +++ b/auth_oauth_autologin/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Auth Oauth Autologin", + "summary": """ + Automatically redirect to the OAuth provider for login""", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "maintainers": ["sbidoul"], + "website": "https://github.com/OCA/server-auth", + "depends": ["auth_oauth"], + "data": ["views/auth_oauth_provider.xml"], + "demo": [], +} diff --git a/auth_oauth_autologin/controllers/__init__.py b/auth_oauth_autologin/controllers/__init__.py new file mode 100644 index 0000000000..12a7e529b6 --- /dev/null +++ b/auth_oauth_autologin/controllers/__init__.py @@ -0,0 +1 @@ +from . import main diff --git a/auth_oauth_autologin/controllers/main.py b/auth_oauth_autologin/controllers/main.py new file mode 100644 index 0000000000..42a9bb28be --- /dev/null +++ b/auth_oauth_autologin/controllers/main.py @@ -0,0 +1,35 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import werkzeug + +from odoo import http + +from odoo.addons.auth_oauth.controllers.main import OAuthLogin + + +class OAuthAutoLogin(OAuthLogin): + def _autologin_disabled(self): + return ( + "no_autologin" in http.request.params + or "oauth_error" in http.request.params + or "error" in http.request.params + ) + + def _autologin_link(self): + providers = [p for p in self.list_providers() if p.get("autologin")] + if len(providers) == 1: + return providers[0].get("auth_link") + + @http.route() + def web_login(self, *args, **kw): + response = super().web_login(*args, **kw) + if not response.is_qweb: + # presumably a redirect already + return response + if self._autologin_disabled(): + return response + auth_link = self._autologin_link() + if not auth_link: + return response + return werkzeug.utils.redirect(auth_link, 303) diff --git a/auth_oauth_autologin/i18n/auth_oauth_autologin.pot b/auth_oauth_autologin/i18n/auth_oauth_autologin.pot new file mode 100644 index 0000000000..a591a6a485 --- /dev/null +++ b/auth_oauth_autologin/i18n/auth_oauth_autologin.pot @@ -0,0 +1,31 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * auth_oauth_autologin +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \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: auth_oauth_autologin +#: model:ir.model.fields,field_description:auth_oauth_autologin.field_auth_oauth_provider__autologin +msgid "Automatic Login" +msgstr "" + +#. module: auth_oauth_autologin +#: model:ir.model.fields,help:auth_oauth_autologin.field_auth_oauth_provider__autologin +msgid "" +"If exactly one enabled provider has this checked, the login screen redirects" +" to the OAuth provider." +msgstr "" + +#. module: auth_oauth_autologin +#: model:ir.model,name:auth_oauth_autologin.model_auth_oauth_provider +msgid "OAuth2 provider" +msgstr "" diff --git a/auth_oauth_autologin/models/__init__.py b/auth_oauth_autologin/models/__init__.py new file mode 100644 index 0000000000..4bc62d3ab5 --- /dev/null +++ b/auth_oauth_autologin/models/__init__.py @@ -0,0 +1 @@ +from . import auth_oauth_provider diff --git a/auth_oauth_autologin/models/auth_oauth_provider.py b/auth_oauth_autologin/models/auth_oauth_provider.py new file mode 100644 index 0000000000..6abefad5e9 --- /dev/null +++ b/auth_oauth_autologin/models/auth_oauth_provider.py @@ -0,0 +1,16 @@ +# Copyright 2021 ACSONE SA/NV +# 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" + + autologin = fields.Boolean( + string="Automatic Login", + help=( + "If exactly one enabled provider has this checked, " + "the login screen redirects to the OAuth provider." + ), + ) diff --git a/auth_oauth_autologin/readme/CONFIGURE.rst b/auth_oauth_autologin/readme/CONFIGURE.rst new file mode 100644 index 0000000000..99464d82ba --- /dev/null +++ b/auth_oauth_autologin/readme/CONFIGURE.rst @@ -0,0 +1,19 @@ +Configure OAuth providers in Settings > Users and Companies, and make sure +there is one and only one that has both the enabled and automatic login flags +set. + +When this is done, users visiting the login page (/web/login), or being +redirected to it because they are not authenticated yet, will be redirected to +the identity provider login page instead of the regular Odoo login page. + +Be aware that this module does not actively prevent users from authenticating +with an login and password stored in the Odoo database. In some unusual +circumstances (such as identity provider errors), the regular Odoo login may +still be displayed. Securely disabling Odoo login and password, if needed, +should be the topic of another module. + +Also be aware that this has a possibly surprising effect on the logout menu +item. When the user logs out of Odoo, a redirect to the login page happens. The +login page in turn redirects to the identity provider, which, if the user is +already authenticated there, automatically logs the user back in Odoo, in a +fresh session. diff --git a/auth_oauth_autologin/readme/DESCRIPTION.rst b/auth_oauth_autologin/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..24021db9af --- /dev/null +++ b/auth_oauth_autologin/readme/DESCRIPTION.rst @@ -0,0 +1,3 @@ +This modules implements an automatic redirection to the configured OAuth +provider login page, if there is one and only one enabled. This effectively +makes the regular Odoo login screen invisible in normal circumstances. diff --git a/auth_oauth_autologin/readme/USAGE.rst b/auth_oauth_autologin/readme/USAGE.rst new file mode 100644 index 0000000000..d2cf4360b6 --- /dev/null +++ b/auth_oauth_autologin/readme/USAGE.rst @@ -0,0 +1,3 @@ +When configured, the Odoo login page redirects to the OAuth identify provider +for authentication and login in Odoo. To access the regular Odoo login page, +visit ``/web/login?no_autologin``. diff --git a/auth_oauth_autologin/static/description/icon.png b/auth_oauth_autologin/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/auth_oauth_autologin/static/description/icon.png differ diff --git a/auth_oauth_autologin/static/description/index.html b/auth_oauth_autologin/static/description/index.html new file mode 100644 index 0000000000..920e222053 --- /dev/null +++ b/auth_oauth_autologin/static/description/index.html @@ -0,0 +1,445 @@ + + + + + + +Auth Oauth Autologin + + + +
+

Auth Oauth Autologin

+ + +

Beta License: AGPL-3 OCA/server-auth Translate me on Weblate Try me on Runboat

+

This modules implements an automatic redirection to the configured OAuth +provider login page, if there is one and only one enabled. This effectively +makes the regular Odoo login screen invisible in normal circumstances.

+

Table of contents

+ +
+

Configuration

+

Configure OAuth providers in Settings > Users and Companies, and make sure +there is one and only one that has both the enabled and automatic login flags +set.

+

When this is done, users visiting the login page (/web/login), or being +redirected to it because they are not authenticated yet, will be redirected to +the identity provider login page instead of the regular Odoo login page.

+

Be aware that this module does not actively prevent users from authenticating +with an login and password stored in the Odoo database. In some unusual +circumstances (such as identity provider errors), the regular Odoo login may +still be displayed. Securely disabling Odoo login and password, if needed, +should be the topic of another module.

+

Also be aware that this has a possibly surprising effect on the logout menu +item. When the user logs out of Odoo, a redirect to the login page happens. The +login page in turn redirects to the identity provider, which, if the user is +already authenticated there, automatically logs the user back in Odoo, in a +fresh session.

+
+
+

Usage

+

When configured, the Odoo login page redirects to the OAuth identify provider +for authentication and login in Odoo. To access the regular Odoo login page, +visit /web/login?no_autologin.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • ACSONE SA/NV
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

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.

+

Current maintainer:

+

sbidoul

+

This module is part of the OCA/server-auth project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/auth_oauth_autologin/tests/__init__.py b/auth_oauth_autologin/tests/__init__.py new file mode 100644 index 0000000000..f94c68ab45 --- /dev/null +++ b/auth_oauth_autologin/tests/__init__.py @@ -0,0 +1 @@ +from . import test_auth_oauth_autologin diff --git a/auth_oauth_autologin/tests/test_auth_oauth_autologin.py b/auth_oauth_autologin/tests/test_auth_oauth_autologin.py new file mode 100644 index 0000000000..cc8b786bf0 --- /dev/null +++ b/auth_oauth_autologin/tests/test_auth_oauth_autologin.py @@ -0,0 +1,44 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import requests + +from odoo.tests.common import HOST, HttpCase +from odoo.tools import config + + +class TestAuthMethod(HttpCase): + def _assert_no_autologin(self, query=""): + r = requests.get( + f"http://{HOST}:{config['http_port']}/web/login{query}", + allow_redirects=False, + ) + self.assertNotEqual(r.status_code, 303) + self.assertTrue(r.ok) + + def _assert_autologin(self, query=""): + r = requests.get( + f"http://{HOST}:{config['http_port']}/web/login{query}", + allow_redirects=False, + ) + self.assertEqual(r.status_code, 303) + + def test_end_to_end_default_providers(self): + # by default no provider is configured + providers = self.env["auth.oauth.provider"].search( + [("enabled", "=", True), ("autologin", "=", True)] + ) + self.assertFalse(providers) + self._assert_no_autologin() + + def test_end_to_end_one_provider(self): + providers = self.env["auth.oauth.provider"].search( + [("enabled", "=", True), ("autologin", "=", False)] + ) + self.assertEqual(len(providers), 1) + providers.autologin = True + providers.flush() + self._assert_autologin() + self._assert_no_autologin(query="?no_autologin=1") + self._assert_no_autologin(query="?error=...") + self._assert_no_autologin(query="?oauth_error=...") diff --git a/auth_oauth_autologin/views/auth_oauth_provider.xml b/auth_oauth_autologin/views/auth_oauth_provider.xml new file mode 100644 index 0000000000..ca3e06beaf --- /dev/null +++ b/auth_oauth_autologin/views/auth_oauth_provider.xml @@ -0,0 +1,23 @@ + + + + auth.oauth.autologin.provider.form + auth.oauth.provider + + + + + + + + + auth.oauth.autologin.provider.form + auth.oauth.provider + + + + + + + + diff --git a/setup/auth_oauth_autologin/odoo/addons/auth_oauth_autologin b/setup/auth_oauth_autologin/odoo/addons/auth_oauth_autologin new file mode 120000 index 0000000000..2910cd9e90 --- /dev/null +++ b/setup/auth_oauth_autologin/odoo/addons/auth_oauth_autologin @@ -0,0 +1 @@ +../../../../auth_oauth_autologin \ No newline at end of file diff --git a/setup/auth_oauth_autologin/setup.py b/setup/auth_oauth_autologin/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/auth_oauth_autologin/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)