Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MIG][17.0] fastapi_auth_api_key: Migration to 17.0 #436

Draft
wants to merge 4 commits into
base: 17.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 40 additions & 31 deletions fastapi_auth_api_key/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ Fastapi Auth Api Key
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
:target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_auth_api_key
:target: https://github.com/OCA/rest-framework/tree/17.0/fastapi_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-16-0/rest-framework-16-0-fastapi_auth_api_key
:target: https://translation.odoo-community.org/projects/rest-framework-17-0/rest-framework-17-0-fastapi_auth_api_key
: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=16.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=17.0
:alt: Try me on Runboat

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

Provides `FastAPI` dependencies for Api Key authentication.
Provides FastAPI dependencies for Api Key authentication.

**Table of contents**

Expand All @@ -39,64 +39,73 @@ Usage
=====

Getting an odoo environment
===========================
---------------------------

If you need to get an odoo env based on the provided api key, you can use `authenticated_env_by_auth_api_key`.
If you need to get an odoo env based on the provided api key, you can
use authenticated_env_by_auth_api_key.

.. code-block:: python
.. code:: python

@router.get("/example_with_authenticated_env")
def example_with_authenticated_env(
env: Annotated[Environment, Depends(authenticated_env_by_auth_api_key)],
) -> None:
# env.user is the user attached to the provided key
pass
@router.get("/example_with_authenticated_env")
def example_with_authenticated_env(
env: Annotated[Environment, Depends(authenticated_env_by_auth_api_key)],
) -> None:
# env.user is the user attached to the provided key
pass

Getting the authenticated partner
=================================
---------------------------------

If want to get the partned related to the the provided api key, you can use `authenticated_partner_by_api_key`
If want to get the partned related to the the provided api key, you can
use authenticated_partner_by_api_key

.. code-block:: python
.. code:: python

@router.get("/example_with_authenticated_partner")
def example_with_authenticated_partner(
partner: Annotated[Partner, Depends(authenticated_partner_by_api_key)],
) -> None:
# partner is the partner related to the provided key key.user_id.partner_id
pass
@router.get("/example_with_authenticated_partner")
def example_with_authenticated_partner(
partner: Annotated[Partner, Depends(authenticated_partner_by_api_key)],
) -> None:
# partner is the partner related to the provided key key.user_id.partner_id
pass

Configuration
=============
-------------

For this to work, the api key must be defined on the `Endpoint`.
A new field `auth_api_key_group_id` has been added to the `Endpoint` model.
For this to work, the api key must be defined on the Endpoint. A new
field auth_api_key_group_id has been added to the Endpoint model.

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 to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_auth_api_key%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/rest-framework/issues/new?body=module:%20fastapi_auth_api_key%0Aversion:%2017.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
~~~~~~~
-------

* Camptocamp

Contributors
~~~~~~~~~~~~
------------

* Matthieu Méquignon <[email protected]>
- Matthieu Méquignon <[email protected]>
- Son Ho <[email protected]>

Other credits
-------------

The migration of this module from 16.0 to 17.0 was financially supported
by Camptocamp

Maintainers
~~~~~~~~~~~
-----------

This module is maintained by the OCA.

Expand All @@ -116,6 +125,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-mmequignon|

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion fastapi_auth_api_key/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{
"name": "Fastapi Auth Api Key",
"version": "16.0.1.0.0",
"version": "17.0.1.0.0",
"category": "Others",
"website": "https://github.com/OCA/rest-framework",
"author": "Camptocamp, Odoo Community Association (OCA)",
Expand Down
4 changes: 2 additions & 2 deletions fastapi_auth_api_key/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from typing_extensions import Annotated
from typing import Annotated

from odoo import SUPERUSER_ID
from odoo.api import Environment
Expand Down Expand Up @@ -34,7 +34,7 @@ def authenticated_auth_api_key(
except ValidationError as error:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=error.name,
detail=error.args,
headers={"WWW-Authenticate": "HTTP-API-KEY"},
) from error
# Ensure the api key is authorized for the current endpoint.
Expand Down
3 changes: 3 additions & 0 deletions fastapi_auth_api_key/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions fastapi_auth_api_key/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Matthieu Méquignon \<<[email protected]>\>
- Son Ho \<<[email protected]>\>
1 change: 0 additions & 1 deletion fastapi_auth_api_key/readme/CONTRIBUTORS.rst

This file was deleted.

2 changes: 2 additions & 0 deletions fastapi_auth_api_key/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The migration of this module from 16.0 to 17.0 was financially supported
by Camptocamp
1 change: 1 addition & 0 deletions fastapi_auth_api_key/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provides FastAPI dependencies for Api Key authentication.
1 change: 0 additions & 1 deletion fastapi_auth_api_key/readme/DESCRIPTION.rst

This file was deleted.

32 changes: 32 additions & 0 deletions fastapi_auth_api_key/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Getting an odoo environment

If you need to get an odoo env based on the provided api key, you can
use authenticated_env_by_auth_api_key.

``` python
@router.get("/example_with_authenticated_env")
def example_with_authenticated_env(
env: Annotated[Environment, Depends(authenticated_env_by_auth_api_key)],
) -> None:
# env.user is the user attached to the provided key
pass
```

## Getting the authenticated partner

If want to get the partned related to the the provided api key, you can
use authenticated_partner_by_api_key

``` python
@router.get("/example_with_authenticated_partner")
def example_with_authenticated_partner(
partner: Annotated[Partner, Depends(authenticated_partner_by_api_key)],
) -> None:
# partner is the partner related to the provided key key.user_id.partner_id
pass
```

## Configuration

For this to work, the api key must be defined on the Endpoint. A new
field auth_api_key_group_id has been added to the Endpoint model.
33 changes: 0 additions & 33 deletions fastapi_auth_api_key/readme/USAGE.rst

This file was deleted.

22 changes: 17 additions & 5 deletions fastapi_auth_api_key/tests/test_fastapi_api_key_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
# Is it valid? We need an env without superpowers
demo_user = cls.env.ref("base.partner_demo")
demo_user = cls.env.ref("base.user_demo")
cls.demo_env = demo_user.with_user(demo_user).env
cls.demo_endpoint = cls.env["fastapi.endpoint"].create(
{
"name": "Test Enpoint",
"app": "demo",
"root_path": "/path_demo",
"demo_auth_method": "api_key",
"user_id": demo_user.id,
}
)
cls.setUpClassApiKey()

@classmethod
Expand Down Expand Up @@ -63,19 +72,22 @@ def setUpClassApiKey(cls):
def test_authenticated_auth_api_key(self):
# An exception is raised when no api key is used
with self.assertRaises(HTTPException) as error:
authenticated_auth_api_key(False, self.demo_env)
authenticated_auth_api_key(False, self.demo_env, self.demo_endpoint)
self.assertEqual(error.exception.detail, "No HTTP-API-KEY provided")
# An exception is raised when no api key record is found
with self.assertRaises(HTTPException) as error:
authenticated_auth_api_key("404", self.demo_env)
self.assertEqual(error.exception.detail, "The key 404 is not allowed")
authenticated_auth_api_key("404", self.demo_env, self.demo_endpoint)
self.assertEqual(error.exception.detail, ("The key 404 is not allowed",))
# TODO enable this when we know how to filter keys based
# on endpoint's api key group.
# An exception is raised when unauthorized api key record is found
# with self.assertRaises(HTTPException) as error:
# authenticated_auth_api_key("not_authorized", self.demo_env)
self.demo_endpoint.auth_api_key_group_id = (
self.authorized_api_key.auth_api_key_group_ids[0]
)
result_key = authenticated_auth_api_key(
self.authorized_api_key.key, self.demo_env
self.authorized_api_key.key, self.demo_env, self.demo_endpoint
)
self.assertEqual(result_key, self.authorized_api_key)

Expand Down
2 changes: 1 addition & 1 deletion fastapi_auth_api_key/views/fastapi_endpoint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<field name="model">fastapi.endpoint</field>
<field name="inherit_id" ref="fastapi.fastapi_endpoint_form_view" />
<field name="arch" type="xml">
<group name="resources" position="inside">
<group name="resoures" position="inside">
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a typo.

<field name="auth_api_key_group_id" />
</group>
</field>
Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.