Skip to content

Commit

Permalink
fix(apple): PyJWT str/bytes compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
LanceMoe authored and pennersr committed Jan 11, 2021
1 parent 52978e2 commit 8b2306b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 10 additions & 2 deletions allauth/socialaccount/providers/apple/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
)


def jwt_encode(*args, **kwargs):
resp = jwt.encode(*args, **kwargs)
if isinstance(resp, bytes):
# For PyJWT <2
resp = resp.decode('utf-8')
return resp


class Scope(object):
EMAIL = "email"
NAME = "name"
Expand Down Expand Up @@ -41,9 +49,9 @@ def generate_client_secret(self):
"exp": now + timedelta(hours=1),
}
headers = {"kid": self.consumer_secret, "alg": "ES256"}
client_secret = jwt.encode(
client_secret = jwt_encode(
payload=claims, key=app.certificate_key, algorithm="ES256", headers=headers
).decode("utf-8")
)
return client_secret

def get_client_id(self):
Expand Down
5 changes: 3 additions & 2 deletions allauth/socialaccount/providers/apple/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from allauth.tests import MockedResponse, TestCase, mocked_response

from .apple_session import APPLE_SESSION_COOKIE_NAME
from .client import jwt_encode
from .provider import AppleProvider


Expand Down Expand Up @@ -89,12 +90,12 @@ def sign_id_token(payload):
Sign a payload as apple normally would for the id_token.
"""
signing_key = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(TESTING_JWT_KEYSET))
return jwt.encode(
return jwt_encode(
payload,
signing_key,
algorithm="RS256",
headers={"kid": TESTING_JWT_KEYSET["kid"]},
).decode("utf8")
)


@override_settings(
Expand Down

0 comments on commit 8b2306b

Please sign in to comment.