コード例 #1
0
async def test_valid_authentication_in_routing_handler(lazy_pipe):
    router = RequestRouter()

    async def authenticate(path: str, authentication: Authentication):
        if not isinstance(
                authentication,
                AuthenticationSimple) or authentication.password != b'pass':
            raise Exception('Invalid credentials')

    @router.response('test.path')
    async def response():
        return create_future(Payload(b'result'))

    def handler_factory(socket):
        return RoutingRequestHandler(socket,
                                     router,
                                     authentication_verifier=authenticate)

    async with lazy_pipe(client_arguments={
            'metadata_encoding':
            WellKnownMimeTypes.MESSAGE_RSOCKET_COMPOSITE_METADATA
    },
                         server_arguments={'handler_factory': handler_factory
                                           }) as (server, client):
        result = await RxRSocket(client).request_response(
            Payload(metadata=composite(route('test.path'),
                                       authenticate_simple('user', 'pass'))))

        assert result.data == b'result'
コード例 #2
0
async def request_response(client: RSocketClient):
    payload = Payload(
        b'The quick brown fox',
        composite(route('single_request'),
                  authenticate_simple('user', '12345')))

    await client.request_response(payload)
コード例 #3
0
async def test_invalid_authentication_in_routing_handler(lazy_pipe):
    router = RequestRouter()

    async def authenticate(path: str, authentication: Authentication):
        if not isinstance(
                authentication,
                AuthenticationSimple) or authentication.password != b'pass':
            raise Exception('Invalid credentials')

    @router.channel('test.path')
    async def request_channel():
        raise Exception('error from server')

    def handler_factory(socket):
        return RoutingRequestHandler(socket,
                                     router,
                                     authentication_verifier=authenticate)

    async with lazy_pipe(client_arguments={
            'metadata_encoding':
            WellKnownMimeTypes.MESSAGE_RSOCKET_COMPOSITE_METADATA
    },
                         server_arguments={'handler_factory': handler_factory
                                           }) as (server, client):
        with pytest.raises(Exception) as exc_info:
            await RxRSocket(client).request_channel(
                Payload(metadata=composite(
                    route('test.path'),
                    authenticate_simple('user', 'wrong_password'))))

        assert str(exc_info.value) == 'Invalid credentials'
コード例 #4
0
ファイル: client_rx.py プロジェクト: rsocket/rsocket-py
async def request_last_metadata(client: RxRSocket):
    payload = Payload(metadata=composite(route('last_metadata_push'),
                                         authenticate_simple('user', '12345')))

    result = await client.request_response(payload).pipe()

    assert result.data == b'audit info'
コード例 #5
0
ファイル: client_rx.py プロジェクト: rsocket/rsocket-py
async def metadata_push(client: RxRSocket, metadata: bytes):

    await client.metadata_push(
        composite(
            route('metadata_push'), authenticate_simple('user', '12345'),
            metadata_item(metadata,
                          WellKnownMimeTypes.TEXT_PLAIN.value.name))).pipe()
コード例 #6
0
ファイル: client_rx.py プロジェクト: rsocket/rsocket-py
async def request_fragmented_stream(client: RxRSocket):
    payload = Payload(
        b'The quick brown fox',
        composite(route('fragmented_stream'),
                  authenticate_simple('user', '12345')))
    result = await client.request_stream(payload).pipe(operators.to_list())
    print(result)
コード例 #7
0
async def test_authentication_success_on_setup(lazy_pipe):
    class Handler(BaseRequestHandler):
        def __init__(self, socket):
            super().__init__(socket)
            self._authenticated = False

        async def on_setup(self, data_encoding: bytes,
                           metadata_encoding: bytes, payload: Payload):
            composite_metadata = self._parse_composite_metadata(
                payload.metadata)
            authentication: AuthenticationSimple = composite_metadata.items[
                0].authentication
            if authentication.username != b'user' or authentication.password != b'12345':
                raise Exception('Authentication rejected')

            self._authenticated = True

        async def request_response(self,
                                   payload: Payload) -> Awaitable[Payload]:
            if not self._authenticated:
                raise RSocketApplicationError("Not authenticated")

            return create_future(Payload(b'response'))

    async with lazy_pipe(client_arguments={
            'setup_payload':
            Payload(metadata=composite(authenticate_simple('user', '12345')))
    },
                         server_arguments={'handler_factory':
                                           Handler}) as (server, client):
        result = await client.request_response(Payload(b'request'))

        assert result.data == b'response'
コード例 #8
0
async def request_slow_stream(client: RSocketClient):
    payload = Payload(
        b'The quick brown fox',
        composite(route('slow_stream'), authenticate_simple('user', '12345')))
    completion_event = Event()
    client.request_stream(payload).subscribe(
        StreamSubscriber(completion_event))
    await completion_event.wait()
コード例 #9
0
async def request_stream_invalid_login(client: RSocketClient):
    payload = Payload(
        b'The quick brown fox',
        composite(route('stream'), authenticate_simple('user',
                                                       'wrong_password')))
    completion_event = Event()
    client.request_stream(payload).initial_request_n(1).subscribe(
        StreamSubscriber(completion_event))
    await completion_event.wait()
コード例 #10
0
ファイル: client_rx.py プロジェクト: rsocket/rsocket-py
async def request_channel(client: RxRSocket):
    # channel_completion_event = Event()
    # requester_completion_event = Event()
    payload = Payload(
        b'The quick brown fox',
        composite(route('channel'), authenticate_simple('user', '12345')))
    # publisher = from_rsocket_publisher(sample_publisher(requester_completion_event))

    result = await client.request_channel(payload, 5).pipe(operators.to_list())
コード例 #11
0
ファイル: client_rx.py プロジェクト: rsocket/rsocket-py
async def request_stream_invalid_login(client: RxRSocket):
    payload = Payload(
        b'The quick brown fox',
        composite(route('stream'), authenticate_simple('user',
                                                       'wrong_password')))

    try:
        await client.request_stream(payload, request_limit=1).pipe()
    except RuntimeError as exception:
        assert str(exception) == 'Authentication error'
コード例 #12
0
async def test_authentication_failure_on_setup(lazy_pipe):
    received_error_event = Event()
    received_error: Optional[tuple] = None

    class ServerHandler(BaseRequestHandler):
        def __init__(self, socket):
            super().__init__(socket)
            self._authenticated = False

        async def on_setup(self, data_encoding: bytes,
                           metadata_encoding: bytes, payload: Payload):
            composite_metadata = self._parse_composite_metadata(
                payload.metadata)
            authentication: AuthenticationSimple = composite_metadata.items[
                0].authentication
            if authentication.username != b'user' or authentication.password != b'12345':
                raise Exception('Authentication error')

            self._authenticated = True

        async def request_response(self,
                                   payload: Payload) -> Awaitable[Payload]:
            if not self._authenticated:
                raise Exception("Not authenticated")

            future = asyncio.get_event_loop().create_future()
            future.set_result(Payload(b'response'))
            return future

    class ClientHandler(BaseRequestHandler):
        async def on_error(self, error_code: ErrorCode, payload: Payload):
            nonlocal received_error
            received_error = (error_code, payload)
            received_error_event.set()

    async with lazy_pipe(client_arguments={
            'handler_factory':
            ClientHandler,
            'setup_payload':
            Payload(metadata=composite(
                authenticate_simple('user', 'wrong_password')))
    },
                         server_arguments={'handler_factory':
                                           ServerHandler}) as (server, client):

        with pytest.raises(RuntimeError):
            await client.request_response(Payload(b'request'))

        await received_error_event.wait()

        assert received_error[0] == ErrorCode.REJECTED_SETUP
        assert received_error[1] == Payload(b'Authentication error', b'')
コード例 #13
0
async def main():
    connection = await asyncio.open_connection('localhost', 7000)

    setup_payload = Payload(data=str(uuid4()).encode(),
                            metadata=composite(
                                route('shell-client'),
                                authenticate_simple('user', 'pass')))

    async with RSocketClient(single_transport_provider(
            TransportTCP(*connection)),
                             setup_payload=setup_payload,
                             metadata_encoding=WellKnownMimeTypes.
                             MESSAGE_RSOCKET_COMPOSITE_METADATA):
        await asyncio.sleep(5)
コード例 #14
0
async def request_channel(client: RSocketClient):
    channel_completion_event = Event()
    requester_completion_event = Event()
    payload = Payload(
        b'The quick brown fox',
        composite(route('channel'), authenticate_simple('user', '12345')))
    publisher = sample_publisher(requester_completion_event)

    requested = client.request_channel(payload, publisher)

    requested.initial_request_n(5).subscribe(
        ChannelSubscriber(channel_completion_event))

    await channel_completion_event.wait()
    await requester_completion_event.wait()
コード例 #15
0
async def test_no_route_in_request(lazy_pipe):
    router = RequestRouter()

    def handler_factory(socket):
        return RoutingRequestHandler(socket, router)

    async with lazy_pipe(client_arguments={
            'metadata_encoding':
            WellKnownMimeTypes.MESSAGE_RSOCKET_COMPOSITE_METADATA
    },
                         server_arguments={'handler_factory': handler_factory
                                           }) as (server, client):
        with pytest.raises(Exception) as exc_info:
            await RxRSocket(client).request_channel(
                Payload(
                    metadata=composite(authenticate_simple('user', 'pass'))))

        assert str(exc_info.value) == 'No route found in request'
コード例 #16
0
ファイル: client_rx.py プロジェクト: rsocket/rsocket-py
async def fire_and_forget(client: RxRSocket, data: bytes):
    payload = Payload(
        data,
        composite(route('no_response'), authenticate_simple('user', '12345')))

    await client.fire_and_forget(payload).pipe()