Skip to content

Commit b92d374

Browse files
author
Vincent Vatelot
authored
refact: extract duplicate code
1 parent 1254d83 commit b92d374

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

slackeventsapi/server.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,19 @@ def verify_signature(self, timestamp, signature):
5252
# Python 2.7.6 doesn't support compare_digest
5353
# It's recommended to use Python 2.7.7+
5454
# noqa See https://docs.python.org/2/whatsnew/2.7.html#pep-466-network-security-enhancements-for-python-2-7
55+
req = str.encode('v0:' + str(timestamp) + ':') + request.get_data()
56+
request_hash = 'v0=' + hmac.new(
57+
str.encode(self.signing_secret),
58+
req, hashlib.sha256
59+
).hexdigest()
60+
5561
if hasattr(hmac, "compare_digest"):
56-
req = str.encode('v0:' + str(timestamp) + ':') + request.get_data()
57-
request_hash = 'v0=' + hmac.new(
58-
str.encode(self.signing_secret),
59-
req, hashlib.sha256
60-
).hexdigest()
6162
# Compare byte strings for Python 2
6263
if (sys.version_info[0] == 2):
6364
return hmac.compare_digest(bytes(request_hash), bytes(signature))
6465
else:
6566
return hmac.compare_digest(request_hash, signature)
6667
else:
67-
# So, we'll compare the signatures explicitly
68-
req = str.encode('v0:' + str(timestamp) + ':') + request.get_data()
69-
request_hash = 'v0=' + hmac.new(
70-
str.encode(self.signing_secret),
71-
req, hashlib.sha256
72-
).hexdigest()
73-
7468
if len(request_hash) != len(signature):
7569
return False
7670
result = 0

0 commit comments

Comments
 (0)