Skip to content

Commit 7ef10a4

Browse files
committed
Merge branch '602-on-17-0-1-2-1' into openspp-17.0.1.2.1
2 parents 08849d2 + 47204ad commit 7ef10a4

File tree

13 files changed

+73
-47
lines changed

13 files changed

+73
-47
lines changed

spp_api/controllers/pinguin.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# fmt: off
3838
from odoo.addons.spp_base_api.lib.pinguin import error_response, get_dict_from_record, get_model_for_read
39-
from odoo.addons.spp_oauth.tools.rsa_encode_decode import verify_and_decode_signature
39+
from odoo.addons.spp_oauth.tools.rsa_encode_decode import OpenSPPOAuthJWTException, verify_and_decode_signature
4040

4141
# fmt: on
4242
from odoo.addons.web.controllers.main import ReportController
@@ -231,9 +231,12 @@ def get_data_from_bearer_auth_header(header):
231231
in the wrong format
232232
"""
233233
normalized_token = header.replace("Bearer ", "").replace("\\n", "").encode("utf-8")
234-
decoded, res = verify_and_decode_signature(normalized_token)
235-
if not decoded:
236-
raise werkzeug.exceptions.HTTPException(response=error_response(*CODE__no_user_auth))
234+
235+
try:
236+
res = verify_and_decode_signature(normalized_token)
237+
except OpenSPPOAuthJWTException as e:
238+
raise werkzeug.exceptions.HTTPException(response=error_response(*CODE__no_user_auth)) from e
239+
237240
if not all([key in res.keys() for key in ("database", "token")]):
238241
err_descrip = 'Bearer auth header payload must include "database" & "token"'
239242
raise werkzeug.exceptions.HTTPException(response=error_response(500, "Invalid header", err_descrip))

spp_base_gis_rest/controllers/controllers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from odoo.http import Controller, request, route
77
from odoo.tools import date_utils
88

9-
from odoo.addons.spp_oauth.tools import verify_and_decode_signature
9+
from odoo.addons.spp_oauth.tools import OpenSPPOAuthJWTException, verify_and_decode_signature
1010

1111
ALLOWED_LAYER_TYPE = [
1212
"point",
@@ -107,7 +107,12 @@ def verify_auth_header():
107107

108108
if auth_header.startswith("Bearer "):
109109
access_token = auth_header.replace("Bearer ", "").replace("\\n", "").encode("utf-8")
110-
verified, _ = verify_and_decode_signature(access_token)
110+
try:
111+
verify_and_decode_signature(access_token)
112+
verified = True
113+
except OpenSPPOAuthJWTException:
114+
verified = False
115+
111116
elif auth_header.startswith("Basic "):
112117
access_token = auth_header.replace("Basic ", "").replace("\\n", "").encode("utf-8")
113118
verified = verify_and_decode_token(access_token)

spp_base_gis_rest/models/api_client_credentials.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import calendar
2+
import os
23
import uuid
34
from datetime import datetime, timedelta
45

@@ -7,6 +8,8 @@
78

89
from odoo.addons.spp_oauth.tools import calculate_signature
910

11+
TOKEN_EXPIRATION_MIN = os.environ.get("SPP_BASE_GIS_TOKEN_EXP_MIN", 10)
12+
1013

1114
class GisApiClientCredential(models.Model):
1215
_name = "spp.gis.api.client.credential"
@@ -65,14 +68,12 @@ def _generate_client_token(self):
6568
),
6669
]
6770

68-
TOKEN_EXPIRATION_MIN = 10
69-
7071
ALLOW_EXPORT = False
7172

7273
@api.model
7374
def generate_access_token(self):
7475
today = datetime.today()
75-
expiry_datetime = today + timedelta(minutes=self.TOKEN_EXPIRATION_MIN)
76+
expiry_datetime = today + timedelta(minutes=TOKEN_EXPIRATION_MIN)
7677

7778
header = {"alg": "RS256", "typ": "JWT"}
7879
payload = {

spp_dci_api_server/controllers/controllers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from odoo.service.db import list_dbs
1010
from odoo.tools import date_utils
1111

12-
from odoo.addons.spp_oauth.tools import verify_and_decode_signature
12+
from odoo.addons.spp_oauth.tools import OpenSPPOAuthJWTException, verify_and_decode_signature
1313

1414
from ..tools import constants
1515

@@ -139,9 +139,9 @@ def retrieve_registry(self, **kw):
139139

140140
access_token = auth_header.replace("Bearer ", "").replace("\\n", "").encode("utf-8")
141141

142-
verified, payload = verify_and_decode_signature(access_token)
143-
144-
if not verified:
142+
try:
143+
payload = verify_and_decode_signature(access_token)
144+
except OpenSPPOAuthJWTException:
145145
return error_wrapper(401, "Invalid Access Token.")
146146

147147
req = request

spp_dci_api_server/models/client_credentials.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import calendar
2+
import os
23
import uuid
34
from datetime import datetime, timedelta
45

@@ -7,6 +8,8 @@
78

89
from odoo.addons.spp_oauth.tools import calculate_signature
910

11+
TOKEN_EXPIRATION_MIN = os.environ.get("SPP_DCI_API_SERVER_TOKEN_EXP_MIN", 10)
12+
1013

1114
class ClientCredential(models.Model):
1215
_name = "spp.dci.api.client.credential"
@@ -43,14 +46,12 @@ def _generate_client_secret(self):
4346
),
4447
]
4548

46-
TOKEN_EXPIRATION_MIN = 10
47-
4849
ALLOW_EXPORT = False
4950

5051
@api.model
5152
def generate_access_token(self, db_name):
5253
today = datetime.today()
53-
expiry_datetime = today + timedelta(minutes=self.TOKEN_EXPIRATION_MIN)
54+
expiry_datetime = today + timedelta(minutes=TOKEN_EXPIRATION_MIN)
5455

5556
header = {"alg": "RS256", "typ": "JWT"}
5657
payload = {

spp_dci_api_server/views/client_credentials_view.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151
<field name="client_id" widget="CopyClipboardChar" string="Client ID" />
5252
<field name="client_secret" widget="CopyClipboardChar" />
5353
</group>
54+
<footer>
5455
<button string="OK" special="cancel" class="oe_highlight" />
56+
</footer>
5557
</sheet>
5658
</form>
5759
</field>

spp_import_dci_api/models/fetch_crvs_beneficiary.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,13 @@ def create_locations(self):
201201

202202
if location_path:
203203
url = f"{data_source_id.url}{location_path}"
204-
response = requests.get(url, timeout=constants.REQUEST_TIMEOUT)
205-
if response.ok:
206-
result = response.json()
207-
self.process_location(result)
204+
try:
205+
response = requests.get(url, timeout=constants.REQUEST_TIMEOUT)
206+
if response.ok:
207+
result = response.json()
208+
self.process_location(result)
209+
except requests.exceptions.ConnectionError as e:
210+
_logger.error(e)
208211

209212
def get_parent(self):
210213
"""
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
from .field_onchange import field_onchange
2-
from .calculate_signature import calculate_signature

spp_import_dci_api/tools/calculate_signature.py

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

spp_oauth/tests/test_rsa_encode_decode.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ def test_calculate_signature(self, mock_encode):
1515
signature = calculate_signature(header, payload)
1616

1717
self.assertEqual(signature, "mocked_signature")
18-
mock_encode.assert_called_once_with(headers=header, payload=payload, key="", algorithm="RS256")
1918

2019
@patch("odoo.addons.spp_oauth.tools.rsa_encode_decode.jwt.decode")
2120
def test_verify_and_decode_signature(self, mock_decode):
2221
mock_decode.return_value = {"data": "test"}
2322

2423
access_token = "mocked_access_token"
2524

26-
success, decoded = verify_and_decode_signature(access_token)
25+
decoded = verify_and_decode_signature(access_token)
2726

28-
self.assertTrue(success)
2927
self.assertEqual(decoded, {"data": "test"})
30-
mock_decode.assert_called_once_with(access_token, key="", algorithms=["RS256"])

0 commit comments

Comments
 (0)