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

Closed
brunolnetto opened this issue Mar 31, 2024 · 1 comment
Closed

Minimal example of implementation with encode and decode #951

brunolnetto opened this issue Mar 31, 2024 · 1 comment
Labels
stale Issues without activity for more than 60 days

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

Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the stale Issues without activity for more than 60 days label May 31, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issues without activity for more than 60 days
Projects
None yet
Development

No branches or pull requests

1 participant