pyjwt icon indicating copy to clipboard operation
pyjwt copied to clipboard

PyJWT async

Open padrepitufo opened this issue 9 months ago • 6 comments

I am somewhat new to contributing to a python library, and I am not sure if the code I'm supplying is needed/wanted-by/appropriate-for this project. If there is already a suggested way to use PyJWT within an asynchronous context (like FastAPI) I would like guidance. If something like this could be useful I can add tests or make changes to align the code with the style of the project.

The purpose of this PR is to mitigate blocking code/calls such as represented in this tutorial (to get_signing_key_from_jwt):

class VerifyToken:
    """Does all the token verification using PyJWT"""
    # ...
    async def verify(self,
                     security_scopes: SecurityScopes,
                     token: Optional[HTTPAuthorizationCredentials] = Depends(HTTPBearer())
                     ):
        # ...
        try:
            signing_key = self.jwks_client.get_signing_key_from_jwt( # 👈 current behavior
                token.credentials
            ).key
        except jwt.exceptions.PyJWKClientError as error:
            # ...
        return payload
    # ...

I wish to call it like so

class VerifyToken:
    """Does all the token verification using PyJWT"""
    # ...
    async def verify(self,
                     security_scopes: SecurityScopes,
                     token: Optional[HTTPAuthorizationCredentials] = Depends(HTTPBearer())
                     ):
        # ...
        try:
            signing_key = await self.jwks_client.get_signing_key_from_jwt_async( # 👈 desired behavior
                token.credentials
            ).key
        except jwt.exceptions.PyJWKClientError as error:
            # ...
        return payload
    # ...

Thank you in advance for any guidance/direction you provide.

padrepitufo avatar May 17 '24 22:05 padrepitufo

apologies on the pre-commit failure, it wasn't working on my end for some reason so I pushed with 🤞 . I'll fix that. And of course, there is no test coverage - but I didn't want to make a deeper investment until I got some feedback.

padrepitufo avatar May 17 '24 23:05 padrepitufo

fixed (but still a lot of tests pending your response @jpadilla )

padrepitufo avatar May 18 '24 00:05 padrepitufo