Skip to content

Commit 0fac902

Browse files
committed
Added support for pyjwt 2.0
1 parent 8bfaf30 commit 0fac902

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

core/auth.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from Crypto.Cipher import AES, PKCS1_OAEP # pycryptodome
1313
from Crypto.PublicKey import RSA
1414
from Crypto.Random import get_random_bytes
15+
from cryptography.hazmat.primitives import serialization
16+
from cryptography.hazmat.backends import default_backend
1517

1618
# Example ACL stored with object
1719
# "acl" : { "ids" : [ { "accessID" : "", "read" : False, "write" : False, "delete" : False } ], "fields" : [ { "field" : "passwordHash", "ids" : [ { "accessID" : "", "read" : False, "write" : False, "delete" : False } ] } ] }
@@ -97,6 +99,9 @@ def new(self,name):
9799
with open(Path(authSettings["rsa"]["key"])) as f:
98100
sessionPrivateKey = f.read()
99101

102+
public_key = serialization.load_pem_public_key( sessionPublicKey.encode(), backend=default_backend() )
103+
private_key = serialization.load_pem_private_key( sessionPrivateKey.encode(), password=None, backend=default_backend() )
104+
100105
requiredhType = "j1"
101106

102107
def meetsPasswordPolicy(password):
@@ -175,15 +180,15 @@ def generateSession(dataDict):
175180
dataDict["expiry"] = time.time() + authSettings["sessionTimeout"]
176181
if "CSRF" not in dataDict:
177182
dataDict["CSRF"] = secrets.token_urlsafe(16)
178-
return jwt.encode(dataDict, sessionPrivateKey.encode(), algorithm="RS256")
183+
return jwt.encode(dataDict, private_key, algorithm="RS256")
179184

180185
def generateSystemSession():
181186
data = { "expiry" : time.time() + 10, "admin" : True, "_id" : 0, "user" : "system", "primaryGroup" : 0, "authenticated" : True, "api" : True }
182-
return jwt.encode(data, sessionPrivateKey.encode(), algorithm="RS256")
187+
return jwt.encode(data, private_key, algorithm="RS256")
183188

184189
def validateSession(sessionToken):
185190
try:
186-
dataDict = jwt.decode(sessionToken, sessionPublicKey.encode(), algorithm="RS256")
191+
dataDict = jwt.decode(sessionToken, public_key, algorithms=["RS256"])
187192
if dataDict["authenticated"]:
188193
if dataDict["expiry"] < time.time():
189194
return None

0 commit comments

Comments
 (0)