def _inbound_connection_request_callback(self,
                                             request,
                                             result,
                                             connection=None):
        connection_response = ConnectionResponse()
        connection_response.ParseFromString(result.content)
        if connection_response.status == connection_response.ERROR:
            LOGGER.debug(
                "Received an error response to the NETWORK_CONNECT "
                "we sent. Removing connection: %s", connection.connection_id)
            self.remove_connection(connection.connection_id)

        # Send correct Authorization Request for network role
        auth_type = {"trust": [], "challenge": []}
        for role_entry in connection_response.roles:
            if role_entry.auth_type == connection_response.TRUST:
                auth_type["trust"].append(role_entry.role)
            elif role_entry.auth_type == connection_response.CHALLENGE:
                auth_type["challenge"].append(role_entry.role)

        if auth_type["trust"]:
            auth_trust_request = AuthorizationTrustRequest(
                roles=[RoleType.Value("NETWORK")], public_key=self._public_key)
            self.send(validator_pb2.Message.AUTHORIZATION_TRUST_REQUEST,
                      auth_trust_request.SerializeToString(), connection)

        if auth_type["challenge"]:
            auth_challenge_request = AuthorizationChallengeRequest()
            self.send(validator_pb2.Message.AUTHORIZATION_CHALLENGE_REQUEST,
                      auth_challenge_request.SerializeToString(),
                      connection,
                      callback=partial(
                          self._inbound_challenge_authorization_callback,
                          connection=connection))
Beispiel #2
0
    def test_authorization_challenge_request_bad_last_message(self):
        """
        Test the AuthorizationChallengeRequestHandler returns an
        AuthorizationViolation and closes the connection if the last message
        was not a ConnectionRequst
        """
        auth_challenge_request = AuthorizationChallengeRequest()
        roles = {"network": AuthorizationType.TRUST}

        network = MockNetwork(roles,
                              connection_status={"connection_id": "other"})
        handler = AuthorizationChallengeRequestHandler(network)
        handler_status = handler.handle(
            "connection_id", auth_challenge_request.SerializeToString())
        self.assertEqual(handler_status.status, HandlerStatus.RETURN_AND_CLOSE)
        self.assertEqual(handler_status.message_type,
                         validator_pb2.Message.AUTHORIZATION_VIOLATION)
def do_authorization_challenge_request():
    """
    Test the AuthorizationChallengeRequestHandler returns an
    AuthorizationChallengeResponse.
    """
    auth_challenge_request = AuthorizationChallengeRequest()
    roles = {"network": AuthorizationType.TRUST}

    network = MockNetwork(roles,
                          connection_status={
                              "connection_id":
                              ConnectionStatus.CONNECTION_REQUEST
                          })
    handler = AuthorizationChallengeRequestHandler(network)
    handler_status = handler.handle("connection_id",
                                    auth_challenge_request.SerializeToString())

    return handler_status
Beispiel #4
0
    def test_authorization_challenge_request(self):
        """
        Test the AuthorizationChallengeRequestHandler returns an
        AuthorizationChallengeResponse.
        """
        auth_challenge_request = AuthorizationChallengeRequest()
        roles = {"network": AuthorizationType.TRUST}

        network = MockNetwork(roles,
                              connection_status={
                                  "connection_id":
                                  ConnectionStatus.CONNECTION_REQUEST
                              })
        handler = AuthorizationChallengeRequestHandler(network)
        handler_status = handler.handle(
            "connection_id", auth_challenge_request.SerializeToString())
        self.assertEqual(handler_status.status, HandlerStatus.RETURN)
        self.assertEqual(
            handler_status.message_type,
            validator_pb2.Message.AUTHORIZATION_CHALLENGE_RESPONSE)