jpadilla / pyjwt

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

Support returning `Algorithm.to_jwk(key)` as dict #880

Closed fluxth closed 1 year ago

fluxth commented 1 year ago

Currently, the when generating a JWK, the only way to get a dict from PyJWT is to:

jwk_str = algo.to_jwk(key)
jwk = json.loads(jwk_str)

This is very unfortunate when you want to add more fields into the JWK:

modified_jwk = {
    **json.loads(jwk_str),
    "kid": "keyid",
}

There should be a way in the API to get the underlying dict from the Algorithm.to_jwk(key) functions. My proposal is to add another variant Algorithm.to_jwk_dict(key) to not break API compatibility.

I'm planning to send PR implementing this, but I figured I should raise an issue first to see if there's any better way of doing this...

Viicos commented 1 year ago

Maybe with a as_dict argument to to_jwk, with a default value of False for backwards compatibility?

fluxth commented 1 year ago

Hmm, that might work... I'm a bit concerned about the return type hint but with @overload it should work fine

Viicos commented 1 year ago

I'm a bit concerned about the return type hint but with @overload it should work fine

yes using overloads is pretty common for this use case