Skip to content

Commit 81d3a97

Browse files
committed
[18.0][IMP] Add Bearer token authentication support to endpoint
1 parent 6713cd9 commit 81d3a97

File tree

7 files changed

+56
-2
lines changed

7 files changed

+56
-2
lines changed

endpoint/demo/endpoint_demo.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ result = {"response": Response(request.params.get("your_name", ""))}
7878
<field name="auth_type">public</field>
7979
<field name="exec_as_user_id" ref="base.user_demo" />
8080
<field name="code_snippet">
81+
result = {"payload": "Method used:" + request.httprequest.method}
82+
</field>
83+
</record>
84+
85+
<record id="endpoint_demo_8" model="endpoint.endpoint">
86+
<field name="name">Demo Endpoint 8</field>
87+
<field name="route">/demo/auth_bearer</field>
88+
<field name="request_method">GET</field>
89+
<field name="exec_mode">code</field>
90+
<field name="auth_type">bearer</field>
91+
<field name="code_snippet">
8192
result = {"payload": "Method used:" + request.httprequest.method}
8293
</field>
8394
</record>

endpoint/models/endpoint_mixin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def _validate_request(self, request):
229229
):
230230
self._logger.error("_validate_request: UnsupportedMediaType")
231231
raise werkzeug.exceptions.UnsupportedMediaType()
232+
if self.auth_type == "bearer":
233+
request.env["ir.http"]._auth_method_bearer()
232234

233235
def _get_handler(self):
234236
try:

endpoint/readme/CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
- Simone Orsi \<<simone.orsi@camptocamp.com>\>
2+
- 张飞虎 \<<feihu.zhang@orado.cn>\>

endpoint/tests/test_endpoint_controller.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import json
66
import os
7+
from datetime import datetime, timedelta
78
from unittest import skipIf
89

910
from odoo.tests.common import HttpCase
@@ -77,3 +78,25 @@ def test_call6(self):
7778
def test_call7(self):
7879
response = self.url_open("/demo/bad_method", data="ok")
7980
self.assertEqual(response.status_code, 405)
81+
82+
def test_call8(self):
83+
response = self.url_open("/demo/auth_bearer")
84+
self.assertEqual(response.status_code, 401)
85+
86+
response = self.url_open(
87+
"/demo/auth_bearer", headers={"Authorization": "Bearer bad_key"}
88+
)
89+
self.assertEqual(response.status_code, 401)
90+
91+
expiration_date = datetime.today() + timedelta(days=1)
92+
demo_user = self.env.ref("base.user_demo")
93+
api_key = (
94+
self.env["res.users.apikeys"]
95+
.with_user(demo_user)
96+
._generate(None, "Test api key", expiration_date)
97+
)
98+
demo_user.api_key_ids.flush_model()
99+
response = self.url_open(
100+
"/demo/auth_bearer", headers={"Authorization": f"Bearer {api_key}"}
101+
)
102+
self.assertEqual(response.status_code, 200)

endpoint/views/endpoint_view.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@
7171
</group>
7272
</page>
7373
<page name="code" string="Code" invisible="exec_mode != 'code'">
74-
<field name="code_snippet" widget="ace" />
74+
<field
75+
name="code_snippet"
76+
widget="ace"
77+
options="{'mode':'python'}"
78+
/>
7579
</page>
7680
<page
7781
name="code_help"
@@ -125,6 +129,13 @@
125129
name="inactive"
126130
domain="[('active', '=', False)]"
127131
/>
132+
<group>
133+
<filter
134+
name="group_route_group"
135+
string="Group"
136+
context="{'group_by':'route_group'}"
137+
/>
138+
</group>
128139
</search>
129140
</field>
130141
</record>

endpoint_route_handler/models/endpoint_route_handler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _selection_route_type(self):
111111
return [("http", "HTTP"), ("json", "JSON")]
112112

113113
def _selection_auth_type(self):
114-
return [("public", "Public"), ("user_endpoint", "User")]
114+
return [("public", "Public"), ("user_endpoint", "User"), ("bearer", "Bearer")]
115115

116116
def _selection_request_method(self):
117117
return [

endpoint_route_handler/tests/test_endpoint.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def test_as_tool_base_data(self):
4848
self.assertTrue(first_hash)
4949
new_route.route += "/new"
5050
self.assertNotEqual(new_route.endpoint_hash, first_hash)
51+
self.assertIn("http", dict(new_route._selection_route_type()))
52+
self.assertIn("POST", dict(new_route._selection_request_method()))
53+
self.assertIn("bearer", dict(new_route._selection_auth_type()))
54+
self.assertIn(
55+
"application/json", dict(new_route._selection_request_content_type())
56+
)
5157

5258
@mute_logger("odoo.addons.base.models.ir_http")
5359
def test_as_tool_register_single_controller(self):

0 commit comments

Comments
 (0)