Skip to content

Commit 3304a58

Browse files
committed
[MIG] fastapi_auth_api_key: Migration to 18.0
1 parent 41a4cfc commit 3304a58

9 files changed

Lines changed: 38 additions & 30 deletions

File tree

fastapi_auth_api_key/README.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ Configuration
7272
-------------
7373

7474
For this to work, the api key must be defined on the Endpoint. A new
75-
field auth_api_key_group_id has been added to the Endpoint model.
75+
field auth_api_key_group_id has been added to the Endpoint model as well
76+
as auth_http_api_key_header.
7677

7778
Bug Tracker
7879
===========
@@ -97,11 +98,12 @@ Contributors
9798

9899
- Matthieu Méquignon <matthieu.mequignon@camptocamp.com>
99100
- Son Ho <sonhd@trobz.com>
101+
- Maksym Yankin <maksym.yankin@camptocamp.com>
100102

101103
Other credits
102104
-------------
103105

104-
The migration of this module from 16.0 to 17.0 was financially supported
106+
The migration of this module from 17.0 to 18.0 was financially supported
105107
by Camptocamp
106108

107109
Maintainers

fastapi_auth_api_key/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
{
55
"name": "Fastapi Auth API Key",
6-
"version": "17.0.1.0.0",
6+
"version": "18.0.1.0.0",
77
"category": "Others",
88
"website": "https://github.com/OCA/rest-framework",
99
"author": "Camptocamp, Odoo Community Association (OCA)",

fastapi_auth_api_key/dependencies.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright 2024 Camptocamp SA
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
3-
import os
3+
44
from typing import Annotated
55

6-
from odoo import SUPERUSER_ID, _
6+
from odoo import SUPERUSER_ID
77
from odoo.api import Environment
88
from odoo.exceptions import ValidationError
99

@@ -14,31 +14,27 @@
1414

1515
from fastapi import Depends, status
1616
from fastapi.exceptions import HTTPException
17-
from fastapi.security import APIKeyHeader
18-
19-
HTTP_API_KEY_HEADER = os.environ.get("FASTAPI_AUTH_HTTP_API_KEY_HEADER", "HTTP-API-KEY")
2017

2118

2219
def authenticated_auth_api_key(
23-
key: Annotated[str, Depends(APIKeyHeader(name=HTTP_API_KEY_HEADER))],
2420
env: Annotated[Environment, Depends(odoo_env)],
2521
endpoint: Annotated[FastapiEndpoint, Depends(fastapi_endpoint)],
2622
) -> AuthApiKey:
23+
admin_env = Environment(env.cr, SUPERUSER_ID, {})
24+
key = endpoint.sudo().auth_http_api_key_header
2725
if not key:
2826
raise HTTPException(
2927
status_code=status.HTTP_401_UNAUTHORIZED,
30-
detail=_("Missing %(HTTP_API_KEY_HEADER)s header")
31-
% {"HTTP_API_KEY_HEADER": HTTP_API_KEY_HEADER},
32-
headers={"WWW-Authenticate": HTTP_API_KEY_HEADER},
28+
detail=admin_env._("Missing HTTP API key header"),
29+
headers={"WWW-Authenticate": key},
3330
)
34-
admin_env = Environment(env.cr, SUPERUSER_ID, {})
3531
try:
3632
auth_api_key = admin_env["auth.api.key"]._retrieve_api_key(key)
3733
except ValidationError as error:
3834
raise HTTPException(
3935
status_code=status.HTTP_401_UNAUTHORIZED,
4036
detail=error.args,
41-
headers={"WWW-Authenticate": HTTP_API_KEY_HEADER},
37+
headers={"WWW-Authenticate": key},
4238
) from error
4339
# Ensure the api key is authorized for the current endpoint.
4440
if (
@@ -47,8 +43,8 @@ def authenticated_auth_api_key(
4743
):
4844
raise HTTPException(
4945
status_code=status.HTTP_401_UNAUTHORIZED,
50-
detail=_("Unauthorized"),
51-
headers={"WWW-Authenticate": HTTP_API_KEY_HEADER},
46+
detail=admin_env._("Unauthorized"),
47+
headers={"WWW-Authenticate": key},
5248
)
5349
return auth_api_key
5450

fastapi_auth_api_key/models/fastapi_endpoint.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ class FastapiEndpoint(models.Model):
1111
"auth.api.key.group",
1212
help="If not set, all 'auth.api.key' are allowed to access to endpoints",
1313
)
14+
auth_http_api_key_header: str = fields.Char(
15+
string="API Key Header",
16+
help="The HTTP header name expected in requests for API authentication "
17+
"(e.g. 'X-API-Key').",
18+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
- Matthieu Méquignon \<<matthieu.mequignon@camptocamp.com>\>
22
- Son Ho \<<sonhd@trobz.com>\>
3+
- Maksym Yankin \<<maksym.yankin@camptocamp.com>\>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
The migration of this module from 16.0 to 17.0 was financially supported
1+
The migration of this module from 17.0 to 18.0 was financially supported
22
by Camptocamp

fastapi_auth_api_key/readme/USAGE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ def example_with_authenticated_partner(
2929
## Configuration
3030

3131
For this to work, the api key must be defined on the Endpoint. A new
32-
field auth_api_key_group_id has been added to the Endpoint model.
32+
field auth_api_key_group_id has been added to the Endpoint model as well
33+
as auth_http_api_key_header.

fastapi_auth_api_key/tests/test_fastapi_api_key_dependencies.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,36 @@ def setUpClassApiKey(cls):
7272
def test_authenticated_auth_api_key_no_api_key(self):
7373
# An exception is raised when no api key is used
7474
with self.assertRaises(HTTPException) as error:
75-
authenticated_auth_api_key(False, self.demo_env, self.demo_endpoint)
76-
self.assertEqual(error.exception.detail, "Missing HTTP-API-KEY header")
75+
authenticated_auth_api_key(self.demo_env, self.demo_endpoint)
76+
self.assertEqual(error.exception.detail, "Missing HTTP API key header")
7777

7878
def test_authenticated_auth_api_key_no_api_key_found(self):
79+
self.demo_endpoint.auth_http_api_key_header = "404"
7980
# An exception is raised when no api key record is found
8081
with self.assertRaises(HTTPException) as error:
81-
authenticated_auth_api_key("404", self.demo_env, self.demo_endpoint)
82+
authenticated_auth_api_key(self.demo_env, self.demo_endpoint)
8283
self.assertEqual(error.exception.detail, ("The key 404 is not allowed",))
8384

8485
def test_authenticated_auth_api_key_unauthorized_key(self):
86+
self.demo_endpoint.auth_http_api_key_header = "unauthorized_key"
8587
# An exception is raised when unauthorized api key record is found
8688
with self.assertRaisesRegex(HTTPException, r"Unauthorized"):
87-
authenticated_auth_api_key(
88-
"unauthorized_key", self.demo_env, self.demo_endpoint
89-
)
89+
authenticated_auth_api_key(self.demo_env, self.demo_endpoint)
9090

9191
def test_authenticated_auth_api_key(self):
92-
result_key = authenticated_auth_api_key(
93-
"authorized_key", self.demo_env, self.demo_endpoint
94-
)
92+
self.demo_endpoint.auth_http_api_key_header = "authorized_key"
93+
result_key = authenticated_auth_api_key(self.demo_env, self.demo_endpoint)
9594
self.assertEqual(result_key, self.authorized_api_key)
9695

9796
def test_authenticated_auth_api_key_without_group(self):
9897
# test with no group set unauthorized is allow to access
99-
self.demo_endpoint.auth_api_key_group_id = False
100-
result_key = authenticated_auth_api_key(
101-
"unauthorized_key", self.demo_env, self.demo_endpoint
98+
self.demo_endpoint.write(
99+
{
100+
"auth_api_key_group_id": False,
101+
"auth_http_api_key_header": "unauthorized_key",
102+
}
102103
)
104+
result_key = authenticated_auth_api_key(self.demo_env, self.demo_endpoint)
103105
self.assertEqual(result_key, self.unauthorized_api_key)
104106

105107
def test_authenticated_partner_by_api_key(self):

fastapi_auth_api_key/views/fastapi_endpoint.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<field name="arch" type="xml">
1010
<group name="resoures" position="inside">
1111
<field name="auth_api_key_group_id" />
12+
<field name="auth_http_api_key_header" />
1213
</group>
1314
</field>
1415
</record>

0 commit comments

Comments
 (0)