def test_to_data_to_sign(self):
        """Tests that the to_data_to_sign function correctly constructs a POST request for multiple headers"""
        authenticator = DefaultAuthenticator(
            AuthorizationType.get_authorization("v1HMAC"), "apiKeyId",
            "secretApiKey")
        http_headers = [
            RequestHeader(
                "X-GCS-ServerMetaInfo",
                "{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 (Oracle Corporation; "
                "Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\",\"sdkIdentifier\":\"1.0\"}"
            ),
            RequestHeader("Content-Type", "application/json"),
            RequestHeader("X-GCS-ClientMetaInfo", "{\"aap\",\"noot\"}"),
            RequestHeader("User-Agent", "Apache-HttpClient/4.3.4 (java 1.5)"),
            RequestHeader("Date", "Mon, 07 Jul 2014 12:12:40 GMT")
        ]
        expected_start = "POST\n" \
                         "application/json\n"
        expected_end = "x-gcs-clientmetainfo:{\"aap\",\"noot\"}\n" \
                       "x-gcs-servermetainfo:{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 " \
                       "(Oracle Corporation; Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\"," \
                       "\"sdkIdentifier\":\"1.0\"}\n" \
                       "/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen%20noot\n"

        url = urlparse.urlparse(
            "http://localhost:8080/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen%20noot"
        )
        data_to_sign = authenticator.to_data_to_sign("POST", url, http_headers)

        actual_start = data_to_sign[:22]
        actual_end = data_to_sign[52:]
        self.assertEqual(expected_start, actual_start)
        self.assertEqual(expected_end, actual_end)
    def test_canonicalized_header_value(self):
        """Tests that the to_canonicalize_header function correctly removes control characters and excessive whitespace
        """
        authenticator = DefaultAuthenticator(
            AuthorizationType.get_authorization("v1HMAC"), "apiKeyId",
            "secretApiKey")

        self.assertEqual(
            "aap noot",
            authenticator.to_canonicalize_header_value("aap\nnoot  "))
        self.assertEqual(
            "aap noot",
            authenticator.to_canonicalize_header_value(" aap\r\n  noot"))
    def test_create_authentication_signature_2(self):
        """Tests if the default authenticator creates the correct signature"""
        authenticator = DefaultAuthenticator(
            AuthorizationType.get_authorization("v1HMAC"), "apiKeyId",
            "6Kj5HT0MQKC6D8eb7W3lTg71kVKVDSt1")
        data_to_sign = "GET\n" \
                       "\n" \
                       "Fri, 06 Jun 2014 13:39:43 GMT\n" \
                       "/v1/9991/tokens/123456789\n"

        authentication_signature = authenticator.create_authentication_signature(
            data_to_sign)

        self.assertEqual("9ond5EIN05dBXJGCLRK5om9pxHsyrh/12pZJ7bvmwNM=",
                         authentication_signature)
Esempio n. 4
0
 def create_session_from_configuration(communicator_configuration,
                                       meta_data_provider=None,
                                       connection=None,
                                       authenticator=None):
     """
     Creates a Session based on the configuration stored in the
     CommunicatorConfiguration argument
     """
     # If an authenticator is not given,  api_key_id and secret_api_key are
     # used to create a DefaultAuthenticator used to create the session.
     if meta_data_provider is None:
         meta_data_provider = MetaDataProvider(
             integrator=communicator_configuration.integrator,
             shopping_cart_extension=communicator_configuration.
             shopping_cart_extension)
     if connection is None:
         connection = DefaultConnection(
             communicator_configuration.connect_timeout,
             communicator_configuration.socket_timeout,
             communicator_configuration.max_connections,
             communicator_configuration.proxy_configuration)
     if authenticator is None:
         authenticator = DefaultAuthenticator(
             communicator_configuration.authorization_type,
             communicator_configuration.api_key_id,
             communicator_configuration.secret_api_key)
     return Session(api_endpoint=communicator_configuration.api_endpoint,
                    meta_data_provider=meta_data_provider,
                    connection=connection,
                    authenticator=authenticator)
    def test_create_authentication_signature(self):
        """Tests if the default authenticator creates the correct signature"""
        authenticator = DefaultAuthenticator(
            AuthorizationType.get_authorization("v1HMAC"), "apiKeyId",
            "secretApiKey")
        data_to_sign = "DELETE\n" \
                       "application/json\n" \
                       "Fri, 06 Jun 2014 13:39:43 GMT\n" \
                       "x-gcs-clientmetainfo:processed header value\n" \
                       "x-gcs-customerheader:processed header value\n" \
                       "x-gcs-servermetainfo:processed header value\n" \
                       "/v1/9991/tokens/123456789\n"

        authentication_signature = authenticator.create_authentication_signature(
            data_to_sign)

        self.assertEqual("VfnXpPBQQoHZivTcAg0JvOWkhnzlPnaCPKpTQn/uMJM=",
                         authentication_signature)
def create_client(
        httphost,
        connect_timeout=0.500,
        socket_timeout=0.500,
        max_connections=EndpointConfiguration.DEFAULT_MAX_CONNECTIONS):
    connection = DefaultConnection(connect_timeout, socket_timeout,
                                   max_connections)
    authenticator = DefaultAuthenticator(AuthorizationType.V1HMAC, "apiKey",
                                         "secret")
    meta_data_provider = MetaDataProvider("Ingenico")
    session = Session(httphost, connection, authenticator, meta_data_provider)
    communicator = Factory.create_communicator_from_session(session)
    return Factory.create_client_from_communicator(communicator)
Esempio n. 7
0
def create_session():
    host = os.getenv("connect.api.endpoint.host")
    scheme = os.getenv("connect.api.endpoint.scheme", "https")
    port = int(os.getenv("connect.api.endpoint.port", -1))
    if not host:
        raise RuntimeError(
            "unable to read environment variables to find api_endpoint")
    api_endpoint = "{2}://{0}:{1}".format(host, port, scheme)
    authenticator = DefaultAuthenticator(
        api_id_key=API_KEY_ID,
        secret_api_key=SECRET_API_KEY,
        authorization_type=AuthorizationType.V1HMAC)
    return Session(api_endpoint=api_endpoint,
                   authenticator=authenticator,
                   connection=DefaultConnection(3, 3),
                   meta_data_provider=MetaDataProvider("Ingenico"))
Esempio n. 8
0
 def setUp(self):
     self.mock_connection = MagicMock(spec=Connection(), autospec=True)
     self.session = Session(
         "http://localhost", self.mock_connection,
         DefaultAuthenticator(AuthorizationType.V1HMAC, "admin", "admin"),
         MetaDataProvider("Ingenico"))