diff --git a/README.md b/README.md index 6530da64c5..5bd64b86a6 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Available addons ---------------- addon | version | maintainers | summary --- | --- | --- | --- +[apikey_scope_editable](apikey_scope_editable/) | 17.0.1.0.0 | | Set the API Key scope at creation [auth_admin_passkey](auth_admin_passkey/) | 17.0.1.0.0 | | Allows system administrator to authenticate with any account [auth_admin_passkey_totp_mail_enforce](auth_admin_passkey_totp_mail_enforce/) | 17.0.1.0.0 | | Disable 2FA if Passkey is being used [auth_api_key](auth_api_key/) | 17.0.1.1.2 | | Authenticate http requests from an API key diff --git a/apikey_scope_editable/README.rst b/apikey_scope_editable/README.rst new file mode 100644 index 0000000000..981912a9e7 --- /dev/null +++ b/apikey_scope_editable/README.rst @@ -0,0 +1,99 @@ +.. image:: https://odoo-community.org/readme-banner-image + :target: https://odoo-community.org/get-involved?utm_source=readme + :alt: Odoo Community Association + +====================== +API Key Scope Editable +====================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:4e8aad6a07761c4885a96c95f07720d4041319afed525392ae71aeb48913aac7 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/license-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/17.0/apikey_scope_editable + :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-17-0/server-auth-17-0-apikey_scope_editable + :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=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the built-in `Odoo API +Keys `__ +by allowing you to define a scope at the moment of creation. + +With scopes, you can restrict an API Key so that it only grants access +to a specific application, model, or functional area of the system. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +1. Click on your user icon and then "Preferences" +2. Go to the "Account Security" tab, and then click the "New Api Key" + button. +3. Fill the data, a new field will appear to set the key scope. + +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 +------- + +* Sygel + +Contributors +------------ + +- `Sygel `__: + + - Alberto Martínez + - Valentin Vinagre + - Harald Panten + +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/server-auth `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/apikey_scope_editable/__init__.py b/apikey_scope_editable/__init__.py new file mode 100644 index 0000000000..adc6207fdd --- /dev/null +++ b/apikey_scope_editable/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import models +from . import wizards diff --git a/apikey_scope_editable/__manifest__.py b/apikey_scope_editable/__manifest__.py new file mode 100644 index 0000000000..43469f49c0 --- /dev/null +++ b/apikey_scope_editable/__manifest__.py @@ -0,0 +1,19 @@ +# Copyright 2025 Alberto Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "API Key Scope Editable", + "summary": "Set the API Key scope at creation", + "version": "17.0.1.0.0", + "category": "Technical", + "website": "https://github.com/OCA/server-auth", + "author": "Sygel, Odoo Community Association (OCA)", + "license": "AGPL-3", + "application": False, + "installable": True, + "depends": [ + "base", + ], + "data": [ + "wizards/res_users_apikeys_description_views.xml", + ], +} diff --git a/apikey_scope_editable/i18n/apikey_scope_editable.pot b/apikey_scope_editable/i18n/apikey_scope_editable.pot new file mode 100644 index 0000000000..9102c52a88 --- /dev/null +++ b/apikey_scope_editable/i18n/apikey_scope_editable.pot @@ -0,0 +1,71 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * apikey_scope_editable +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 17.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: apikey_scope_editable +#: model:ir.model,name:apikey_scope_editable.model_res_users_apikeys_description +msgid "API Key Description" +msgstr "" + +#. module: apikey_scope_editable +#: model_terms:ir.ui.view,arch_db:apikey_scope_editable.form_res_users_key_description +msgid "APIKey scope:" +msgstr "" + +#. module: apikey_scope_editable +#: model:ir.model.fields,field_description:apikey_scope_editable.field_res_users_apikeys_description__custom_scope +msgid "Custom Scope" +msgstr "" + +#. module: apikey_scope_editable +#: model_terms:ir.ui.view,arch_db:apikey_scope_editable.form_res_users_key_description +msgid "" +"Enter the optional scope for the key. If set, the key access would be " +"restricted." +msgstr "" + +#. module: apikey_scope_editable +#: model:ir.model.fields,field_description:apikey_scope_editable.field_res_users_apikeys_description__has_custom_scope +msgid "Has Custom Scope" +msgstr "" + +#. module: apikey_scope_editable +#: model_terms:ir.ui.view,arch_db:apikey_scope_editable.form_res_users_key_description +msgid "I want a custom scope" +msgstr "" + +#. module: apikey_scope_editable +#: model_terms:ir.ui.view,arch_db:apikey_scope_editable.form_res_users_key_description +msgid "No scope" +msgstr "" + +#. module: apikey_scope_editable +#: model:ir.model.fields,field_description:apikey_scope_editable.field_res_users_apikeys_description__scope +msgid "Scope" +msgstr "" + +#. module: apikey_scope_editable +#: model:ir.model,name:apikey_scope_editable.model_res_users_apikeys +msgid "Users API Keys" +msgstr "" + +#. module: apikey_scope_editable +#: model_terms:ir.ui.view,arch_db:apikey_scope_editable.form_res_users_key_description +msgid "What's the key scope?" +msgstr "" + +#. module: apikey_scope_editable +#: model:ir.model.fields.selection,name:apikey_scope_editable.selection__res_users_apikeys_description__scope__rpc +msgid "rpc" +msgstr "" diff --git a/apikey_scope_editable/models/__init__.py b/apikey_scope_editable/models/__init__.py new file mode 100644 index 0000000000..7396852919 --- /dev/null +++ b/apikey_scope_editable/models/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import res_users_apikeys diff --git a/apikey_scope_editable/models/res_users_apikeys.py b/apikey_scope_editable/models/res_users_apikeys.py new file mode 100644 index 0000000000..9aaecce159 --- /dev/null +++ b/apikey_scope_editable/models/res_users_apikeys.py @@ -0,0 +1,13 @@ +# Copyright 2025 Alberto Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class APIKeys(models.Model): + _inherit = "res.users.apikeys" + + def _generate(self, scope, name): + new_scope = self.env.context.get("apikey_scope") + scope = new_scope if new_scope else scope + return super()._generate(scope, name) diff --git a/apikey_scope_editable/pyproject.toml b/apikey_scope_editable/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/apikey_scope_editable/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/apikey_scope_editable/readme/CONTRIBUTORS.md b/apikey_scope_editable/readme/CONTRIBUTORS.md new file mode 100755 index 0000000000..f566245042 --- /dev/null +++ b/apikey_scope_editable/readme/CONTRIBUTORS.md @@ -0,0 +1,4 @@ +- [Sygel](https://www.sygel.es): + - Alberto Martínez + - Valentin Vinagre + - Harald Panten diff --git a/apikey_scope_editable/readme/DESCRIPTION.md b/apikey_scope_editable/readme/DESCRIPTION.md new file mode 100755 index 0000000000..f6a4d76f97 --- /dev/null +++ b/apikey_scope_editable/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module extends the built-in [Odoo API Keys](https://www.odoo.com/documentation/17.0/developer/reference/external_api.html#api-keys) by allowing you to define a scope at the moment of creation. + +With scopes, you can restrict an API Key so that it only grants access to a specific application, model, or functional area of the system. diff --git a/apikey_scope_editable/readme/USAGE.md b/apikey_scope_editable/readme/USAGE.md new file mode 100755 index 0000000000..795895d71e --- /dev/null +++ b/apikey_scope_editable/readme/USAGE.md @@ -0,0 +1,5 @@ +To use this module, you need to: + +1. Click on your user icon and then "Preferences" +2. Go to the "Account Security" tab, and then click the "New Api Key" button. +3. Fill the data, a new field will appear to set the key scope. diff --git a/apikey_scope_editable/static/description/icon.png b/apikey_scope_editable/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/apikey_scope_editable/static/description/icon.png differ diff --git a/apikey_scope_editable/static/description/icon.svg b/apikey_scope_editable/static/description/icon.svg new file mode 100644 index 0000000000..a7a26d0932 --- /dev/null +++ b/apikey_scope_editable/static/description/icon.svg @@ -0,0 +1,79 @@ + + + + + + + + image/svg+xml + + + + + + + + + + + diff --git a/apikey_scope_editable/static/description/index.html b/apikey_scope_editable/static/description/index.html new file mode 100644 index 0000000000..54f4432c04 --- /dev/null +++ b/apikey_scope_editable/static/description/index.html @@ -0,0 +1,449 @@ + + + + + +README.rst + + + +
+ + + +Odoo Community Association + +
+

API Key Scope Editable

+ +

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

+

This module extends the built-in Odoo API +Keys +by allowing you to define a scope at the moment of creation.

+

With scopes, you can restrict an API Key so that it only grants access +to a specific application, model, or functional area of the system.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Click on your user icon and then “Preferences”
  2. +
  3. Go to the “Account Security” tab, and then click the “New Api Key” +button.
  4. +
  5. Fill the data, a new field will appear to set the key scope.
  6. +
+
+
+

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

+
    +
  • Sygel
  • +
+
+
+

Contributors

+
    +
  • Sygel:
      +
    • Alberto Martínez
    • +
    • Valentin Vinagre
    • +
    • Harald Panten
    • +
    +
  • +
+
+
+

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.

+

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/apikey_scope_editable/tests/__init__.py b/apikey_scope_editable/tests/__init__.py new file mode 100644 index 0000000000..0efecd864d --- /dev/null +++ b/apikey_scope_editable/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_apikey_scope_editable diff --git a/apikey_scope_editable/tests/test_apikey_scope_editable.py b/apikey_scope_editable/tests/test_apikey_scope_editable.py new file mode 100644 index 0000000000..749bbbac04 --- /dev/null +++ b/apikey_scope_editable/tests/test_apikey_scope_editable.py @@ -0,0 +1,68 @@ +# Copyright 2025 Alberto Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import time + +from odoo.http import _request_stack +from odoo.tests.common import HttpCase +from odoo.tools import DotDict + + +class TestApiKeyScopeEditable(HttpCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.test_user = cls.env["res.users"].create( + { + "name": "Tyrion", + "login": "imp", + "password": "dracarys123", + "tz": "Pacific/Chatham", + } + ) + + def setUp(self): + def json_data(): + raise ValueError("Expected JSON content is missing") + + super().setUp() + mock_http = DotDict( + { + "httprequest": DotDict( + { + "environ": {"REMOTE_ADDR": "127.0.0.1"}, + "cookies": {}, + "args": {}, + } + ), + "session": {"identity-check-last": time.time()}, + "geoip": {}, + "get_json_data": json_data, + } + ) + _request_stack.push(mock_http) + self.addCleanup(lambda: _request_stack.pop()) + + def test_scope_generation(self): + scoped_env = self.env(user=self.test_user) + description = scoped_env["res.users.apikeys.description"].create( + {"name": "API Entry", "scope": "rpc"} + ) + description.make_key() + matching_keys = scoped_env["res.users.apikeys"].search([("scope", "=", "rpc")]) + self.assertTrue(matching_keys) + + def test_custom_scope_generation(self): + scoped_env = self.env(user=self.test_user) + description = scoped_env["res.users.apikeys.description"].create( + { + "name": "API Entry", + "has_custom_scope": True, + "custom_scope": "Custom Scope", + } + ) + description.make_key() + matching_keys = scoped_env["res.users.apikeys"].search( + [("scope", "=", "Custom Scope")] + ) + self.assertTrue(matching_keys) diff --git a/apikey_scope_editable/wizards/__init__.py b/apikey_scope_editable/wizards/__init__.py new file mode 100644 index 0000000000..65e51f8fc5 --- /dev/null +++ b/apikey_scope_editable/wizards/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import res_users_apikeys_description diff --git a/apikey_scope_editable/wizards/res_users_apikeys_description.py b/apikey_scope_editable/wizards/res_users_apikeys_description.py new file mode 100644 index 0000000000..b51442d6cc --- /dev/null +++ b/apikey_scope_editable/wizards/res_users_apikeys_description.py @@ -0,0 +1,28 @@ +# Copyright 2025 Alberto Martínez +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +from odoo import fields, models + +from odoo.addons.base.models.res_users import check_identity + + +class APIKeyDescription(models.TransientModel): + _inherit = "res.users.apikeys.description" + + scope = fields.Selection( + [("rpc", "rpc")], + ) + has_custom_scope = fields.Boolean() + custom_scope = fields.Char() + + @check_identity + def make_key(self): + return super( + APIKeyDescription, + self.with_context( + apikey_scope=self.scope + if not self.has_custom_scope + else self.custom_scope + ), + ).make_key() diff --git a/apikey_scope_editable/wizards/res_users_apikeys_description_views.xml b/apikey_scope_editable/wizards/res_users_apikeys_description_views.xml new file mode 100644 index 0000000000..88edcd59e1 --- /dev/null +++ b/apikey_scope_editable/wizards/res_users_apikeys_description_views.xml @@ -0,0 +1,28 @@ + + + + + + form_res_users_key_description + res.users.apikeys.description + + + +
+

Enter the optional scope for the key. If set, the key access would be restricted.

+

APIKey scope:

+

+

I want a custom scope

+
+
+
+ +
diff --git a/setup/_metapackage/pyproject.toml b/setup/_metapackage/pyproject.toml index a717ce93ce..39792048d1 100644 --- a/setup/_metapackage/pyproject.toml +++ b/setup/_metapackage/pyproject.toml @@ -1,7 +1,8 @@ [project] name = "odoo-addons-oca-server-auth" -version = "17.0.20250811.0" +version = "17.0.20250821.0" dependencies = [ + "odoo-addon-apikey_scope_editable>=17.0dev,<17.1dev", "odoo-addon-auth_admin_passkey>=17.0dev,<17.1dev", "odoo-addon-auth_admin_passkey_totp_mail_enforce>=17.0dev,<17.1dev", "odoo-addon-auth_api_key>=17.0dev,<17.1dev",