Skip to content

Commit e035f31

Browse files
committed
[FIX] auth_api_key: Read API key from headers
1 parent 6c7d03e commit e035f31

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

auth_api_key/models/ir_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class IrHttp(models.AbstractModel):
1818

1919
@classmethod
2020
def _auth_method_api_key(cls):
21-
headers = request.httprequest.environ
22-
api_key = headers.get("HTTP_API_KEY")
21+
headers = request.httprequest.headers
22+
api_key = headers.get("API_KEY")
2323
if api_key:
2424
request.update_env(user=1)
2525
auth_api_key = request.env["auth.api.key"]._retrieve_api_key(api_key)

auth_api_key/tests/test_auth_api_key.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# Copyright 2018 ACSONE SA/NV
22
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).
3+
4+
from werkzeug.datastructures import EnvironHeaders
5+
36
from odoo.exceptions import AccessError, ValidationError
47
from odoo.tests.common import TransactionCase
58

9+
from odoo.addons.website.tools import MockRequest
10+
611

712
class TestAuthApiKey(TransactionCase):
813
@classmethod
@@ -68,3 +73,12 @@ def test_user_archived_unarchived_with_option_off(self):
6873
self.assertEqual(
6974
self.env["auth.api.key"]._retrieve_uid_from_api_key("api_key"), demo_user.id
7075
)
76+
77+
def test_api_key_headers(self):
78+
"""The API key in the headers is read."""
79+
with MockRequest(self.env) as mocked_request:
80+
mocked_request.httprequest.environ["HTTP_API_KEY"] = self.api_key_good.key
81+
mocked_request.httprequest.headers = EnvironHeaders(
82+
mocked_request.httprequest.environ
83+
)
84+
self.assertTrue(self.env["ir.http"]._auth_method_api_key())

0 commit comments

Comments
 (0)