Skip to content

Commit 72ae70c

Browse files
committed
[MIG] fastapi_auth_jwt: Migration to 17.0
[MIG] fastapi_auth_jwt: installable True [IMP] fastapi_auth_jwt: pre-commit auto fixes
1 parent 6639917 commit 72ae70c

File tree

11 files changed

+183
-157
lines changed

11 files changed

+183
-157
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ exclude: |
1111
^datamodel/|
1212
^extendable/|
1313
^extendable_fastapi/|
14-
^fastapi_auth_jwt/|
1514
^fastapi_auth_jwt_demo/|
1615
^graphql_base/|
1716
^graphql_demo/|

fastapi_auth_jwt/README.rst

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ FastAPI Auth JWT support
77
!! This file is generated by oca-gen-addon-readme !!
88
!! changes will be overwritten. !!
99
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10-
!! source digest: sha256:2829a34d48a1906819029e7b796d33a1ee2ad2a47693396da96f92ede04ec17d
10+
!! source digest: sha256:a7c4a8b4ef360ff91c0b2bcb7ee4ce90308f98ee56c94dd2d651e35ceb92011c
1111
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1212
1313
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
@@ -17,19 +17,20 @@ FastAPI Auth JWT support
1717
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
1818
:alt: License: LGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Frest--framework-lightgray.png?logo=github
20-
:target: https://github.com/OCA/rest-framework/tree/16.0/fastapi_auth_jwt
20+
:target: https://github.com/OCA/rest-framework/tree/17.0/fastapi_auth_jwt
2121
:alt: OCA/rest-framework
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/rest-framework-16-0/rest-framework-16-0-fastapi_auth_jwt
23+
:target: https://translation.odoo-community.org/projects/rest-framework-17-0/rest-framework-17-0-fastapi_auth_jwt
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=16.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/rest-framework&target_branch=17.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
3030

31-
This module provides ``FastAPI`` ``Depends`` to allow authentication with `auth_jwt
32-
<https://github.com/OCA/server-auth/tree/16.0/auth_jwt>`_.
31+
This module provides ``FastAPI`` ``Depends`` to allow authentication
32+
with
33+
`auth_jwt <https://github.com/OCA/server-auth/tree/16.0/auth_jwt>`__.
3334

3435
**Table of contents**
3536

@@ -39,83 +40,90 @@ This module provides ``FastAPI`` ``Depends`` to allow authentication with `auth_
3940
Usage
4041
=====
4142

42-
The following FastAPI dependencies are provided and importable from
43+
The following FastAPI dependencies are provided and importable from
4344
``odoo.addons.fastapi_auth_jwt.dependencies``:
4445

4546
``def auth_jwt_authenticated_payload() -> Payload``
4647

47-
Return the authenticated JWT payload. Raise a 401 (unauthorized) if absent or invalid.
48+
Return the authenticated JWT payload. Raise a 401 (unauthorized) if
49+
absent or invalid.
4850

4951
``def auth_jwt_optionally_authenticated_payload() -> Payload | None``
5052

51-
Return the authenticated JWT payload, or ``None`` if the ``Authorization`` header and
52-
cookie are absent. Raise a 401 (unauthorized) if present and invalid.
53+
Return the authenticated JWT payload, or ``None`` if the
54+
``Authorization`` header and cookie are absent. Raise a 401
55+
(unauthorized) if present and invalid.
5356

5457
``def auth_jwt_authenticated_partner() -> Partner``
5558

56-
Obtain the authenticated partner corresponding to the provided JWT token, according to
57-
the partner strategy defined on the ``auth_jwt`` validator. Raise a 401 (unauthorized)
58-
if the partner could not be determined for any reason.
59+
Obtain the authenticated partner corresponding to the provided JWT
60+
token, according to the partner strategy defined on the ``auth_jwt``
61+
validator. Raise a 401 (unauthorized) if the partner could not be
62+
determined for any reason.
5963

60-
This is function suitable and intended to override
61-
``odoo.addons.fastapi.dependencies.authenticated_partner_impl``.
64+
This is function suitable and intended to override
65+
``odoo.addons.fastapi.dependencies.authenticated_partner_impl``.
6266

63-
The partner record returned by this function is bound to an environment that uses the
64-
Odoo user obtained from the user strategy defined on the ``auth_jwt`` validator. When
65-
used ``authenticated_partner_impl`` this in turn ensures that
66-
``odoo.addons.fastapi.dependencies.authenticated_partner_env`` is also bound to the
67-
correct Odoo user.
67+
The partner record returned by this function is bound to an
68+
environment that uses the Odoo user obtained from the user strategy
69+
defined on the ``auth_jwt`` validator. When used
70+
``authenticated_partner_impl`` this in turn ensures that
71+
``odoo.addons.fastapi.dependencies.authenticated_partner_env`` is
72+
also bound to the correct Odoo user.
6873

6974
``def auth_jwt_optionally_authenticated_partner() -> Partner``
7075

71-
Same as ``auth_jwt_partner`` except it returns an empty recordset bound to the
72-
``public`` user if the ``Authorization`` header and cookie are absent, or if the JWT
73-
validator could not find the partner and declares that the partner is not required.
76+
Same as ``auth_jwt_partner`` except it returns an empty recordset
77+
bound to the ``public`` user if the ``Authorization`` header and
78+
cookie are absent, or if the JWT validator could not find the partner
79+
and declares that the partner is not required.
7480

7581
``def auth_jwt_authenticated_odoo_env() -> Environment``
7682

77-
Return an Odoo environment using the the Odoo user obtained from the user strategy
78-
defined on the ``auth_jwt`` validator, if the request could be authenticated using a
79-
JWT validator. Raise a 401 (unauthorized) otherwise.
83+
Return an Odoo environment using the the Odoo user obtained from the
84+
user strategy defined on the ``auth_jwt`` validator, if the request
85+
could be authenticated using a JWT validator. Raise a 401
86+
(unauthorized) otherwise.
8087

81-
This is function suitable and intended to override
82-
``odoo.addons.fastapi.dependencies.authenticated_odoo_env_impl``.
88+
This is function suitable and intended to override
89+
``odoo.addons.fastapi.dependencies.authenticated_odoo_env_impl``.
8390

8491
``def auth_jwt_default_validator_name() -> str | None``
8592

86-
Return the name of the default JWT validator to use.
93+
Return the name of the default JWT validator to use.
8794

88-
The default implementation returns ``None`` meaning only one active JWT validator is
89-
allowed. This dependency is meant to be overridden.
95+
The default implementation returns ``None`` meaning only one active
96+
JWT validator is allowed. This dependency is meant to be overridden.
9097

9198
``def auth_jwt_http_header_authorization() -> str | None``
9299

93-
By default, return the credentials part of the ``Authorization`` header, or ``None``
94-
if absent. This dependency is meant to be overridden, in particular with
95-
``fastapi.security.OAuth2AuthorizationCodeBearer`` to let swagger handle OAuth2
96-
authorization (such override is only necessary for comfort when using the swagger
97-
interface).
100+
By default, return the credentials part of the ``Authorization``
101+
header, or ``None`` if absent. This dependency is meant to be
102+
overridden, in particular with
103+
``fastapi.security.OAuth2AuthorizationCodeBearer`` to let swagger
104+
handle OAuth2 authorization (such override is only necessary for
105+
comfort when using the swagger interface).
98106

99107
Bug Tracker
100108
===========
101109

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

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

109117
Credits
110118
=======
111119

112120
Authors
113-
~~~~~~~
121+
-------
114122

115123
* ACSONE SA/NV
116124

117125
Maintainers
118-
~~~~~~~~~~~
126+
-----------
119127

120128
This module is maintained by the OCA.
121129

@@ -135,6 +143,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
135143

136144
|maintainer-sbidoul|
137145

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

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

fastapi_auth_jwt/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "FastAPI Auth JWT support",
66
"summary": """
77
JWT bearer token authentication for FastAPI.""",
8-
"version": "16.0.1.0.1",
8+
"version": "17.0.1.0.0",
99
"license": "LGPL-3",
1010
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
1111
"maintainers": ["sbidoul"],
@@ -16,5 +16,5 @@
1616
],
1717
"data": [],
1818
"demo": [],
19-
"installable": False,
19+
"installable": True,
2020
}

fastapi_auth_jwt/dependencies.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
33

44
import logging
5-
from typing import Annotated, Any, Dict, Optional, Tuple, Union
5+
from typing import Annotated, Any
66

77
from starlette.status import HTTP_401_UNAUTHORIZED
88

@@ -24,12 +24,11 @@
2424

2525
_logger = logging.getLogger(__name__)
2626

27-
28-
Payload = Dict[str, Any]
27+
Payload = dict[str, Any]
2928

3029

3130
def _get_auth_jwt_validator(
32-
validator_name: Union[str, None],
31+
validator_name: str | None,
3332
env: Environment,
3433
) -> AuthJwtValidator:
3534
validator = env["auth.jwt.validator"].sudo()._get_validator_by_name(validator_name)
@@ -39,9 +38,9 @@ def _get_auth_jwt_validator(
3938

4039
def _request_has_authentication(
4140
request: Request,
42-
authorization_header: Optional[str],
41+
authorization_header: str | None,
4342
validator: AuthJwtValidator,
44-
) -> Union[Payload, None]:
43+
) -> Payload | None:
4544
if authorization_header is not None:
4645
return True
4746
if not validator.cookie_enabled:
@@ -52,7 +51,7 @@ def _request_has_authentication(
5251

5352
def _get_jwt_payload(
5453
request: Request,
55-
authorization_header: Optional[str],
54+
authorization_header: str | None,
5655
validator: AuthJwtValidator,
5756
) -> Payload:
5857
"""Obtain and validate the JWT payload from the request authorization header or
@@ -76,9 +75,9 @@ def _get_jwt_payload(
7675
def _get_jwt_payload_and_validator(
7776
request: Request,
7877
response: Response,
79-
authorization_header: Optional[str],
78+
authorization_header: str | None,
8079
validator: AuthJwtValidator,
81-
) -> Tuple[Payload, AuthJwtValidator]:
80+
) -> tuple[Payload, AuthJwtValidator]:
8281
try:
8382
payload = None
8483
exceptions = {}
@@ -117,24 +116,24 @@ def _get_jwt_payload_and_validator(
117116
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED) from e
118117

119118

120-
def auth_jwt_default_validator_name() -> Union[str, None]:
119+
def auth_jwt_default_validator_name() -> str | None:
121120
return None
122121

123122

124123
def auth_jwt_http_header_authorization(
125124
credentials: Annotated[
126-
Optional[HTTPAuthorizationCredentials],
125+
HTTPAuthorizationCredentials | None,
127126
Depends(HTTPBearer(auto_error=False)),
128-
]
129-
):
127+
],
128+
) -> str | None:
130129
if credentials is None:
131130
return None
132131
return credentials.credentials
133132

134133

135134
class BaseAuthJwt: # noqa: B903
136135
def __init__(
137-
self, validator_name: Optional[str] = None, allow_unauthenticated: bool = False
136+
self, validator_name: str | None = None, allow_unauthenticated: bool = False
138137
):
139138
self.validator_name = validator_name
140139
self.allow_unauthenticated = allow_unauthenticated
@@ -146,18 +145,18 @@ def __call__(
146145
request: Request,
147146
response: Response,
148147
authorization_header: Annotated[
149-
Optional[str],
148+
str | None,
150149
Depends(auth_jwt_http_header_authorization),
151150
],
152151
default_validator_name: Annotated[
153-
Union[str, None],
152+
str | None,
154153
Depends(auth_jwt_default_validator_name),
155154
],
156155
env: Annotated[
157156
Environment,
158157
Depends(odoo_env),
159158
],
160-
) -> Optional[Payload]:
159+
) -> Payload | None:
161160
validator = _get_auth_jwt_validator(
162161
self.validator_name or default_validator_name, env
163162
)
@@ -176,11 +175,11 @@ def __call__(
176175
request: Request,
177176
response: Response,
178177
authorization_header: Annotated[
179-
Optional[str],
178+
str | None,
180179
Depends(auth_jwt_http_header_authorization),
181180
],
182181
default_validator_name: Annotated[
183-
Union[str, None],
182+
str | None,
184183
Depends(auth_jwt_default_validator_name),
185184
],
186185
env: Annotated[
@@ -215,11 +214,11 @@ def __call__(
215214
request: Request,
216215
response: Response,
217216
authorization_header: Annotated[
218-
Optional[str],
217+
str | None,
219218
Depends(auth_jwt_http_header_authorization),
220219
],
221220
default_validator_name: Annotated[
222-
Union[str, None],
221+
str | None,
223222
Depends(auth_jwt_default_validator_name),
224223
],
225224
env: Annotated[

fastapi_auth_jwt/pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["whool"]
3+
build-backend = "whool.buildapi"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This module provides `FastAPI` `Depends` to allow authentication with
2+
[auth_jwt](https://github.com/OCA/server-auth/tree/16.0/auth_jwt).

fastapi_auth_jwt/readme/DESCRIPTION.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)