Skip to content

Commit e02b115

Browse files
committed
fix(application-integration): use exchanged auth credential for connector tools
1 parent 1445ad5 commit e02b115

File tree

2 files changed

+75
-9
lines changed

2 files changed

+75
-9
lines changed

src/google/adk/tools/application_integration_tool/application_integration_toolset.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,19 @@ async def get_tools(
275275
self,
276276
readonly_context: Optional[ReadonlyContext] = None,
277277
) -> List[RestApiTool]:
278-
return (
279-
[
280-
tool
281-
for tool in self._tools
282-
if self._is_tool_selected(tool, readonly_context)
283-
]
284-
if self._openapi_toolset is None
285-
else await self._openapi_toolset.get_tools(readonly_context)
286-
)
278+
if self._openapi_toolset is not None:
279+
return await self._openapi_toolset.get_tools(readonly_context)
280+
281+
if self._auth_config and self._auth_config.exchanged_auth_credential:
282+
for tool in self._tools:
283+
if isinstance(tool, IntegrationConnectorTool) and tool._auth_scheme:
284+
tool._auth_credential = self._auth_config.exchanged_auth_credential
285+
286+
return [
287+
tool
288+
for tool in self._tools
289+
if self._is_tool_selected(tool, readonly_context)
290+
]
287291

288292
@override
289293
async def close(self) -> None:

tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,65 @@ async def test_init_with_connection_with_auth_override_disabled_and_custom_auth(
671671
assert (await toolset.get_tools())[0]._operation == "EXECUTE_ACTION"
672672
assert not (await toolset.get_tools())[0]._auth_scheme
673673
assert not (await toolset.get_tools())[0]._auth_credential
674+
675+
676+
@pytest.mark.asyncio
677+
async def test_get_tools_uses_exchanged_auth_credential_when_available(
678+
project,
679+
location,
680+
mock_integration_client,
681+
mock_connections_client,
682+
mock_openapi_action_spec_parser,
683+
connection_details_auth_override_enabled,
684+
):
685+
connection_name = "test-connection"
686+
actions_list = ["create"]
687+
mock_connections_client.return_value.get_connection_details.return_value = (
688+
connection_details_auth_override_enabled
689+
)
690+
691+
oauth2_data_google_cloud = {
692+
"type": "oauth2",
693+
"flows": {
694+
"authorizationCode": {
695+
"authorizationUrl": "https://test-url/o/oauth2/auth",
696+
"tokenUrl": "https://test-url/token",
697+
"scopes": {
698+
"https://test-url/auth/test-scope": "test scope",
699+
},
700+
}
701+
},
702+
}
703+
704+
oauth2_scheme = dict_to_auth_scheme(oauth2_data_google_cloud)
705+
raw_auth_credential = AuthCredential(
706+
auth_type=AuthCredentialTypes.OAUTH2,
707+
oauth2=OAuth2Auth(
708+
client_id="test-client-id",
709+
client_secret="test-client-secret",
710+
),
711+
)
712+
713+
toolset = ApplicationIntegrationToolset(
714+
project,
715+
location,
716+
connection=connection_name,
717+
actions=actions_list,
718+
auth_scheme=oauth2_scheme,
719+
auth_credential=raw_auth_credential,
720+
)
721+
722+
exchanged_auth_credential = AuthCredential(
723+
auth_type=AuthCredentialTypes.OAUTH2,
724+
oauth2=OAuth2Auth(
725+
client_id="test-client-id",
726+
client_secret="test-client-secret",
727+
access_token="exchanged-access-token",
728+
),
729+
)
730+
toolset._auth_config.exchanged_auth_credential = exchanged_auth_credential
731+
732+
tools = await toolset.get_tools()
733+
734+
assert len(tools) == 1
735+
assert tools[0]._auth_credential == exchanged_auth_credential

0 commit comments

Comments
 (0)