|
12 | 12 | from Crypto.Cipher import AES, PKCS1_OAEP # pycryptodome |
13 | 13 | from Crypto.PublicKey import RSA |
14 | 14 | from Crypto.Random import get_random_bytes |
| 15 | +from cryptography.hazmat.primitives import serialization |
| 16 | +from cryptography.hazmat.backends import default_backend |
15 | 17 |
|
16 | 18 | # Example ACL stored with object |
17 | 19 | # "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): |
97 | 99 | with open(Path(authSettings["rsa"]["key"])) as f: |
98 | 100 | sessionPrivateKey = f.read() |
99 | 101 |
|
| 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 | + |
100 | 105 | requiredhType = "j1" |
101 | 106 |
|
102 | 107 | def meetsPasswordPolicy(password): |
@@ -175,15 +180,15 @@ def generateSession(dataDict): |
175 | 180 | dataDict["expiry"] = time.time() + authSettings["sessionTimeout"] |
176 | 181 | if "CSRF" not in dataDict: |
177 | 182 | dataDict["CSRF"] = secrets.token_urlsafe(16) |
178 | | - return jwt.encode(dataDict, sessionPrivateKey.encode(), algorithm="RS256") |
| 183 | + return jwt.encode(dataDict, private_key, algorithm="RS256") |
179 | 184 |
|
180 | 185 | def generateSystemSession(): |
181 | 186 | 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") |
183 | 188 |
|
184 | 189 | def validateSession(sessionToken): |
185 | 190 | try: |
186 | | - dataDict = jwt.decode(sessionToken, sessionPublicKey.encode(), algorithm="RS256") |
| 191 | + dataDict = jwt.decode(sessionToken, public_key, algorithms=["RS256"]) |
187 | 192 | if dataDict["authenticated"]: |
188 | 193 | if dataDict["expiry"] < time.time(): |
189 | 194 | return None |
|
0 commit comments