diff --git a/aries_cloudagent/core/oob_processor.py b/aries_cloudagent/core/oob_processor.py index c3a99a64b1..91ade823c6 100644 --- a/aries_cloudagent/core/oob_processor.py +++ b/aries_cloudagent/core/oob_processor.py @@ -2,7 +2,7 @@ import json import logging -from typing import Any, Callable, Dict, List, Optional, cast +from typing import Any, Callable, Dict, List, Optional from ..messaging.agent_message import AgentMessage from ..connections.models.conn_record import ConnRecord @@ -91,16 +91,18 @@ async def find_oob_target_for_outbound_message( oob_record.their_service, ) - their_service = ServiceDecorator.deserialize(oob_record.their_service) + their_service = oob_record.their_service + if not their_service: + raise OobMessageProcessorError("Could not determine their service") # Attach ~service decorator so other message can respond message = json.loads(outbound_message.payload) - if not message.get("~service"): + if not message.get("~service") and oob_record.our_service: LOGGER.debug( "Setting our service on the message ~service %s", oob_record.our_service, ) - message["~service"] = oob_record.our_service + message["~service"] = oob_record.our_service.serialize() message["~thread"] = { **message.get("~thread", {}), @@ -256,24 +258,15 @@ async def find_oob_record_for_inbound_message( ) return None - their_service = ( - cast( - ServiceDecorator, - ServiceDecorator.deserialize(oob_record.their_service), - ) - if oob_record.their_service - else None - ) - # Verify the sender key is present in their service in our record # If we don't have the sender verkey stored yet we can allow any key - if their_service and ( + if oob_record.their_service and ( ( context.message_receipt.recipient_verkey and ( not context.message_receipt.sender_verkey or context.message_receipt.sender_verkey - not in their_service.recipient_keys + not in oob_record.their_service.recipient_keys ) ) ): @@ -357,7 +350,7 @@ async def handle_message( LOGGER.debug( "Storing their service in oob record %s", their_service ) - oob_record.their_service = their_service.serialize() + oob_record.their_service = their_service await oob_record.save(session) diff --git a/aries_cloudagent/core/tests/test_oob_processor.py b/aries_cloudagent/core/tests/test_oob_processor.py index 9559e9bde5..c7e42e2b81 100644 --- a/aries_cloudagent/core/tests/test_oob_processor.py +++ b/aries_cloudagent/core/tests/test_oob_processor.py @@ -31,11 +31,11 @@ async def asyncSetUp(self): self.oob_record = mock.MagicMock( connection_id="a-connection-id", attach_thread_id="the-thid", - their_service={ - "recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], - "routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"], - "serviceEndpoint": "http://their-service-endpoint.com", - }, + their_service=ServiceDecorator( + recipient_keys=["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], + routing_keys=["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"], + endpoint="http://their-service-endpoint.com", + ), emit_event=mock.CoroutineMock(), delete_record=mock.CoroutineMock(), save=mock.CoroutineMock(), @@ -121,16 +121,16 @@ async def test_find_oob_target_for_outbound_message(self): invitation=mock.MagicMock(requests_attach=[]), invi_msg_id="the-pthid", our_recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", - their_service={ - "recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], - "serviceEndpoint": "http://their-service-endpoint.com", - "routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"], - }, - our_service={ - "recipientKeys": ["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"], - "serviceEndpoint": "http://our-service-endpoint.com", - "routingKeys": [], - }, + their_service=ServiceDecorator( + recipient_keys=["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], + endpoint="http://their-service-endpoint.com", + routing_keys=["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"], + ), + our_service=ServiceDecorator( + recipient_keys=["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"], + endpoint="http://our-service-endpoint.com", + routing_keys=[], + ), ) message = json.dumps({"~thread": {"thid": "the-thid"}}) @@ -196,16 +196,16 @@ async def test_find_oob_target_for_outbound_message_update_service_thread(self): invitation=mock.MagicMock(requests_attach=[]), invi_msg_id="the-pthid", our_recipient_key="3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx", - their_service={ - "recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], - "serviceEndpoint": "http://their-service-endpoint.com", - "routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"], - }, - our_service={ - "recipientKeys": ["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"], - "serviceEndpoint": "http://our-service-endpoint.com", - "routingKeys": [], - }, + their_service=ServiceDecorator( + recipient_keys=["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], + endpoint="http://their-service-endpoint.com", + routing_keys=["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"], + ), + our_service=ServiceDecorator( + recipient_keys=["3Dn1SJNPaCXcvvJvSbsFWP2xaCjMom3can8CQNhWrTRx"], + endpoint="http://our-service-endpoint.com", + routing_keys=[], + ), ) with mock.patch.object( @@ -303,7 +303,7 @@ async def test_find_oob_record_for_inbound_message_connectionless_retrieve_oob( self.context.message_receipt = MessageReceipt( thread_id="the-thid", recipient_verkey="our-recipient-key", - sender_verkey=self.oob_record.their_service["recipientKeys"][0], + sender_verkey=self.oob_record.their_service.recipient_keys[0], ) assert await self.oob_processor.find_oob_record_for_inbound_message( @@ -728,7 +728,7 @@ async def test_handle_message_connectionless(self): ) assert oob_record.attach_thread_id == "4a580490-a9d8-44f5-a3f6-14e0b8a219b0" - assert oob_record.their_service == { + assert oob_record.their_service.serialize() == { "serviceEndpoint": "http://their-service-endpoint.com", "recipientKeys": ["9WCgWKUaAJj3VWxxtzvvMQN3AoFxoBtBDo9ntwJnVVCC"], "routingKeys": ["6QSduYdf8Bi6t8PfNm5vNomGWDtXhmMmTRzaciudBXYJ"],