Skip to content

Commit bb06c3e

Browse files
committed
feat: Track utilized auth services
This PR expands the `identify_required_authn_params` helper function. Previously, it only identified missing auth params based on requirements and available token getters. Now, the helper also returns a list of the auth token getters that were actually used during the identification process. > [!NOTE] > This enhancement is a preparatory step for implementing `strict` flag validation in an upcoming PR, allowing us to determine if all provided authentication methods were necessary.
1 parent 574e39f commit bb06c3e

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

packages/toolbox-core/src/toolbox_core/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __parse_tool(
8181

8282
authn_params = identify_required_authn_params(
8383
authn_params, auth_token_getters.keys()
84-
)
84+
)[0]
8585

8686
tool = ToolboxTool(
8787
session=self.__session,

packages/toolbox-core/src/toolbox_core/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def add_auth_token_getters(
293293
new_req_authn_params = MappingProxyType(
294294
identify_required_authn_params(
295295
self.__required_authn_params, auth_token_getters.keys()
296-
)
296+
)[0]
297297
)
298298

299299
return self.__copy(

packages/toolbox-core/src/toolbox_core/utils.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,42 @@ def create_func_docstring(description: str, params: Sequence[ParameterSchema]) -
4646

4747
def identify_required_authn_params(
4848
req_authn_params: Mapping[str, list[str]], auth_service_names: Iterable[str]
49-
) -> dict[str, list[str]]:
49+
) -> tuple[dict[str, list[str]], set[str]]:
5050
"""
5151
Identifies authentication parameters that are still required; because they
52-
are not covered by the provided `auth_service_names`.
52+
are not covered by the provided `auth_service_names`, and also returns a
53+
set of all authentication services that were found to be matching.
5354
5455
Args:
55-
req_authn_params: A mapping of parameter names to sets of required
56+
req_authn_params: A mapping of parameter names to lists of required
5657
authentication services.
5758
auth_service_names: An iterable of authentication service names for which
5859
token getters are available.
5960
6061
Returns:
61-
A new dictionary representing the subset of required authentication parameters
62-
that are not covered by the provided `auth_service_names`.
62+
A tuple containing:
63+
- A new dictionary representing the subset of required
64+
authentication parameters that are not covered by the provided
65+
`auth_service_names`.
66+
- A list of authentication service names from `auth_service_names`
67+
that were found to satisfy at least one parameter's requirements.
6368
"""
64-
required_params = {} # params that are still required with provided auth_services
69+
required_params: dict[str, list[str]] = {}
70+
used_services: set[str] = set()
71+
6572
for param, services in req_authn_params.items():
6673
# if we don't have a token_getter for any of the services required by the param,
6774
# the param is still required
68-
required = not any(s in services for s in auth_service_names)
69-
if required:
75+
matched_services = [
76+
s for s in services if s in auth_service_names
77+
]
78+
79+
if matched_services:
80+
used_services.update(matched_services)
81+
else:
7082
required_params[param] = services
71-
return required_params
83+
84+
return required_params, used_services
7285

7386

7487
def params_to_pydantic_model(

packages/toolbox-core/tests/test_utils.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ def test_identify_required_authn_params_none_required():
8787
req_authn_params = {}
8888
auth_service_names = ["service_a", "service_b"]
8989
expected = {}
90+
expected_used = set()
9091
assert (
91-
identify_required_authn_params(req_authn_params, auth_service_names) == expected
92+
identify_required_authn_params(req_authn_params, auth_service_names) == (expected, expected_used)
9293
)
9394

9495

@@ -100,8 +101,9 @@ def test_identify_required_authn_params_all_covered():
100101
}
101102
auth_service_names = ["service_a", "service_b"]
102103
expected = {}
104+
expected_used = set(auth_service_names)
103105
assert (
104-
identify_required_authn_params(req_authn_params, auth_service_names) == expected
106+
identify_required_authn_params(req_authn_params, auth_service_names) == (expected, expected_used)
105107
)
106108

107109

@@ -118,8 +120,9 @@ def test_identify_required_authn_params_some_covered():
118120
"token_d": ["service_d"],
119121
"token_e": ["service_e", "service_f"],
120122
}
123+
expected_used = set(auth_service_names)
121124
assert (
122-
identify_required_authn_params(req_authn_params, auth_service_names) == expected
125+
identify_required_authn_params(req_authn_params, auth_service_names) == (expected, expected_used)
123126
)
124127

125128

@@ -134,8 +137,9 @@ def test_identify_required_authn_params_none_covered():
134137
"token_d": ["service_d"],
135138
"token_e": ["service_e", "service_f"],
136139
}
140+
expected_used = set()
137141
assert (
138-
identify_required_authn_params(req_authn_params, auth_service_names) == expected
142+
identify_required_authn_params(req_authn_params, auth_service_names) == (expected, expected_used)
139143
)
140144

141145

@@ -150,8 +154,9 @@ def test_identify_required_authn_params_no_available_services():
150154
"token_a": ["service_a"],
151155
"token_b": ["service_b", "service_c"],
152156
}
157+
expected_used = set()
153158
assert (
154-
identify_required_authn_params(req_authn_params, auth_service_names) == expected
159+
identify_required_authn_params(req_authn_params, auth_service_names) == (expected, expected_used)
155160
)
156161

157162

@@ -164,8 +169,9 @@ def test_identify_required_authn_params_empty_services_for_param():
164169
expected = {
165170
"token_x": [],
166171
}
172+
expected_used = set()
167173
assert (
168-
identify_required_authn_params(req_authn_params, auth_service_names) == expected
174+
identify_required_authn_params(req_authn_params, auth_service_names) == (expected, expected_used)
169175
)
170176

171177

0 commit comments

Comments
 (0)