-
-
Notifications
You must be signed in to change notification settings - Fork 378
[17.0][MIG] add fastapi_auth_api_key #539
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| ==================== | ||
| Fastapi Auth Api Key | ||
| ==================== | ||
|
|
||
| .. | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! This file is generated by oca-gen-addon-readme !! | ||
| !! changes will be overwritten. !! | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
| !! source digest: sha256:a1a8681b1c3e7a13dc83e2e61a1d78ad8c8da1ddb684c8cf563607e96cf4f7e7 | ||
| !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
|
|
||
| .. |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%2Frest--framework-lightgray.png?logo=github | ||
| :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-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=17.0 | ||
| :alt: Try me on Runboat | ||
|
|
||
| |badge1| |badge2| |badge3| |badge4| |badge5| | ||
|
|
||
| Provides FastAPI dependencies for Api Key authentication. | ||
|
|
||
| **Table of contents** | ||
|
|
||
| .. contents:: | ||
| :local: | ||
|
|
||
| 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. | ||
|
|
||
| .. 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 | ||
|
|
||
| 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 | ||
|
|
||
| .. 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 | ||
|
|
||
| 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. | ||
|
|
||
| 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:%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 <matthieu.mequignon@camptocamp.com> | ||
| - Son Ho <sonhd@trobz.com> | ||
|
|
||
| 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. | ||
|
|
||
| .. 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-mmequignon| image:: https://github.com/mmequignon.png?size=40px | ||
| :target: https://github.com/mmequignon | ||
| :alt: mmequignon | ||
|
|
||
| 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/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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Copyright 2024 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| { | ||
| "name": "Fastapi Auth Api Key", | ||
| "version": "17.0.1.0.0", | ||
| "category": "Others", | ||
| "website": "https://github.com/OCA/rest-framework", | ||
| "author": "Camptocamp, Odoo Community Association (OCA)", | ||
| "maintainers": ["mmequignon"], | ||
| "license": "AGPL-3", | ||
| "installable": True, | ||
| "depends": [ | ||
| "fastapi", | ||
| "auth_api_key_group", | ||
| ], | ||
| "data": [ | ||
| "views/fastapi_endpoint.xml", | ||
| ], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||
| # Copyright 2024 Camptocamp SA | ||||||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||||||
|
|
||||||
| from typing import Annotated | ||||||
|
|
||||||
| from odoo import SUPERUSER_ID | ||||||
| from odoo.api import Environment | ||||||
| from odoo.exceptions import ValidationError | ||||||
|
|
||||||
| from odoo.addons.auth_api_key.models.auth_api_key import AuthApiKey | ||||||
| from odoo.addons.base.models.res_partner import Partner | ||||||
| from odoo.addons.fastapi.dependencies import fastapi_endpoint, odoo_env | ||||||
| from odoo.addons.fastapi.models.fastapi_endpoint import FastapiEndpoint | ||||||
|
|
||||||
| from fastapi import Depends, status | ||||||
| from fastapi.exceptions import HTTPException | ||||||
| from fastapi.security import APIKeyHeader | ||||||
|
|
||||||
|
|
||||||
| def authenticated_auth_api_key( | ||||||
| key: Annotated[str, Depends(APIKeyHeader(name="HTTP-API-KEY"))], | ||||||
| env: Annotated[Environment, Depends(odoo_env)], | ||||||
| endpoint: Annotated[FastapiEndpoint, Depends(fastapi_endpoint)], | ||||||
| ) -> AuthApiKey: | ||||||
| if not key: | ||||||
| raise HTTPException( | ||||||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||||||
| detail="No HTTP-API-KEY provided", | ||||||
| headers={"WWW-Authenticate": "HTTP-API-KEY"}, | ||||||
| ) | ||||||
| admin_env = Environment(env.cr, SUPERUSER_ID, {}) | ||||||
| try: | ||||||
| auth_api_key = admin_env["auth.api.key"]._retrieve_api_key(key) | ||||||
| except ValidationError as error: | ||||||
| raise HTTPException( | ||||||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||||||
| detail=error.args, | ||||||
| headers={"WWW-Authenticate": "HTTP-API-KEY"}, | ||||||
| ) from error | ||||||
| # Ensure the api key is authorized for the current endpoint. | ||||||
| if auth_api_key not in endpoint.sudo().auth_api_key_group_id.auth_api_key_ids: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far I understand we must set an if yes I would set the field as required or support the case there is no
Suggested change
|
||||||
| raise HTTPException( | ||||||
| status_code=status.HTTP_401_UNAUTHORIZED, | ||||||
| detail="Unauthorized", | ||||||
| headers={"WWW-Authenticate": "HTTP-API-KEY"}, | ||||||
| ) | ||||||
| return auth_api_key | ||||||
|
|
||||||
|
|
||||||
| def authenticated_partner_by_api_key( | ||||||
| auth_api_key: Annotated[AuthApiKey, Depends(authenticated_auth_api_key)] | ||||||
| ) -> Partner: | ||||||
| return auth_api_key.user_id.partner_id | ||||||
|
|
||||||
|
|
||||||
| def authenticated_env_by_auth_api_key( | ||||||
| auth_api_key: Annotated[AuthApiKey, Depends(authenticated_auth_api_key)] | ||||||
| ) -> Environment: | ||||||
| # set api key id in context | ||||||
| return auth_api_key.with_user(auth_api_key.user_id).env | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import fastapi_endpoint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright 2024 Camptocamp SA | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
|
||
| from odoo import fields, models | ||
|
|
||
|
|
||
| class FastapiEndpoint(models.Model): | ||
| _inherit = "fastapi.endpoint" | ||
|
|
||
| auth_api_key_group_id = fields.Many2one("auth.api.key.group") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [build-system] | ||
| requires = ["whool"] | ||
| build-backend = "whool.buildapi" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Matthieu Méquignon \<<matthieu.mequignon@camptocamp.com>\> | ||
| - Son Ho \<<sonhd@trobz.com>\> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Provides FastAPI dependencies for Api Key authentication. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it a real desired changed ? to avoid breaking consumer I would recomand to use same header as on previous version ie; https://github.com/OCA/rest-framework/blob/16.0/base_rest_auth_api_key/apispec/rest_method_security_plugin.py#L17