Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Support PyJWT v2.0.0. (#8986)
Browse files Browse the repository at this point in the history
Tests were broken due to an API changing. The code used in Synapse
proper should be compatible with both versions already.
  • Loading branch information
clokep authored and anoadragon453 committed Dec 29, 2020
1 parent e6797e0 commit 0d86b11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.d/8986.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support using PyJWT v2.0.0 in the test suite.
16 changes: 12 additions & 4 deletions tests/rest/client/v1/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,12 @@ def make_homeserver(self, reactor, clock):
self.hs.config.jwt_algorithm = self.jwt_algorithm
return self.hs

def jwt_encode(self, token, secret=jwt_secret):
return jwt.encode(token, secret, self.jwt_algorithm).decode("ascii")
def jwt_encode(self, token: str, secret: str = jwt_secret) -> str:
# PyJWT 2.0.0 changed the return type of jwt.encode from bytes to str.
result = jwt.encode(token, secret, self.jwt_algorithm)
if isinstance(result, bytes):
return result.decode("ascii")
return result

def jwt_login(self, *args):
params = json.dumps(
Expand Down Expand Up @@ -725,8 +729,12 @@ def make_homeserver(self, reactor, clock):
self.hs.config.jwt_algorithm = "RS256"
return self.hs

def jwt_encode(self, token, secret=jwt_privatekey):
return jwt.encode(token, secret, "RS256").decode("ascii")
def jwt_encode(self, token: str, secret: str = jwt_privatekey) -> str:
# PyJWT 2.0.0 changed the return type of jwt.encode from bytes to str.
result = jwt.encode(token, secret, "RS256")
if isinstance(result, bytes):
return result.decode("ascii")
return result

def jwt_login(self, *args):
params = json.dumps(
Expand Down

0 comments on commit 0d86b11

Please sign in to comment.