Esempio n. 1
0
 def setUp(self):
     self.connection_invitation = ConnectionInvitation(
         label=self.label, recipient_keys=[self.key], endpoint=self.endpoint_url
     )
     self.invitation = ForwardInvitation(
         invitation=self.connection_invitation, message=self.test_message
     )
    async def test_handle_auto_accept(self):
        handler = test_module.InvitationRequestHandler()
        self.context.update_settings({"accept_requests": True})

        conn_invitation = ConnectionInvitation(
            label=TEST_LABEL,
            did=TEST_DID,
            recipient_keys=[TEST_VERKEY],
            endpoint=TEST_ENDPOINT,
            routing_keys=[TEST_ROUTE_VERKEY],
            image_url=TEST_IMAGE_URL,
        )
        mock_conn_rec = async_mock.MagicMock(connection_id="dummy")

        responder = MockResponder()
        with async_mock.patch.object(test_module,
                                     "ConnectionManager",
                                     autospec=True) as mock_mgr:
            mock_mgr.return_value.create_invitation = async_mock.CoroutineMock(
                return_value=(mock_conn_rec, conn_invitation))

            await handler.handle(self.context, responder)
            assert mock_mgr.return_value.create_invitation.called_once_with()

            messages = responder.messages
            assert len(messages) == 1
            (result, _) = messages[0]
            assert type(result) == Invitation
            assert result._thread._thid == self.context.message._message_id
Esempio n. 3
0
    async def test_service_return_invitation_not_found(self):
        invite = demo_service.Invitation(
            invitation=ConnectionInvitation(
                label=TEST_LABEL,
                did=TEST_DID,
                recipient_keys=[TEST_VERKEY],
                endpoint=TEST_ENDPOINT,
                routing_keys=[TEST_ROUTE_VERKEY],
                image_url=TEST_IMAGE_URL,
            ),
            message="Hello World",
        )

        service = await demo_service.DemoIntroductionService.service_handler()(
            self.context)

        conn_rec_target = ConnectionRecord(
            connection_id=None,
            state=ConnectionRecord.STATE_ACTIVE,
        )
        await conn_rec_target.save(self.context)
        assert conn_rec_target._id

        await service.return_invitation(
            target_connection_id=conn_rec_target._id,
            invitation=invite,
            outbound_handler=None,
        )
 async def handle(self, context: RequestContext, responder: BaseResponder):
     """Handle recieve invitation request."""
     connection_mgr = ConnectionManager(context)
     invitation = ConnectionInvitation.from_url(context.message.invitation)
     connection = await connection_mgr.receive_invitation(
         invitation, auto_accept=context.message.auto_accept)
     connection_resp = Connection(**conn_record_to_message_repr(connection))
     await responder.send_reply(connection_resp)
Esempio n. 5
0
    async def test_service_start_and_return_introduction(self):
        service = await demo_service.DemoIntroductionService.service_handler()(
            self.context
        )
        start_responder = MockResponder()

        conn_rec_init = ConnectionRecord(
            connection_id=None,
            state=ConnectionRecord.STATE_ACTIVE,
        )
        await conn_rec_init.save(self.context)
        assert conn_rec_init._id

        conn_rec_target = ConnectionRecord(
            connection_id=None,
            state=ConnectionRecord.STATE_ACTIVE,
        )
        await conn_rec_target.save(self.context)
        assert conn_rec_target._id

        await service.start_introduction(
            init_connection_id=conn_rec_init._id,
            target_connection_id=conn_rec_target._id,
            message="Hello Start",
            outbound_handler=start_responder.send,
        )
        messages = start_responder.messages
        assert len(messages) == 1
        (result, target) = messages[0]
        assert isinstance(result, demo_service.InvitationRequest)
        assert result.message == "Hello Start"
        assert target["connection_id"] == conn_rec_target._id

        invite = demo_service.Invitation(
            invitation=ConnectionInvitation(
                label=TEST_LABEL,
                did=TEST_DID,
                recipient_keys=[TEST_VERKEY],
                endpoint=TEST_ENDPOINT,
                routing_keys=[TEST_ROUTE_VERKEY],
                image_url=TEST_IMAGE_URL,
            ),
            message="Hello Invite",
            _id=result._id,
        )
        return_responder = MockResponder()

        await service.return_invitation(
            target_connection_id=conn_rec_target._id,
            invitation=invite,
            outbound_handler=return_responder.send,
        )
        messages = return_responder.messages
        assert len(messages) == 1
        (result, target) = messages[0]
        assert isinstance(result, demo_service.ForwardInvitation)
        assert result.message == "Hello Invite"
        assert target["connection_id"] == conn_rec_init._id
Esempio n. 6
0
 async def test_make_model(self):
     invitation = ForwardInvitation(
         invitation=ConnectionInvitation(
             label=self.label, recipient_keys=[self.key], endpoint=self.endpoint_url
         ),
         message=self.test_message,
     )
     data = invitation.serialize()
     model_instance = ForwardInvitation.deserialize(data)
     assert type(model_instance) is type(invitation)
    async def setUp(self):
        self.context = RequestContext(base_context=InjectionContext(
            enforce_typing=False))

        self.context.connection_ready = True
        self.context.message = ForwardInvitation(
            invitation=ConnectionInvitation(
                label=TEST_LABEL,
                did=TEST_DID,
                recipient_keys=[TEST_VERKEY],
                endpoint=TEST_ENDPOINT,
                routing_keys=[TEST_ROUTE_VERKEY],
                image_url=TEST_IMAGE_URL,
            ),
            message="Hello World",
        )
        self.context.update_settings({"accept_invites": False})
    async def setUp(self):
        self.storage = BasicStorage()

        self.context = RequestContext(base_context=InjectionContext(
            enforce_typing=False))
        self.context.injector.bind_instance(BaseStorage, self.storage)

        self.context.connection_ready = True
        self.context.message = Invitation(
            invitation=ConnectionInvitation(
                label=TEST_LABEL,
                did=TEST_DID,
                recipient_keys=[TEST_VERKEY],
                endpoint=TEST_ENDPOINT,
                routing_keys=[TEST_ROUTE_VERKEY],
                image_url=TEST_IMAGE_URL,
            ),
            message="Hello World",
        )
        self.context.connection_record = async_mock.MagicMock(
            connection_id="dummy")
    async def receive_invitation(
            self, invitation: InvitationMessage) -> ConnectionRecord:
        """Receive an out of band invitation message."""

        ledger: BaseLedger = await self.context.inject(BaseLedger)

        # New message format
        invitation_message = InvitationMessage.deserialize(invitation)

        # Convert to old format and pass to relevant manager
        # The following logic adheres to Aries RFC 0496

        # There must be exactly 1 service entry
        if (len(invitation_message.service_blocks) +
                len(invitation_message.service_dids) != 1):
            raise OutOfBandManagerError(
                "service array must have exactly one element")

        # Get the single service item
        if invitation_message.service_blocks:
            service = invitation_message.service_blocks[0]
        else:
            # If it's in the did format, we need to convert to a full service block
            service_did = invitation_message.service_dids[0]
            async with ledger:
                verkey = await ledger.get_key_for_did(service_did)
                endpoint = await ledger.get_endpoint_for_did(service_did)
            service = ServiceMessage.deserialize({
                "id": "#inline",
                "type": "did-communication",
                "recipientKeys": [verkey],
                "routingKeys": [],
                "serviceEndpoint": endpoint,
            })

        # If we are dealing with an invitation
        if (len(invitation_message.handshake_protocols) == 1
                and invitation_message.handshake_protocols[0] ==
                "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation"
            ):

            if len(invitation_message.request_attach) != 0:
                raise OutOfBandManagerError(
                    "request block must be empty for invitation message type.")

            # Convert to the old message format
            connection_invitation = ConnectionInvitation.deserialize({
                "@id":
                invitation_message._id,
                "@type":
                invitation_message.handshake_protocols[0],
                "label":
                invitation_message.label,
                "recipientKeys":
                service.recipient_keys,
                "serviceEndpoint":
                service.service_endpoint,
                "routingKeys":
                service.routing_keys,
            })

            connection_mgr = ConnectionManager(self.context)
            connection = await connection_mgr.receive_invitation(
                connection_invitation, auto_accept=True)

        elif (len(invitation_message.request_attach) == 1
              and invitation_message.request_attach[0].data.json["@type"]
              == "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec" +
              "/present-proof/1.0/request-presentation"):
            raise OutOfBandManagerNotImplementedError(
                "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec" +
                "/present-proof/1.0/request-presentation " +
                "request type not implemented.")
        else:
            raise OutOfBandManagerError("Invalid request type")

        return connection