Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal example of implementation with encode and decode #951

Open
brunolnetto opened this issue Mar 31, 2024 · 0 comments
Open

Minimal example of implementation with encode and decode #951

brunolnetto opened this issue Mar 31, 2024 · 0 comments

Comments

@brunolnetto
Copy link

I implemented a minimal example for this library on my FastAPI app. However, I do not know how to add the JWT_SECRET to the JWT instance using library objects.

Expected Result

from datetime import timedelta
from os import environ
from typing import Dict
from time import time
from jwt import JWT

JWT_SECRET = str(environ.get('JWT_SECRET'))
JWT_ALGORITHM = str(environ.get('JWT_ALGORITHM')) 

def token_response(token: str):
    return {
        "access_token": token,
        "token_type": "bearer"
    }

def signJWT(user_id: str, expires_delta: timedelta) -> Dict[str, str]:
    payload = {
        "user_id": user_id,
        "expires": time() + 600
    }
    jwt_obj = JWT()

    token = jwt_obj.encode(payload, key=JWT_SECRET, alg=JWT_ALGORITHM)

    return token_response(token)

def decodeJWT(token: str) -> dict:
    try:
        jwt_obj = JWT()

        decoded_token = jwt_obj.decode(token, key=JWT_SECRET, alg=JWT_ALGORITHM)
        
        return decoded_token if decoded_token["expires"] >= time.time() else None
    except:
        return {}

Actual Result

E TypeError: key must be an instance of a class implements jwt.AbstractJWKBase

Reproduction Steps

Code provided above

System Information

$ python -m jwt.help
/home/brunolnetto/.rye/py/cpython@3.12.1/install/bin/python3: Error while finding module specification for 'jwt.help' (ModuleNotFoundError: No module named 'jwt')

This command is only available on PyJWT v1.6.3 and greater. Otherwise,
please provide some basic information about your system.

Ubuntu 20.04
PyJWT==1.7.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant