Skip to content
Merged
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
20 changes: 12 additions & 8 deletions src/a2a/compat/v0_3/proto_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,9 @@ def oauth2_flows(cls, flows: types.OAuthFlows) -> a2a_pb2.OAuthFlows:
token_url=flows.password.token_url,
Comment thread
Iwaniukooo11 marked this conversation as resolved.
Resolved
),
)
raise ValueError('Unknown oauth flow definition')
# No v0.3 flow is set, e.g. for a v1.0 device code flow, which v0.3
# does not have.
return a2a_pb2.OAuthFlows()

@classmethod
def skill(cls, skill: types.AgentSkill) -> a2a_pb2.AgentSkill:
Expand Down Expand Up @@ -1058,13 +1060,15 @@ def oauth2_flows(cls, flows: a2a_pb2.OAuthFlows) -> types.OAuthFlows:
scopes=dict(flows.implicit.scopes.items()),
),
)
return types.OAuthFlows(
password=types.PasswordOAuthFlow(
refresh_url=flows.password.refresh_url,
scopes=dict(flows.password.scopes.items()),
token_url=flows.password.token_url,
),
)
if flows.HasField('password'):
return types.OAuthFlows(
password=types.PasswordOAuthFlow(
refresh_url=flows.password.refresh_url,
scopes=dict(flows.password.scopes.items()),
token_url=flows.password.token_url,
),
)
return types.OAuthFlows()

@classmethod
def stream_response(
Expand Down
39 changes: 39 additions & 0 deletions tests/compat/v0_3/test_grpc_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,42 @@ async def test_extract_task_and_config_id_invalid(
):
with pytest.raises(InvalidParamsError):
handler._extract_task_and_config_id('invalid-name')


@pytest.mark.asyncio
async def test_get_agent_card_with_device_code_oauth_flow(
handler: compat_grpc_handler.CompatGrpcHandler,
mock_request_handler: AsyncMock,
mock_grpc_context: AsyncMock,
sample_agent_card: a2a_pb2.AgentCard,
) -> None:
"""A v1.0 device code flow, which v0.3 lacks, is sent with no flows."""
sample_agent_card.security_schemes['device'].CopyFrom(
a2a_pb2.SecurityScheme(
oauth2_security_scheme=a2a_pb2.OAuth2SecurityScheme(
description='Device code',
flows=a2a_pb2.OAuthFlows(
device_code=a2a_pb2.DeviceCodeOAuthFlow(
device_authorization_url='https://auth.example.com/device',
token_url='https://auth.example.com/token',
scopes={'read': 'Read access'},
)
),
)
)
)
mock_request_handler.on_get_extended_agent_card.return_value = (
sample_agent_card
)

response = await handler.GetAgentCard(
a2a_v0_3_pb2.GetAgentCardRequest(), mock_grpc_context
)

mock_grpc_context.abort.assert_not_called()
assert response.security_schemes['device'] == a2a_v0_3_pb2.SecurityScheme(
oauth2_security_scheme=a2a_v0_3_pb2.OAuth2SecurityScheme(
description='Device code',
flows=a2a_v0_3_pb2.OAuthFlows(),
)
)
10 changes: 7 additions & 3 deletions tests/compat/v0_3/test_proto_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,13 @@ def test_oauth_flows_conversion(self):
proto_auth_code_flow = proto_utils.ToProto.oauth2_flows(auth_code_flow)
assert proto_auth_code_flow.HasField('authorization_code')

# Test invalid flow
with pytest.raises(ValueError):
proto_utils.ToProto.oauth2_flows(types.OAuthFlows())
# Test no flow, e.g. a v1.0 device code flow converted to v0.3
proto_no_flow = proto_utils.ToProto.oauth2_flows(types.OAuthFlows())
assert proto_no_flow == a2a_pb2.OAuthFlows()
assert (
proto_utils.FromProto.oauth2_flows(proto_no_flow)
== types.OAuthFlows()
)

# Test FromProto
roundtrip_password = proto_utils.FromProto.oauth2_flows(
Expand Down
15 changes: 14 additions & 1 deletion tests/integration/cross_version/client_server/client_0_3.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
FileWithBytes,
FileWithUri,
Message,
OAuth2SecurityScheme,
Part,
Role,
TaskIdParams,
Expand Down Expand Up @@ -224,7 +225,19 @@ async def test_get_extended_agent_card(client):
print(
f'card.supports_authenticated_extended_card is: {card.supports_authenticated_extended_card}'
)
assert card.supports_authenticated_extended_card in (False, None)
assert card.supports_authenticated_extended_card is True

scheme = card.security_schemes['oauth'].root
assert isinstance(scheme, OAuth2SecurityScheme)
assert scheme.description == 'Device code'
# v0.3 has no device code flow, so the server sends no flows. The
# v0.3 gRPC client reads empty flows as a password flow with empty
# fields.
flows = scheme.flows
assert flows.authorization_code is None
assert flows.client_credentials is None
assert flows.implicit is None
assert flows.password is None or flows.password.token_url == ''

print(f'Success: get_extended_agent_card passed.')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ async def test_get_extended_agent_card(client):

if card.name == 'Server 1.0':
assert len(card.supported_interfaces) == 4
assert card.capabilities.extended_agent_card in (False, None)
assert card.capabilities.extended_agent_card is True
flows = card.security_schemes['oauth'].oauth2_security_scheme.flows
assert flows.device_code.token_url == 'https://auth.example.com/token'
else:
assert len(card.supported_interfaces) > 0
assert card.capabilities.extended_agent_card in (False, None)
Expand Down
26 changes: 25 additions & 1 deletion tests/integration/cross_version/client_server/server_1_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
AgentCapabilities,
AgentCard,
AgentInterface,
DeviceCodeOAuthFlow,
OAuth2SecurityScheme,
OAuthFlows,
Part,
SecurityScheme,
TaskState,
)
from a2a.utils import TransportProtocol
Expand Down Expand Up @@ -138,7 +142,27 @@ async def main_async(http_port: int, grpc_port: int):
description='Server running on a2a v1.0',
version='1.0.0',
skills=[],
capabilities=AgentCapabilities(streaming=True, push_notifications=True),
capabilities=AgentCapabilities(
streaming=True,
push_notifications=True,
# Makes v0.3 clients fetch the extended card from the server.
extended_agent_card=True,
),
# v0.3 has no device code flow, so v0.3 clients get no flows.
security_schemes={
'oauth': SecurityScheme(
oauth2_security_scheme=OAuth2SecurityScheme(
description='Device code',
flows=OAuthFlows(
device_code=DeviceCodeOAuthFlow(
device_authorization_url='https://auth.example.com/device',
token_url='https://auth.example.com/token',
scopes={'read': 'Read access'},
)
),
)
)
},
default_input_modes=['text/plain'],
default_output_modes=['text/plain'],
supported_interfaces=[
Expand Down
Loading