Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/a2a/client/card_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,40 @@ def _map_legacy_security(
]


# v0.3 OAuthFlows fields in the order `to_core_oauth_flows` applies them.
_LEGACY_OAUTH_FLOWS = (
'authorizationCode',
'clientCredentials',
'implicit',
'password',
)


def _keep_single_legacy_oauth_flow(scheme: dict[str, Any]) -> None:
"""Reduce a v0.3 OAuthFlows object to the single flow 1.0.0 allows.

In v0.3 every flow is an optional field, so a card may list several. In
1.0.0 `OAuthFlows` is a oneof. Keep the flow the v0.3 compat conversion
keeps (the last one in `_LEGACY_OAUTH_FLOWS`) so both paths agree.
"""
flows = scheme.get('flows')
if not isinstance(flows, dict):
return
present = [
name for name in _LEGACY_OAUTH_FLOWS if flows.get(name) is not None
]
if len(present) <= 1:
return
kept = present[-1]
logger.warning(
'Legacy OAuth2 security scheme lists flows %s; keeping only %r.',
present,
kept,
)
for name in present[:-1]:
del flows[name]


def _handle_security_compatibility(agent_card_data: dict[str, Any]) -> None:
"""Map legacy security requirements and schemas to their 1.0.0 Protobuf equivalents."""
legacy_security = agent_card_data.pop('security', None)
Expand Down Expand Up @@ -121,6 +155,8 @@ def _handle_security_compatibility(agent_card_data: dict[str, Any]) -> None:
# Map legacy 'in' to modern 'location'
if scheme_type == 'apiKey' and 'in' in scheme:
scheme['location'] = scheme.pop('in')
if scheme_type == 'oauth2':
_keep_single_legacy_oauth_flow(scheme)

mapped_name = type_mapping[scheme_type]
new_scheme_wrapper = {mapped_name: scheme.copy()}
Expand Down
45 changes: 45 additions & 0 deletions tests/client/test_card_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,51 @@ def test_parse_agent_card_security_scheme_unknown_type(self) -> None:
'scheme'
)

def test_parse_030_agent_card_oauth2_with_several_flows(self) -> None:
flows = {
'authorizationCode': {
'authorizationUrl': 'https://auth.example.com/authorize',
'tokenUrl': 'https://auth.example.com/token',
'scopes': {'read': 'Read access'},
},
'clientCredentials': {
'tokenUrl': 'https://auth.example.com/token',
'scopes': {'read': 'Read access'},
},
}
data = {
'protocolVersion': '0.3.0',
'name': 'OAuth Agent',
'description': 'Lists two OAuth flows',
'url': 'http://agent.example.com/api',
'version': '1.0',
'securitySchemes': {
'oauth': {'type': 'oauth2', 'flows': copy.deepcopy(flows)}
},
}

card = parse_agent_card(data)

scheme = card.security_schemes['oauth'].oauth2_security_scheme
assert scheme.flows.WhichOneof('flow') == 'client_credentials'
assert (
scheme.flows.client_credentials.token_url
== 'https://auth.example.com/token'
)

# A 1.0 card must still carry a single flow.
with pytest.raises(ParseError):
parse_agent_card(
{
'name': 'OAuth Agent',
'description': 'Lists two OAuth flows',
'version': '1.0',
'securitySchemes': {
'oauth': {'oauth2SecurityScheme': {'flows': flows}}
},
}
)

def test_parse_030_agent_card_route_planner(self) -> None:
data = {
'protocolVersion': '0.3',
Expand Down
Loading