Skip to content

Commit

Permalink
[ADD] base_rest_auth_api_key: Add support for the auth_api_key securi…
Browse files Browse the repository at this point in the history
…ty policy into the openapi documentation
  • Loading branch information
lmignon committed Jul 23, 2021
1 parent 638fd1f commit 0ea37d9
Show file tree
Hide file tree
Showing 13 changed files with 588 additions and 0 deletions.
83 changes: 83 additions & 0 deletions base_rest_auth_api_key/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
======================
Base Rest Auth Api Key
======================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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/14.0/base_rest_auth_api_key
: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-14-0/rest-framework-14-0-base_rest_auth_api_key
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/271/14.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|


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

**Table of contents**

.. contents::
:local:

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/rest-framework/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20base_rest_auth_api_key%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* ACSONE SA/NV

Contributors
~~~~~~~~~~~~

* Laurent Mignon <[email protected]>

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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-lmignon|

This module is part of the `OCA/rest-framework <https://github.com/OCA/rest-framework/tree/14.0/base_rest_auth_api_key>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions base_rest_auth_api_key/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import components
22 changes: 22 additions & 0 deletions base_rest_auth_api_key/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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 Api Key",
"summary": """
Base Rest: Add support for the auth_api_key security policy into the
openapi documentation""",
"version": "14.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_api_key"],
"maintainers": ["lmignon"],
"installable": True,
"auto_install": True,
"external_dependencies": {
"python": [
"apispec>=4.0.0",
]
},
}
Empty file.
31 changes: 31 additions & 0 deletions base_rest_auth_api_key/apispec/rest_method_security_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2021 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from apispec import BasePlugin


class RestMethodSecurityPlugin(BasePlugin):
def __init__(self, service):
super(RestMethodSecurityPlugin, self).__init__()
self._service = service

def init_spec(self, spec):
super(RestMethodSecurityPlugin, self).init_spec(spec)
self.spec = spec
self.openapi_version = spec.openapi_version
api_key_scheme = {"type": "apiKey", "in": "header", "name": "API-KEY"}
spec.components.security_scheme("api_key", api_key_scheme)

def operation_helper(self, path=None, operations=None, **kwargs):
routing = kwargs.get("routing")
if not routing:
super(RestMethodSecurityPlugin, self).operation_helper(
path, operations, **kwargs
)
if not operations:
return
auth = routing.get("auth", self.spec._params.get("default_auth"))
if auth == "api_key":
for _method, params in operations.items():
security = params.setdefault("security", [])
security.append({"api_key": []})
1 change: 1 addition & 0 deletions base_rest_auth_api_key/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import service
17 changes: 17 additions & 0 deletions base_rest_auth_api_key/components/service.py
Original file line number Diff line number Diff line change
@@ -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(BaseRestService, self)._get_api_spec(**params)
plugin = RestMethodSecurityPlugin(self)
plugin.init_spec(spec)
spec.plugins.append(plugin)
return spec
1 change: 1 addition & 0 deletions base_rest_auth_api_key/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Laurent Mignon <[email protected]>
3 changes: 3 additions & 0 deletions base_rest_auth_api_key/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

This technical addon extend `base_rest` to add the support for auth_api_key
authentication mechanism into the generated openapi documentation.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0ea37d9

Please sign in to comment.