jpadilla / pyjwt

JSON Web Token implementation in Python
https://pyjwt.readthedocs.io
MIT License
5.05k stars 676 forks source link

Add PyJWT._{de,en}code_payload hooks #829

Closed akx closed 1 year ago

akx commented 1 year ago

This enables custom subclasses of PyJWT to customize decoding of the payload data of the JWS.

There's an example on how to subclass the function in the test case.

Refs #666 (reimplementation of it) Refs #753 (reimplementation of it, borrows test case)

russoale commented 1 year ago

Alternatively, this could be implemented as well by passing a callable

    def decode(
        self,
        jwt: str,
        ...
        # hook for custom payload decompression 
        decompress_payload: Optional[Callable[Dict]] = None,
        ...
        **kwargs,
    ) -> Dict[str, Any]:

   # passing to 

    def decode_complete(
        self,
        jwt: str,
                ...
        # hook for custom payload decompression 
        decompress_payload: Optional[Callable[Dict]] = None,
        ...
        # kwargs
        **kwargs,
    ) -> Dict[str, Any]:

Then checking if the function has been passed and use it or proceed with default

akx commented 1 year ago

@russoale Possibly, but those are pretty arg-heavy functions as-is. Also, "decompress" is not a good name – the function can do something else than just decompress.

simon-sk commented 1 year ago

@akx Would it be possible to add a similar hook for the encode function as well?

akx commented 1 year ago

@simon-sk Good idea. Added.