-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecrypt-NextAuth-JWE-Python.py
37 lines (31 loc) · 1.32 KB
/
Decrypt-NextAuth-JWE-Python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import json
from typing import Any
from Crypto.Protocol.KDF import HKDF # pip install pycryptodome
from Crypto.Hash import SHA256
from jose import jwe # pip install python-jose
from lib.getEnvironmentVariable import getEnvironmentVariable
def getDerivedEncryptionKey(secret: str) -> Any:
# Think about including the context in your environment variables.
context = str.encode("NextAuth.js Generated Encryption Key")
return HKDF(
master=secret.encode(),
key_len=32,
salt="".encode(),
hashmod=SHA256,
num_keys=1,
context=context,
)
def get_token(token: str) -> dict[str, Any]:
'''
Get the JWE payload from a NextAuth.js JWT/JWE token in Python
Steps:
1. Get the encryption key using HKDF defined in RFC5869
2. Decrypt the JWE token using the encryption key
3. Create a JSON object from the decrypted JWE token
'''
# Retrieve the same JWT_SECRET which was used to encrypt the JWE token on the NextAuth Server
jwt_secret = getEnvironmentVariable("JWT_SECRET") # Replace this with your environment variable logic - default: os.environ.get("JWT_SECRET")
encryption_key = getDerivedEncryptionKey(jwt_secret)
payload_str = jwe.decrypt(token, encryption_key).decode()
payload: dict[str, Any] = json.loads(payload_str)
return payload