Esempio n. 1
0
 def test_fail_to_retrieve_public_key_v2(self):
     KeyHolder.get_public_key = MagicMock(return_value="")
     self.authenticator.signed = Signed.from_headers(self.v2_headers)
     with self.assertRaises(UnableToAuthenticateError) as exc:
         self.authenticator._authenticate()
     self.assertEqual(str(exc.exception),
                      "Unable to identify Public Key type from Signature.")
    def test_protocol_test_suite(self):
        for case_path in TEST_SUITE.cases():
            parser = ProtocolTestSuiteParser(case_path)
            request_signable = RequestSignable(**parser.request_attributes)
            signed_headers_v2 = TEST_SUITE.signer.signed_headers_v2(
                request_signable, TEST_SUITE.additional_attributes)
            if "authentication-only" not in case_path:
                with self.subTest(test="string_to_sign_v2",
                                  case_name=parser.case_name):
                    string_to_sign = request_signable.string_to_sign_v2(
                        TEST_SUITE.additional_attributes)
                    self.assertEqual(string_to_sign.decode("utf-8"),
                                     parser.sts)

                with self.subTest(test="signature",
                                  case_name=parser.case_name):
                    self.assertEqual(
                        TEST_SUITE.signer.signature_v2(parser.sts), parser.sig)

                with self.subTest(test="authentication headers",
                                  case_name=parser.case_name):
                    self.assertEqual(signed_headers_v2, parser.auth_headers)

            with self.subTest(test="authentication",
                              case_name=parser.case_name):
                signed = Signed.from_headers(signed_headers_v2)
                authenticator = LocalAuthenticator(request_signable, signed,
                                                   self.logger)
                self.assertTrue(authenticator._authenticate())
Esempio n. 3
0
    def test_from_headers_v2(self):
        signed = Signed.from_headers(MWSV2_HEADERS)

        self.assertEqual(signed.protocol_version(), 2)
        self.assertEqual(signed.mcc_time, EPOCH)
        self.assertEqual(signed.token, "MWSV2")
        self.assertEqual(signed.app_uuid, APP_UUID)
        self.assertEqual(signed.signature, MWSV2_SIGNATURE)
Esempio n. 4
0
    def test_from_headers_v1(self):
        signed = Signed.from_headers(X_MWS_HEADERS)

        self.assertEqual(signed.protocol_version(), 1)
        self.assertEqual(signed.x_mws_time, EPOCH)
        self.assertEqual(signed.token, "MWS")
        self.assertEqual(signed.app_uuid, APP_UUID)
        self.assertEqual(signed.signature, X_MWS_SIGNATURE)
Esempio n. 5
0
    def test_from_headers_missing_header(self):
        signed = Signed.from_headers({})

        self.assertEqual(signed.protocol_version(), None)
        self.assertEqual(signed.mcc_time, "")
        self.assertEqual(signed.token, "")
        self.assertEqual(signed.app_uuid, "")
        self.assertEqual(signed.signature, "")
Esempio n. 6
0
 def test_authentication_v2_does_not_authenticate_500(self):
     self.authenticator.signed = Signed.from_headers(self.v2_headers)
     with requests_mock.mock() as requests:
         requests.post(MAUTH_AUTHENTICATION_URL, status_code=500)
         with self.assertRaises(UnableToAuthenticateError) as exc:
             self.authenticator._authenticate()
         self.assertEqual(str(exc.exception),
                          "The mAuth service responded with 500: ")
Esempio n. 7
0
 def test_authentication_v2_does_not_authenticate_a_false_message(self):
     self.authenticator.signed = Signed.from_headers(self.v2_headers)
     self.authenticator.signable = RequestSignable(method="GET",
                                                   url=URL,
                                                   body=BODY)
     with self.assertRaises(InauthenticError) as exc:
         self.authenticator._authenticate()
     self.assertEqual(str(exc.exception),
                      "Signature verification failed for request.")
Esempio n. 8
0
    def authenticate(self, signed_request):
        """
        Authenticates a request

        :param signed_request: Request object
        :type request: werkzeug.wrappers.BaseRequest
        :return: Is the request authentic, Status Code, Message
        :rtype: bool, int, str
        """
        signable = RequestSignable(method=signed_request.method, url=signed_request.path, body=signed_request.data)
        return self._authenticator(signable, Signed.from_headers(signed_request.headers), logger).is_authentic()
Esempio n. 9
0
    def test_from_headers_bad_header(self):
        bad_header = {
            "MCC-Time": EPOCH,
            "MCC-Authentication": X_MWS_AUTHENTICATION
        }
        signed = Signed.from_headers(bad_header)

        self.assertEqual(signed.protocol_version(), 2)
        self.assertEqual(signed.mcc_time, EPOCH)
        self.assertEqual(signed.token, "")
        self.assertEqual(signed.app_uuid, "")
        self.assertEqual(signed.signature, "")
Esempio n. 10
0
    def setUp(self):
        self.__get_public_key__ = KeyHolder.get_public_key
        KeyHolder.get_public_key = MagicMock(return_value=load_key("rsapub"))

        Config.V2_ONLY_AUTHENTICATE = False
        self.logger = logging.getLogger()

        self.v1_headers = copy.deepcopy(X_MWS_HEADERS)
        self.v2_headers = copy.deepcopy(MWSV2_HEADERS)
        self.signable = RequestSignable(method="POST", url=URL, body=BODY)

        self.authenticator = LocalAuthenticator(
            self.signable, Signed.from_headers(self.v1_headers), self.logger)
Esempio n. 11
0
    def setUp(self):
        Config.V2_ONLY_AUTHENTICATE = False
        RemoteAuthenticator._MAUTH = {
            "auth": MagicMock(),
            "url": MAUTH_AUTHENTICATION_URL
        }

        self.logger = logging.getLogger()

        self.v1_headers = copy.deepcopy(X_MWS_HEADERS)
        self.v2_headers = copy.deepcopy(MWSV2_HEADERS)
        self.signable = RequestSignable(method="POST", url=URL, body=BODY)
        self.signed = Signed.from_headers(self.v1_headers)

        self.authenticator = RemoteAuthenticator(self.signable, self.signed,
                                                 self.logger)
Esempio n. 12
0
    def test_authentication_v2_happy_path(self):
        expected_ticket_v2 = {
            "authentication_ticket": {
                "verb": "POST",
                "app_uuid": APP_UUID,
                "client_signature": MWSV2_SIGNATURE,
                "request_url": "/sandbox/path",
                "request_time": EPOCH,
                "b64encoded_body": "44GT44KT44Gr44Gh44Gvw4Y=",
                "query_string": "",
                "token": "MWSV2",
            }
        }
        self.authenticator.signed = Signed.from_headers(self.v2_headers)

        with requests_mock.mock() as requests:
            requests.post(MAUTH_AUTHENTICATION_URL, status_code=200)
            result = self.authenticator._authenticate()

        self.assertTrue(result)
        self.assertEqual(requests.last_request.json(), expected_ticket_v2)
Esempio n. 13
0
 def test_authentication_v2_happy_path(self):
     self.authenticator.signed = Signed.from_headers(self.v2_headers)
     self.assertTrue(self.authenticator._authenticate())
Esempio n. 14
0
 def __init__(self, headers, v2_only_authenticate=False, method="POST"):
     Config.V2_ONLY_AUTHENTICATE = v2_only_authenticate
     signable = RequestSignable(method=method, url=URL, body=BODY)
     super().__init__(signable, Signed.from_headers(headers),
                      logging.getLogger())
Esempio n. 15
0
 def test_authentication_v2_happy_path_multiple_versions(self):
     self.v2_headers[
         "MCC-Authentication"] = "RWS {app_uuid}:ABC;{mwsv2_authentication};MWSV3 {app_uuid}:DEF".format(
             app_uuid=APP_UUID, mwsv2_authentication=MWSV2_AUTHENTICATION)
     self.authenticator.signed = Signed.from_headers(self.v2_headers)
     self.assertTrue(self.authenticator._authenticate())
Esempio n. 16
0
 def test_authentication_v2_happy_path_pub_key(self):
     self.authenticator.signed = Signed.from_headers(self.v2_headers)
     KeyHolder.get_public_key = MagicMock(return_value=load_key("pub"))
     self.assertTrue(self.authenticator._authenticate())
 def __init__(self, method, url, headers, body):
     logger = logging.getLogger()
     signable = RequestSignable(method=method, url=url, body=body)
     authenticator = LocalAuthenticator if Config.MAUTH_MODE == "local" else RemoteAuthenticator
     self._authenticator = authenticator(signable, Signed.from_headers(headers), logger)