Skip to content

JWT decoding in python backend #8807

Discussion options

You must be logged in to vote

reverse engineer next-auth code and it works fine now.
Sharing the code :

import json
from typing import Any, Dict

from hkdf import Hkdf
from jose.jwe import decrypt, encrypt


def __encryption_key(secret: str):
    return Hkdf("", bytes(secret, "utf-8")).expand(b"NextAuth.js Generated Encryption Key", 32)


def encode_jwe(payload: Dict[str, Any], secret: str):
    data = bytes(json.dumps(payload), "utf-8")
    key = __encryption_key(secret)
    return bytes.decode(encrypt(data, key), "utf-8")


def decode_jwe(token: str, secret: str):
    decrypted = decrypt(token, __encryption_key(secret))

    if decrypted:
        return json.loads(bytes.decode(decrypted, "utf-8"))
    else:
        r…

Replies: 3 comments 8 replies

Comment options

You must be logged in to vote
3 replies
@poshak-rialtes
Comment options

Answer selected by poshak-rialtes
@poshak-rialtes
Comment options

@sokenny
Comment options

Comment options

You must be logged in to vote
1 reply
@poshak-rialtes
Comment options

Comment options

You must be logged in to vote
4 replies
@kewldan
Comment options

@pimpadev
Comment options

@cblberlin
Comment options

@gajendar-work
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
7 participants