Skip to content

Commit 6812412

Browse files
authored
Merge pull request #33 from RomainGehrig/master
Fix OAuth1RSA oauth_body_hash of None body
2 parents 8623605 + ac5b1d4 commit 6812412

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

oauth1/oauth_ext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ def _hash(self, message: str) -> str:
9797
return self.hash_f(message.encode('utf8')).digest()
9898
elif type(message) is bytes:
9999
return self.hash_f(message).digest()
100-
else:
100+
elif not message:
101101
# Generally for calls where the payload is empty. Eg: get calls
102102
# Fix for AttributeError: 'NoneType' object has no attribute 'encode'
103-
return self.hash_f(str(message).encode('utf8')).digest()
103+
return self.hash_f("".encode('utf8')).digest()
104104

105105
@staticmethod
106106
def signable_message(r: PreparedRequest, payload: dict):

tests/test_oauth_ext.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,37 @@ def test_oauth_body_hash_with_body_none(self):
109109
oauth_body_hash_object = oauth_object.oauth_body_hash(OAuthExtTest.mock_prepared_request, OAuthExtTest.payload)
110110

111111
# Using mock data to find the hash value
112-
hashlib_val = hashlib.sha256(str(OAuthExtTest.mock_prepared_request.body).encode('utf8')).digest()
112+
hashlib_val = hashlib.sha256(str("").encode('utf8')).digest()
113113
payload_hash_value = util.uri_rfc3986_encode(util.base64_encode(hashlib_val))
114114

115115
self.assertEqual(oauth_body_hash_object['oauth_body_hash'], payload_hash_value)
116116

117+
def test_oauth_body_hash_with_body_empty_or_none(self):
118+
def prep_request():
119+
req = PreparedRequest()
120+
req.prepare(headers={'Content-type': 'application/json', 'Accept': 'application/json'},
121+
method="POST",
122+
url="http://www.example.com")
123+
return req
124+
125+
oauth_object = OAuth1RSA(OAuthExtTest.consumer_key, OAuthExtTest.signing_key)
126+
request_empty = prep_request()
127+
request_none = prep_request()
128+
129+
request_empty.body = ""
130+
request_none.body = None
131+
132+
# Passing data to the actual func to get the value
133+
empty_body_hash = oauth_object.oauth_body_hash(request_empty, OAuthExtTest.payload)
134+
none_body_hash = oauth_object.oauth_body_hash(request_none, OAuthExtTest.payload)
135+
136+
# Using mock data to find the hash value
137+
empty_string_hash = hashlib.sha256(str("").encode('utf8')).digest()
138+
empty_string_encoded = util.uri_rfc3986_encode(util.base64_encode(empty_string_hash))
139+
140+
self.assertEqual(empty_body_hash['oauth_body_hash'], empty_string_encoded)
141+
self.assertEqual(none_body_hash['oauth_body_hash'], empty_string_encoded)
142+
117143
def test_oauth_body_hash_with_body_multipart(self):
118144
oauth_object = OAuth1RSA(OAuthExtTest.consumer_key, OAuthExtTest.signing_key)
119145
mock_request = PreparedRequest()

0 commit comments

Comments
 (0)