def get_base_string(url, method, query_params, oauth_parameters): # Merge the query string parameters merge_params = oauth_parameters.copy() # merge_params.update(query_params) return "{}&{}&{}".format( util.uri_rfc3986_encode(method.upper()), util.uri_rfc3986_encode(util.normalize_url(url)), util.uri_rfc3986_encode(util.normalize_params(url, merge_params)))
def get_authorization_header(self, uri, method, payload, consumer_key, signing_key): oauth_parameters = OAuth.get_oauth_parameters(self, uri, method, payload, consumer_key, signing_key) # Get the updated base parameteres dict oauth_base_parameters_dict = oauth_parameters.get_base_parameters_dict() # Generate the header value for OAuth Header oauth_key = OAuthParameters.OAUTH_KEY+" "+ \ ",".join([util.uri_rfc3986_encode(str(key)) + "=\"" + util.uri_rfc3986_encode(str(value)) + "\"" for (key, value) in oauth_base_parameters_dict.items()]) return oauth_key
def get_encoded_body_hash(self, payload): payload_str = json.dumps(payload) if type(payload) is dict else payload if not payload_str: # If the request does not have an entity body, the hash should be taken over the empty string payload_str = OAuth.EMPTY_STRING encoded_hash = util.base64_encode(util.sha256_encode(payload_str)) return util.uri_rfc3986_encode(encoded_hash)
def test_sign_message(self): baseString = 'POST&https%3A%2F%2Fsandbox.api.mastercard.com%2Ffraud%2Fmerchant%2Fv1%2Ftermination-inquiry&Format%3DXML%26PageLength%3D10%26PageOffset%3D0%26oauth_body_hash%3DWhqqH%252BTU95VgZMItpdq78BWb4cE%253D%26oauth_consumer_key%3Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%26oauth_nonce%3D1111111111111111111%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D1111111111%26oauth_version%3D1.0' signature = OAuth().sign_message(baseString, OAuthTest.signing_key) signature = Util.uri_rfc3986_encode(signature) self.assertEqual( signature, "DvyS3R795sUb%2FcvBfiFYZzPDU%2BRVefW6X%2BAfyu%2B9fxjudQft%2BShXhpounzJxYCwOkkjZWXOR0ICTMn6MOuG04TTtmPMrOxj5feGwD3leMBsi%2B3XxcFLPi8BhZKqgapcAqlGfjEhq0COZ%2FF9aYDcjswLu0zgrTMSTp4cqXYMr9mbQVB4HL%2FjiHni5ejQu9f6JB9wWW%2BLXYhe8F6b4niETtzIe5o77%2B%2BkKK67v9wFIZ9pgREz7ug8K5DlxX0DuwdUKFhsenA5z%2FNNCZrJE%2BtLu0tSjuF5Gsjw5GRrvW33MSoZ0AYfeleh5V3nLGgHrhVjl5%2BiS40pnG2po%2F5hIAUT5ag%3D%3D" )
def test_sign_message(self): baseString = 'POST&https%3A%2F%2Fsandbox.api.mastercard.com%2Ffraud%2Fmerchant%2Fv1%2Ftermination-inquiry&Format%3DXML%26PageLength%3D10%26PageOffset%3D0%26oauth_body_hash%3DWhqqH%252BTU95VgZMItpdq78BWb4cE%253D%26oauth_consumer_key%3Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%26oauth_nonce%3D1111111111111111111%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D1111111111%26oauth_version%3D1.0' signature = OAuth().sign_message(baseString, self.signing_key) signature = Util.uri_rfc3986_encode(signature) self.assertEqual( signature, "nqxpgHpye%2BdOEkEbC%2FS3N1%2FCRFlZPHoyRztkRhkCoz7ISNmV9V60TQ7zwS8Q59SGQUGYuoNSVe8SWtNVQTEuRiZfXd6Eme%2BCdHfAt7%2BbNd3UsrcIHl3CJEvx7u70ItW8aOx4F7rjF%2BaIOq%2Bpc0rbuBugF%2BnElGKydpPiQrKKwE5kB3TZKVkYLLCsLU8Ry%2Fjg05d2TcnGTyfYZDchV4ui0uPzR5UH%2Fkb4ni8lchrtAeaGJwCimACIk6qNLoNnz7u9joKHtYeuZhORRVodxKB%2BAolgAQqJBMyLrseJDITmwIRTRSzQ3vclt%2BMvVs1CMbXYuvnDYd5NFv98emJgBC%2FX1A%3D%3D" )
def test_oauth_body_hash_with_body_bytes(self): oauth_object = OAuth1RSA(OAuthExtTest.consumer_key, OAuthExtTest.signing_key) OAuthExtTest.mock_prepared_request.body = b'{"A" : OAuthExtTest.data}' # Passing mock data to the actual func to get the value oauth_body_hash_object = oauth_object.oauth_body_hash( OAuthExtTest.mock_prepared_request, OAuthExtTest.payload) # Using mock data to find the hash value hashlib_val = hashlib.sha256( OAuthExtTest.mock_prepared_request.body).digest() payload_hash_value = util.uri_rfc3986_encode( util.base64_encode(hashlib_val)) self.assertEqual(oauth_body_hash_object['oauth_body_hash'], payload_hash_value)
def test_oauth_body_hash_with_body_multipart(self): oauth_object = OAuth1RSA(OAuthExtTest.consumer_key, OAuthExtTest.signing_key) mock_request = PreparedRequest() mock_request.prepare(headers={'Content-type': 'multipart/form-data'}, method="GET", url="http://www.mastercard.com") # Passing mock data to the actual func to get the value oauth_body_hash_object = oauth_object.oauth_body_hash( mock_request, OAuthExtTest.payload) # Using mock data to find the hash value hashlib_val = hashlib.sha256( str(OAuthExtTest.mock_prepared_request.body).encode( 'utf8')).digest() payload_hash_value = util.uri_rfc3986_encode( util.base64_encode(hashlib_val)) self.assertEqual(oauth_body_hash_object['oauth_body_hash'], payload_hash_value)
def get_base_string(url, method, oauth_parameters): merge_params = oauth_parameters.copy() return "{}&{}&{}".format(util.uri_rfc3986_encode(method.upper()), util.uri_rfc3986_encode(util.normalize_url(url)), util.uri_rfc3986_encode(util.normalize_params(url, merge_params)))
def test_url_encode3(self): self.assertEqual( "WhqqH%2BTU95VgZMItpdq78BWb4cE%3D%26o", Util.uri_rfc3986_encode("WhqqH+TU95VgZMItpdq78BWb4cE=&o"))
def test_url_encode1(self): self.assertEqual("Format%3DXML", Util.uri_rfc3986_encode("Format=XML"))