diff --git a/base_rest_auth_jwt/README.rst b/base_rest_auth_jwt/README.rst new file mode 100644 index 000000000..b3a0f9cf9 --- /dev/null +++ b/base_rest_auth_jwt/README.rst @@ -0,0 +1,85 @@ +================== +Base Rest Auth Jwt +================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f7b7885f92510156ec9d3cd8871149dbee21599ab16e40b67f3a68d6ce2d0ea9 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github + :target: https://github.com/OCA/rest-framework/tree/18.0/base_rest_auth_jwt + :alt: OCA/rest-framework +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/rest-framework-18-0/rest-framework-18-0-base_rest_auth_jwt + :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/rest-framework&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This technical addon extend base_rest to add the support for auth_jwt +authentication mechanism into the generated openapi documentation. + +**Table of contents** + +.. contents:: + :local: + +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 + +Contributors +------------ + +- Laurent Mignon + +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-lmignon| image:: https://github.com/lmignon.png?size=40px + :target: https://github.com/lmignon + :alt: lmignon + +Current `maintainer `__: + +|maintainer-lmignon| + +This module is part of the `OCA/rest-framework `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/base_rest_auth_jwt/__init__.py b/base_rest_auth_jwt/__init__.py new file mode 100644 index 000000000..1377f57f5 --- /dev/null +++ b/base_rest_auth_jwt/__init__.py @@ -0,0 +1 @@ +from . import components diff --git a/base_rest_auth_jwt/__manifest__.py b/base_rest_auth_jwt/__manifest__.py new file mode 100644 index 000000000..3c88c0cd4 --- /dev/null +++ b/base_rest_auth_jwt/__manifest__.py @@ -0,0 +1,22 @@ +# Copyright 2021 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +{ + "name": "Base Rest Auth Jwt", + "summary": """ + Base Rest: Add support for the auth_jwt security policy into the + openapi documentation""", + "version": "18.0.1.0.0", + "license": "LGPL-3", + "author": "ACSONE SA/NV,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/rest-framework", + "depends": ["base_rest", "auth_jwt"], + "maintainers": ["lmignon"], + "installable": True, + "auto_install": True, + "external_dependencies": { + "python": [ + "apispec", + ] + }, +} diff --git a/base_rest_auth_jwt/apispec/__init__.py b/base_rest_auth_jwt/apispec/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/base_rest_auth_jwt/apispec/rest_method_security_plugin.py b/base_rest_auth_jwt/apispec/rest_method_security_plugin.py new file mode 100644 index 000000000..bcf217117 --- /dev/null +++ b/base_rest_auth_jwt/apispec/rest_method_security_plugin.py @@ -0,0 +1,43 @@ +# Copyright 2021 ACSONE SA/NV +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from apispec import BasePlugin + +from odoo.addons.base_rest.tools import ROUTING_DECORATOR_ATTR + + +class RestMethodSecurityPlugin(BasePlugin): + def __init__(self, service): + super().__init__() + self._service = service + + # pylint: disable=W8110 + def init_spec(self, spec): + super().init_spec(spec) + self.spec = spec + self.openapi_version = spec.openapi_version + jwt_scheme = { + "type": "http", + "scheme": "bearer", + "bearerFormat": "JWT", + "name": "jwt", + "description": "Enter JWT Bearer Token ** only **", + } + spec.components.security_scheme("jwt", jwt_scheme) + + def operation_helper(self, path=None, operations=None, **kwargs): + routing = kwargs.get(ROUTING_DECORATOR_ATTR) + if not routing: + super().operation_helper(path, operations, **kwargs) + if not operations: + return + default_auth = self.spec._params.get("default_auth") + auth = routing.get("auth", default_auth) + if (auth and auth.startswith("jwt")) or ( + auth == "public_or_default" + and default_auth + and default_auth.startswith("jwt") + ): + for _method, params in operations.items(): + security = params.setdefault("security", []) + security.append({"jwt": []}) diff --git a/base_rest_auth_jwt/components/__init__.py b/base_rest_auth_jwt/components/__init__.py new file mode 100644 index 000000000..9ffb68f0b --- /dev/null +++ b/base_rest_auth_jwt/components/__init__.py @@ -0,0 +1,2 @@ +from . import auth_jwt_component_context_provider +from . import service diff --git a/base_rest_auth_jwt/components/auth_jwt_component_context_provider.py b/base_rest_auth_jwt/components/auth_jwt_component_context_provider.py new file mode 100644 index 000000000..e83c21d44 --- /dev/null +++ b/base_rest_auth_jwt/components/auth_jwt_component_context_provider.py @@ -0,0 +1,23 @@ +# Copyright 2021 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + + +from odoo.http import request + +from odoo.addons.component.core import AbstractComponent, Component + + +class AbstractAuthJwtAuthenticatedPartnerProvider(AbstractComponent): + _name = "abstract.auth.jwt.authenticated.partner.provider" + + def _get_authenticated_partner_id(self): + return request.jwt_partner_id + + +class BaseRestAuthJwtComponentContextProvider(Component): + _name = "base.rest.auth.jwt.component.context.provider" + _inherit = [ + "abstract.auth.jwt.authenticated.partner.provider", + "base.rest.service.context.provider", + ] + _usage = "auth_jwt_component_context_provider" diff --git a/base_rest_auth_jwt/components/service.py b/base_rest_auth_jwt/components/service.py new file mode 100644 index 000000000..4ee6e84c6 --- /dev/null +++ b/base_rest_auth_jwt/components/service.py @@ -0,0 +1,17 @@ +# Copyright 2021 ACSONE SA/NV +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). + +from odoo.addons.component.core import AbstractComponent + +from ..apispec.rest_method_security_plugin import RestMethodSecurityPlugin + + +class BaseRestService(AbstractComponent): + _inherit = "base.rest.service" + + def _get_api_spec(self, **params): + spec = super()._get_api_spec(**params) + plugin = RestMethodSecurityPlugin(self) + plugin.init_spec(spec) + spec.plugins.append(plugin) + return spec diff --git a/base_rest_auth_jwt/i18n/base_rest_auth_jwt.pot b/base_rest_auth_jwt/i18n/base_rest_auth_jwt.pot new file mode 100644 index 000000000..4d8b20f91 --- /dev/null +++ b/base_rest_auth_jwt/i18n/base_rest_auth_jwt.pot @@ -0,0 +1,13 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.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" diff --git a/base_rest_auth_jwt/pyproject.toml b/base_rest_auth_jwt/pyproject.toml new file mode 100644 index 000000000..4231d0ccc --- /dev/null +++ b/base_rest_auth_jwt/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/base_rest_auth_jwt/readme/CONTRIBUTORS.md b/base_rest_auth_jwt/readme/CONTRIBUTORS.md new file mode 100644 index 000000000..dc42b35f3 --- /dev/null +++ b/base_rest_auth_jwt/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Laurent Mignon \ diff --git a/base_rest_auth_jwt/readme/DESCRIPTION.md b/base_rest_auth_jwt/readme/DESCRIPTION.md new file mode 100644 index 000000000..f3e03f3d9 --- /dev/null +++ b/base_rest_auth_jwt/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This technical addon extend base_rest to add the support for auth_jwt +authentication mechanism into the generated openapi documentation. diff --git a/base_rest_auth_jwt/static/description/icon.png b/base_rest_auth_jwt/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/base_rest_auth_jwt/static/description/icon.png differ diff --git a/base_rest_auth_jwt/static/description/index.html b/base_rest_auth_jwt/static/description/index.html new file mode 100644 index 000000000..3cfac8713 --- /dev/null +++ b/base_rest_auth_jwt/static/description/index.html @@ -0,0 +1,426 @@ + + + + + +Base Rest Auth Jwt + + + +
+

Base Rest Auth Jwt

+ + +

Beta License: LGPL-3 OCA/rest-framework Translate me on Weblate Try me on Runboat

+

This technical addon extend base_rest to add the support for auth_jwt +authentication mechanism into the generated openapi documentation.

+

Table of contents

+ +
+

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
  • +
+
+
+

Contributors

+ +
+
+

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:

+

lmignon

+

This module is part of the OCA/rest-framework project on GitHub.

+

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

+
+
+
+ +