Skip to content

Commit

Permalink
Update calls to hmac to use bytes instead of strings so it works on py3.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnjlee committed Jan 3, 2020
1 parent 5386427 commit 2093048
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
7 changes: 5 additions & 2 deletions schoolyourself/schoolyourself.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""The base class for School Yourself XBlocks (lessons and reviews)."""

from __future__ import absolute_import
import hashlib
import hmac
import os
import pkg_resources
Expand Down Expand Up @@ -157,8 +158,10 @@ def get_partner_url_params(self, shared_key=None):
if user_id:
url_params["partner_user_id"] = user_id
if shared_key:
url_params["partner_signature"] = hmac.new(str(shared_key),
user_id).hexdigest()
url_params["partner_signature"] = hmac.new(
bytes(shared_key, "utf-8"),
bytes(user_id, "utf-8"),
digestmod=hashlib.md5).hexdigest()

return url_params

Expand Down
9 changes: 6 additions & 3 deletions schoolyourself/schoolyourself_review.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""An XBlock that displays School Yourself reviews and may publish grades."""

from __future__ import absolute_import
import hashlib
import hmac
import six.moves.urllib.request, six.moves.urllib.parse, six.moves.urllib.error

Expand Down Expand Up @@ -123,17 +124,19 @@ def handle_grade_json(self, data):
return "bad_request"

# Verify the signature.
verifier = hmac.new(str(self.shared_key), user_id)
verifier = hmac.new(bytes(self.shared_key, "utf-8"),
bytes(user_id, "utf-8"),
digestmod=hashlib.md5)
for key in sorted(mastery):
verifier.update(key)
verifier.update(bytes(key, "utf-8"))

# Every entry should be a number.
try:
mastery[key] = float(mastery[key])
except ValueError:
return "bad_request"

verifier.update("%.2f" % mastery[key])
verifier.update(bytes("%.2f" % mastery[key], "utf-8"))


# If the signature is invalid, do nothing.
Expand Down

0 comments on commit 2093048

Please sign in to comment.