def test_encode_body_safe_unicode_to_other_bytes(self):
     self.req.body = 'hello'
     self.req.headers['content-type'] = 'text/plain; charset=ascii'
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\x68\x65\x6c\x6c\x6f')
     expected = 'text/plain; charset=ascii'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_safe_unicode_to_other_bytes(self):
     self.req.body = 'hello'
     self.req.headers['content-type'] = 'text/plain; charset=ascii'
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\x68\x65\x6c\x6c\x6f')
     expected = 'text/plain; charset=ascii'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_unsafe_unicode_to_other_bytes(self):
     self.req.body = '€'
     self.req.headers['content-type'] = 'text/plain; charset=cp1252'
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\x80')
     expected = 'text/plain; charset=cp1252'
     self.assertEqual(self.req.headers['content-type'], expected)
    def test_generate_signature(self):
        """
        Using example data from
        http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html

        """
        access_key = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
        region = 'us-east-1'
        service = 'iam'
        date = '20110909'
        key = AWS4SigningKey(access_key, region, service, date)
        req_text = [
            'POST https://iam.amazonaws.com/ HTTP/1.1',
            'Host: iam.amazonaws.com',
            'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
            'X-Amz-Date: 20110909T233600Z', '',
            'Action=ListUsers&Version=2010-05-08'
        ]
        req_text = '\n'.join(req_text) + '\n'
        req = request_from_text(req_text)
        del req.headers['content-length']
        include_hdrs = list(req.headers)
        auth = AWS4Auth('dummy', key, include_hdrs=include_hdrs)
        AWS4Auth.encode_body(req)
        hsh = hashlib.sha256(req.body)
        req.headers['x-amz-content-sha256'] = hsh.hexdigest()
        sreq = auth(req)
        signature = sreq.headers['Authorization'].split('=')[3]
        expected = ('ced6826de92d2bdeed8f846f0bf508e8559e98e4b0199114b84c541'
                    '74deb456c')
        self.assertEqual(signature, expected)
    def test_amz1(self):
        """
        Using example data selected from:
        http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

        """
        req_text = [
            'POST https://iam.amazonaws.com/ HTTP/1.1',
            'Host: iam.amazonaws.com', 'Content-Length: 54',
            'Content-Type: application/x-www-form-urlencoded',
            'X-Amz-Date: 20110909T233600Z', '',
            'Action=ListUsers&Version=2010-05-08'
        ]
        req = request_from_text('\n'.join(req_text))
        AWS4Auth.encode_body(req)
        hsh = hashlib.sha256(req.body)
        req.headers['x-amz-content-sha256'] = hsh.hexdigest()
        include_hdrs = ['host', 'content-type', 'x-amz-date']
        result = AWS4Auth.get_canonical_headers(req, include=include_hdrs)
        cano_headers, signed_headers = result
        expected = [
            'POST', '/', '', 'content-type:application/x-www-form-urlencoded',
            'host:iam.amazonaws.com', 'x-amz-date:20110909T233600Z', '',
            'content-type;host;x-amz-date',
            'b6359072c78d70ebee1e81adcbab4f01bf2c23245fa365ef83fe8f1f95'
            '5085e2'
        ]
        expected = '\n'.join(expected)
        cano_req = AWS4Auth.get_canonical_request(req, cano_headers,
                                                  signed_headers)
        self.assertEqual(cano_req, expected)
 def test_encode_body_unsafe_unicode_to_other_bytes(self):
     self.req.body = '€'
     self.req.headers['content-type'] = 'text/plain; charset=cp1252'
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\x80')
     expected = 'text/plain; charset=cp1252'
     self.assertEqual(self.req.headers['content-type'], expected)
    def test_generate_signature(self):
        """
        Using example data from
        http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html

        """
        access_key = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY'
        region = 'us-east-1'
        service = 'iam'
        date = '20110909'
        key = AWS4SigningKey(access_key, region, service, date)
        req_text = [
            'POST https://iam.amazonaws.com/ HTTP/1.1',
            'Host: iam.amazonaws.com',
            'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
            'X-Amz-Date: 20110909T233600Z',
            '',
            'Action=ListUsers&Version=2010-05-08']
        req_text = '\n'.join(req_text) + '\n'
        req = request_from_text(req_text)
        del req.headers['content-length']
        include_hdrs = list(req.headers)
        auth = AWS4Auth('dummy', key, include_hdrs=include_hdrs)
        AWS4Auth.encode_body(req)
        hsh = hashlib.sha256(req.body)
        req.headers['x-amz-content-sha256'] = hsh.hexdigest()
        sreq = auth(req)
        signature = sreq.headers['Authorization'].split('=')[3]
        expected = ('ced6826de92d2bdeed8f846f0bf508e8559e98e4b0199114b84c541'
                    '74deb456c')
        self.assertEqual(signature, expected)
 def _test_amz_test_suite_item(self, group_name, group):
     req = request_from_text(group['.req'])
     if 'content-length' in req.headers:
         del req.headers['content-length']
     include_hdrs = list(req.headers)
     AWS4Auth.encode_body(req)
     hsh = hashlib.sha256(req.body or b'')
     req.headers['x-amz-content-sha256'] = hsh.hexdigest()
     result = AWS4Auth.get_canonical_headers(req, include_hdrs)
     cano_headers, signed_headers = result
     cano_req = AWS4Auth.get_canonical_request(req, cano_headers,
                                               signed_headers)
     msg = 'Group: ' + group_name
     self.assertEqual(cano_req, group['.creq'], msg=msg)
 def _test_amz_test_suite_item(self, group_name, group):
     req = request_from_text(group['.req'])
     if 'content-length' in req.headers:
         del req.headers['content-length']
     include_hdrs = list(req.headers)
     AWS4Auth.encode_body(req)
     hsh = hashlib.sha256(req.body or b'')
     req.headers['x-amz-content-sha256'] = hsh.hexdigest()
     result = AWS4Auth.get_canonical_headers(req, include_hdrs)
     cano_headers, signed_headers = result
     cano_req = AWS4Auth.get_canonical_request(req, cano_headers,
                                               signed_headers)
     msg = 'Group: ' + group_name
     self.assertEqual(cano_req, group['.creq'], msg=msg)
 def _test_amz_test_suite_item(self, group_name):
     group = amz_aws4_testsuite.data[group_name]
     req = request_from_text(group['.req'])
     if 'content-length' in req.headers:
         del req.headers['content-length']
     include_hdrs = list(req.headers)
     AWS4Auth.encode_body(req)
     hsh = hashlib.sha256(req.body or b'')
     req.headers['x-amz-content-sha256'] = hsh.hexdigest()
     req.headers['x-amz-date'] = amz_aws4_testsuite.timestamp
     key = AWS4SigningKey(amz_aws4_testsuite.access_key,
                          amz_aws4_testsuite.region,
                          amz_aws4_testsuite.service,
                          amz_aws4_testsuite.date)
     auth = AWS4Auth(amz_aws4_testsuite.access_id, key,
                     include_hdrs=include_hdrs)
     sreq = auth(req)
     auth_hdr = sreq.headers['Authorization']
     msg = 'Group: ' + group_name
     self.assertEqual(auth_hdr, group['.authz'], msg=msg)
 def _test_amz_test_suite_item(self, group_name):
     group = amz_aws4_testsuite.data[group_name]
     req = request_from_text(group['.req'])
     if 'content-length' in req.headers:
         del req.headers['content-length']
     include_hdrs = list(req.headers)
     AWS4Auth.encode_body(req)
     hsh = hashlib.sha256(req.body or b'')
     req.headers['x-amz-content-sha256'] = hsh.hexdigest()
     req.headers['x-amz-date'] = amz_aws4_testsuite.timestamp
     key = AWS4SigningKey(amz_aws4_testsuite.access_key,
                          amz_aws4_testsuite.region,
                          amz_aws4_testsuite.service,
                          amz_aws4_testsuite.date)
     auth = AWS4Auth(amz_aws4_testsuite.access_id,
                     key,
                     include_hdrs=include_hdrs)
     sreq = auth(req)
     auth_hdr = sreq.headers['Authorization']
     msg = 'Group: ' + group_name
     self.assertEqual(auth_hdr, group['.authz'], msg=msg)
    def test_amz1(self):
        """
        Using example data selected from:
        http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html

        """
        req_text = [
            'POST https://iam.amazonaws.com/ HTTP/1.1',
            'Host: iam.amazonaws.com',
            'Content-Length: 54',
            'Content-Type: application/x-www-form-urlencoded',
            'X-Amz-Date: 20110909T233600Z',
            '',
            'Action=ListUsers&Version=2010-05-08']
        req = request_from_text('\n'.join(req_text))
        AWS4Auth.encode_body(req)
        hsh = hashlib.sha256(req.body)
        req.headers['x-amz-content-sha256'] = hsh.hexdigest()
        include_hdrs = ['host', 'content-type', 'x-amz-date']
        result = AWS4Auth.get_canonical_headers(req, include=include_hdrs)
        cano_headers, signed_headers = result
        expected = [
            'POST',
            '/',
            '',
            'content-type:application/x-www-form-urlencoded',
            'host:iam.amazonaws.com',
            'x-amz-date:20110909T233600Z',
            '',
            'content-type;host;x-amz-date',
            'b6359072c78d70ebee1e81adcbab4f01bf2c23245fa365ef83fe8f1f95'
            '5085e2']
        expected = '\n'.join(expected)
        auth = AWS4Auth('dummy', 'dummy', 'dummy', 'host')
        cano_req = auth.get_canonical_request(req, cano_headers,
                                                  signed_headers)
        self.assertEqual(cano_req, expected)
 def test_encode_body_unsafe_unicode_to_utf8(self):
     self.req.body = '☃'
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\xe2\x98\x83')
     expected = 'text/plain; charset=utf-8'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_utf8_string_to_bytes(self):
     self.req.body = u('☃')
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\xe2\x98\x83')
     expected = 'text/plain; charset=utf-8'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_unicode_to_bytes(self):
     self.req.body = u('hello')
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\x68\x65\x6c\x6c\x6f')
     expected = 'text/plain; charset=utf-8'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_unsafe_unicode_to_utf8(self):
     self.req.body = '☃'
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\xe2\x98\x83')
     expected = 'text/plain; charset=utf-8'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_bytes(self):
     text = b'hello'
     self.req.body = text
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, text)
     self.assertEqual(self.req.headers, {})
 def test_encode_body_utf8_string_to_bytes(self):
     self.req.body = u('☃')
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\xe2\x98\x83')
     expected = 'text/plain; charset=utf-8'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_unicode_to_bytes(self):
     self.req.body = u('hello')
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, b'\x68\x65\x6c\x6c\x6f')
     expected = 'text/plain; charset=utf-8'
     self.assertEqual(self.req.headers['content-type'], expected)
 def test_encode_body_bytes(self):
     text = b'hello'
     self.req.body = text
     AWS4Auth.encode_body(self.req)
     self.assertEqual(self.req.body, text)
     self.assertEqual(self.req.headers, {})