-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shopinvader_fastapi_auth_partner: add module
- Loading branch information
1 parent
88eccb3
commit e13753b
Showing
10 changed files
with
399 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
setup/shopinvader_fastapi_auth_partner/odoo/addons/shopinvader_fastapi_auth_partner
This file contains 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 @@ | ||
../../../../shopinvader_fastapi_auth_partner |
This file contains 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,6 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
setup_requires=['setuptools-odoo'], | ||
odoo_addon=True, | ||
) |
This file contains 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,87 @@ | ||
============================================================= | ||
Shopinvader Auth Partner authentication for FastAPI endpoints | ||
============================================================= | ||
|
||
.. | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! This file is generated by oca-gen-addon-readme !! | ||
!! changes will be overwritten. !! | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
!! source digest: sha256:2d541bb8cb7014b523724a433fb30146e0a3afb1190677f90431e36e41cd3d96 | ||
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! | ||
.. |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-shopinvader%2Fodoo--shopinvader-lightgray.png?logo=github | ||
:target: https://github.com/shopinvader/odoo-shopinvader/tree/16.0/shopinvader_fastapi_auth_partner | ||
:alt: shopinvader/odoo-shopinvader | ||
|
||
|badge1| |badge2| |badge3| | ||
|
||
This module provides the ``auth_jwt_authenticated_or_anonymous_partner`` and | ||
``auth_jwt_authenticated_or_anonymous_partner_auto_create`` FastAPI dependencies. | ||
|
||
**Table of contents** | ||
|
||
.. contents:: | ||
:local: | ||
|
||
Usage | ||
===== | ||
|
||
This module provide the following FastAPI dependencies: | ||
|
||
``def auth_jwt_authenticated_or_anonymous_partner() -> Partner`` | ||
|
||
This dependency returns the authenticated partner from ``fast_api_auth_jwt`` | ||
``auth_jwt_optionally_authenticated_partner``. If not authenticated or no partner is | ||
found, look for the ``shopinvader_anonymous_partner`` cookie in the request and return | ||
the corresponding partner. | ||
|
||
If not partner is found, raise a 401 (unauthorized). | ||
|
||
``def auth_jwt_authenticated_or_anonymous_partner_auto_create() -> Partner`` | ||
|
||
This dependency returns the authenticated partner from ``fast_api_auth_jwt`` | ||
``auth_jwt_optionally_authenticated_partner``. If not authenticated or no partner is | ||
found, look for the ``shopinvader_anonymous_partner`` cookie in the request and return | ||
the corresponding partner. | ||
|
||
If no partner is found, create an anonymous partner, set the corresponding cookie and | ||
return the newly created partner. | ||
|
||
The record sets returned from these functions are bound either to the Odoo user defined | ||
on the JWT validaator (if authenticated), or to the Odoo user defined on the FastAPI | ||
endpoint. | ||
|
||
These dependencies are suitable and intended to override the | ||
``odoo.addon.fastapi.dependencies.authenticated_partner_impl``. | ||
|
||
Bug Tracker | ||
=========== | ||
|
||
Bugs are tracked on `GitHub Issues <https://github.com/shopinvader/odoo-shopinvader/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/shopinvader/odoo-shopinvader/issues/new?body=module:%20shopinvader_fastapi_auth_partner%0Aversion:%2016.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 | ||
~~~~~~~ | ||
|
||
* Akretion | ||
|
||
Maintainers | ||
~~~~~~~~~~~ | ||
|
||
This module is part of the `shopinvader/odoo-shopinvader <https://github.com/shopinvader/odoo-shopinvader/tree/16.0/shopinvader_fastapi_auth_partner>`_ project on GitHub. | ||
|
||
You are welcome to contribute. |
Empty file.
This file contains 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,18 @@ | ||
# Copyright 2023 Akretion (https://www.akretion.com). | ||
# @author Sébastien BEAU <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
|
||
{ | ||
"name": "Shopinvader Auth Partner authentication for FastAPI endpoints", | ||
"summary": """ | ||
Provide Partner and Anonymous Partner authentication to FastAPI routes.""", | ||
"version": "16.0.1.0.0", | ||
"license": "AGPL-3", | ||
"author": "Akretion", | ||
"maintainers": [], | ||
"website": "https://github.com/shopinvader/odoo-shopinvader", | ||
"depends": ["fastapi_auth_partner", "shopinvader_anonymous_partner"], | ||
"data": [], | ||
"demo": [], | ||
} |
This file contains 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,66 @@ | ||
# Copyright 2023 Akretion (https://www.akretion.com). | ||
# @author Sébastien BEAU <[email protected]> | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
|
||
import logging | ||
import sys | ||
|
||
from fastapi import Depends, HTTPException, Request, Response, status | ||
|
||
from odoo.api import Environment | ||
|
||
from odoo.addons.base.models.res_partner import Partner | ||
from odoo.addons.fastapi.dependencies import odoo_env | ||
from odoo.addons.fastapi_auth_partner.dependencies import ( | ||
auth_partner_optionally_authenticated_partner, | ||
) | ||
|
||
if sys.version_info >= (3, 9): | ||
from typing import Annotated | ||
else: | ||
from typing_extensions import Annotated | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
def auth_partner_authenticated_or_anonymous_partner( | ||
partner: Annotated[ | ||
Partner, | ||
Depends(auth_partner_optionally_authenticated_partner), | ||
], | ||
env: Annotated[Environment, Depends(odoo_env)], | ||
request: Request, | ||
) -> Partner: | ||
if partner: | ||
return partner | ||
anonymous_partner = env["res.partner"]._get_anonymous_partner__cookie( | ||
request.cookies | ||
) | ||
if anonymous_partner: | ||
return env["res.partner"].browse(anonymous_partner.id) | ||
_logger.info( | ||
"Partner auth authentication failed and no anonymous partner cookie found." | ||
) | ||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED) | ||
|
||
|
||
def auth_partner_authenticated_or_anonymous_partner_autocreate( | ||
partner: Annotated[ | ||
Partner, | ||
Depends(auth_partner_optionally_authenticated_partner), | ||
], | ||
env: Annotated[Environment, Depends(odoo_env)], | ||
request: Request, | ||
response: Response, | ||
) -> Partner: | ||
if partner: | ||
return partner | ||
anonymous_partner = env["res.partner"]._get_anonymous_partner__cookie( | ||
request.cookies | ||
) | ||
if not anonymous_partner: | ||
anonymous_partner = env["res.partner"]._create_anonymous_partner__cookie( | ||
response | ||
) | ||
return env["res.partner"].browse(anonymous_partner.id) |
This file contains 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 @@ | ||
This module provides the ``auth_jwt_authenticated_or_anonymous_partner`` and | ||
``auth_jwt_authenticated_or_anonymous_partner_auto_create`` FastAPI dependencies. |
This file contains 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,27 @@ | ||
This module provide the following FastAPI dependencies: | ||
|
||
``def auth_jwt_authenticated_or_anonymous_partner() -> Partner`` | ||
|
||
This dependency returns the authenticated partner from ``fast_api_auth_jwt`` | ||
``auth_jwt_optionally_authenticated_partner``. If not authenticated or no partner is | ||
found, look for the ``shopinvader_anonymous_partner`` cookie in the request and return | ||
the corresponding partner. | ||
|
||
If not partner is found, raise a 401 (unauthorized). | ||
|
||
``def auth_jwt_authenticated_or_anonymous_partner_auto_create() -> Partner`` | ||
|
||
This dependency returns the authenticated partner from ``fast_api_auth_jwt`` | ||
``auth_jwt_optionally_authenticated_partner``. If not authenticated or no partner is | ||
found, look for the ``shopinvader_anonymous_partner`` cookie in the request and return | ||
the corresponding partner. | ||
|
||
If no partner is found, create an anonymous partner, set the corresponding cookie and | ||
return the newly created partner. | ||
|
||
The record sets returned from these functions are bound either to the Odoo user defined | ||
on the JWT validaator (if authenticated), or to the Odoo user defined on the FastAPI | ||
endpoint. | ||
|
||
These dependencies are suitable and intended to override the | ||
``odoo.addon.fastapi.dependencies.authenticated_partner_impl``. |
This file contains 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 test_auth_partner_or_anonymous |
Oops, something went wrong.