Skip to content

Commit f495a37

Browse files
esunejamshale
andauthored
Fixed handling of base wallet routes in auth decorator (openwallet-foundation#3448)
* Fixed handling of base wallet routes in auth decorator Signed-off-by: Emiliano Suñé <[email protected]> * Use multiple routes in test case Signed-off-by: Emiliano Suñé <[email protected]> --------- Signed-off-by: Emiliano Suñé <[email protected]> Co-authored-by: jamshale <[email protected]>
1 parent 315537c commit f495a37

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

acapy_agent/admin/decorators/auth.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import functools
44
import re
5-
from typing import Optional, Pattern
5+
from typing import List, Optional, Pattern
66

77
from aiohttp import web
88

@@ -65,8 +65,12 @@ async def tenant_auth(request):
6565
)
6666
insecure_mode = bool(profile.settings.get("admin.admin_insecure_mode"))
6767
multitenant_enabled = profile.settings.get("multitenant.enabled")
68+
base_wallet_routes = profile.settings.get("multitenant.base_wallet_routes")
6869
base_wallet_allowed_route = _base_wallet_route_access(
69-
profile.settings.get("multitenant.base_wallet_routes"), request.path
70+
[base_wallet_routes]
71+
if isinstance(base_wallet_routes, str)
72+
else base_wallet_routes,
73+
request.path,
7074
)
7175

7276
# CORS fix: allow OPTIONS method access to paths without a token
@@ -88,19 +92,22 @@ async def tenant_auth(request):
8892
return tenant_auth
8993

9094

91-
def _base_wallet_route_access(additional_routes: str, request_path: str) -> bool:
95+
def _base_wallet_route_access(additional_routes: List[str], request_path: str) -> bool:
9296
"""Check if request path matches additional routes."""
93-
additional_routes_pattern = _build_additional_routes_pattern(additional_routes)
97+
additional_routes_pattern = (
98+
_build_additional_routes_pattern(additional_routes) if additional_routes else None
99+
)
94100
return _matches_additional_routes(additional_routes_pattern, request_path)
95101

96102

97-
def _build_additional_routes_pattern(pattern_string: str) -> Optional[Pattern]:
103+
def _build_additional_routes_pattern(pattern_list: List[str]) -> Optional[Pattern]:
98104
"""Build pattern from space delimited list of paths."""
99105
# create array and add word boundary to avoid false positives
100-
if pattern_string:
101-
paths = pattern_string.split(" ")
102-
return re.compile("^((?:)" + "|".join(paths) + ")$")
103-
return None
106+
all_paths = []
107+
for pattern in pattern_list:
108+
paths = pattern.split(" ")
109+
all_paths = all_paths + paths
110+
return re.compile("^((?:)" + "|".join(all_paths) + ")$")
104111

105112

106113
def _matches_additional_routes(pattern: Pattern, path: str) -> bool:

acapy_agent/admin/tests/test_auth.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,25 @@ async def test_multi_tenant_valid_auth_header(self):
134134
await decor_func(self.request)
135135
self.decorated_handler.assert_called_once_with(self.request)
136136

137-
async def test_base_wallet_additional_route_allowed(self):
138-
self.profile.settings["multitenant.base_wallet_routes"] = "/extra-route"
137+
async def test_base_wallet_additional_route_allowed_string(self):
138+
self.profile.settings["multitenant.base_wallet_routes"] = (
139+
"/not-this-route /extra-route"
140+
)
141+
self.request = mock.MagicMock(
142+
__getitem__=lambda _, k: self.request_dict[k],
143+
headers={"x-api-key": "admin_api_key"},
144+
method="POST",
145+
path="/extra-route",
146+
)
147+
decor_func = tenant_authentication(self.decorated_handler)
148+
await decor_func(self.request)
149+
self.decorated_handler.assert_called_once_with(self.request)
150+
151+
async def test_base_wallet_additional_route_allowed_list(self):
152+
self.profile.settings["multitenant.base_wallet_routes"] = [
153+
"/extra-route",
154+
"/not-this-route",
155+
]
139156
self.request = mock.MagicMock(
140157
__getitem__=lambda _, k: self.request_dict[k],
141158
headers={"x-api-key": "admin_api_key"},

0 commit comments

Comments
 (0)