diff --git a/endpoint/demo/endpoint_demo.xml b/endpoint/demo/endpoint_demo.xml index 290cfe30..611b8dd8 100644 --- a/endpoint/demo/endpoint_demo.xml +++ b/endpoint/demo/endpoint_demo.xml @@ -78,6 +78,17 @@ result = {"response": Response(request.params.get("your_name", ""))} public +result = {"payload": "Method used:" + request.httprequest.method} + + + + + Demo Endpoint 8 + /demo/auth_bearer + GET + code + bearer + result = {"payload": "Method used:" + request.httprequest.method} diff --git a/endpoint/models/endpoint_mixin.py b/endpoint/models/endpoint_mixin.py index 4b98074d..07e4dbf8 100644 --- a/endpoint/models/endpoint_mixin.py +++ b/endpoint/models/endpoint_mixin.py @@ -229,6 +229,8 @@ def _validate_request(self, request): ): self._logger.error("_validate_request: UnsupportedMediaType") raise werkzeug.exceptions.UnsupportedMediaType() + if self.auth_type == "bearer": + request.env["ir.http"]._auth_method_bearer() def _get_handler(self): try: diff --git a/endpoint/readme/CONTRIBUTORS.md b/endpoint/readme/CONTRIBUTORS.md index 2b66303a..c68f17e0 100644 --- a/endpoint/readme/CONTRIBUTORS.md +++ b/endpoint/readme/CONTRIBUTORS.md @@ -1 +1,2 @@ - Simone Orsi \<\> +- 张飞虎 \<\> \ No newline at end of file diff --git a/endpoint/tests/test_endpoint_controller.py b/endpoint/tests/test_endpoint_controller.py index fb5e84fd..72a3e045 100644 --- a/endpoint/tests/test_endpoint_controller.py +++ b/endpoint/tests/test_endpoint_controller.py @@ -4,6 +4,7 @@ import json import os +from datetime import datetime, timedelta from unittest import skipIf from odoo.tests.common import HttpCase @@ -77,3 +78,25 @@ def test_call6(self): def test_call7(self): response = self.url_open("/demo/bad_method", data="ok") self.assertEqual(response.status_code, 405) + + def test_call8(self): + response = self.url_open("/demo/auth_bearer") + self.assertEqual(response.status_code, 401) + + response = self.url_open( + "/demo/auth_bearer", headers={"Authorization": "Bearer bad_key"} + ) + self.assertEqual(response.status_code, 401) + + expiration_date = datetime.today() + timedelta(days=1) + demo_user = self.env.ref("base.user_demo") + api_key = ( + self.env["res.users.apikeys"] + .with_user(demo_user) + ._generate(None, "Test api key", expiration_date) + ) + demo_user.api_key_ids.flush_model() + response = self.url_open( + "/demo/auth_bearer", headers={"Authorization": f"Bearer {api_key}"} + ) + self.assertEqual(response.status_code, 200) diff --git a/endpoint/views/endpoint_view.xml b/endpoint/views/endpoint_view.xml index a267153d..14645564 100644 --- a/endpoint/views/endpoint_view.xml +++ b/endpoint/views/endpoint_view.xml @@ -71,7 +71,11 @@ - + + + + diff --git a/endpoint_route_handler/models/endpoint_route_handler.py b/endpoint_route_handler/models/endpoint_route_handler.py index 19a8c766..d96ac5a8 100644 --- a/endpoint_route_handler/models/endpoint_route_handler.py +++ b/endpoint_route_handler/models/endpoint_route_handler.py @@ -111,7 +111,7 @@ def _selection_route_type(self): return [("http", "HTTP"), ("json", "JSON")] def _selection_auth_type(self): - return [("public", "Public"), ("user_endpoint", "User")] + return [("public", "Public"), ("user_endpoint", "User"), ("bearer", "Bearer")] def _selection_request_method(self): return [ diff --git a/endpoint_route_handler/tests/test_endpoint.py b/endpoint_route_handler/tests/test_endpoint.py index 2dfde2b1..feca13d8 100644 --- a/endpoint_route_handler/tests/test_endpoint.py +++ b/endpoint_route_handler/tests/test_endpoint.py @@ -48,6 +48,12 @@ def test_as_tool_base_data(self): self.assertTrue(first_hash) new_route.route += "/new" self.assertNotEqual(new_route.endpoint_hash, first_hash) + self.assertIn("http", dict(new_route._selection_route_type())) + self.assertIn("POST", dict(new_route._selection_request_method())) + self.assertIn("bearer", dict(new_route._selection_auth_type())) + self.assertIn( + "application/json", dict(new_route._selection_request_content_type()) + ) @mute_logger("odoo.addons.base.models.ir_http") def test_as_tool_register_single_controller(self):