Improve ergonomics or document usage to emulate python-jose/PyJWT #346
Replies: 14 comments 1 reply
-
What is missing from the last example here: https://jwcrypto.readthedocs.io/en/latest/jwt.html#examples ? |
Beta Was this translation helpful? Give feedback.
-
I'm going to take the test ran in
The result of this is when the functions is called is:
jt.JWT(jwt=token, key=key, algs=algorithms) and I get, I think it'd be great to point at what a valid key type could be in the error message. Now, I need to convert the key from a string to a valid JWK that will be usable by |
Beta Was this translation helpful? Give feedback.
-
The last example here shows how to import a public key from a PEM file, which is waht you seem to have: If you have the blob already you can just pass in the blob, you do not have to .read() from a file. can you show what formats are the other arguments of the pyJWT jwt.decode() function? |
Beta Was this translation helpful? Give feedback.
-
N.B: This is Thanks, this does bring me closer. The other arguments are called like this: jwt.decode(token, key, algorithms=algorithms, audience=self.client_id, **kwargs) The fonction looks like this in full: def decode(token, key, algorithms=None, options=None, audience=None, issuer=None, subject=None, access_token=None) The
EDIT: I suppose this is all things we can pass in the header? |
Beta Was this translation helpful? Give feedback.
-
Sorry I was not clear, In jwcrypto for example you pass the claims in a dict Here is a link to the docs before generation broke (fixing as we speak): For example to validate a specific issuer you would pass in a check_claims dict with the issuer to check like this: In your case I see an audience claim so: Assuming self.client_id is a properly formatted string for audience as specified in RFC 7519 |
Beta Was this translation helpful? Give feedback.
-
You can see more examples on how to pass claims to check in https://github.com/latchset/jwcrypto/blob/main/jwcrypto/tests.py |
Beta Was this translation helpful? Give feedback.
-
Yes indeed! The For example, |
Beta Was this translation helpful? Give feedback.
-
it depends on what they mean by that, verify_aud is not a standard attribute, if I had to guess the verify_aud is what goes in the check_claims dict as the aud claim, if not you'll have to read their documentation to understand what it is. |
Beta Was this translation helpful? Give feedback.
-
ahh it seems that those verify_ = True/False are ways to enforce or skip specific claims checks If you do not want to check 'aud' you can simply not pass it to check_claims, only exp and nbf are implicitly check, because they are dates and the library can get the current date easily, for all other claims you have to pass values to check against via check_claims, and if you pass nothing nothing is checked |
Beta Was this translation helpful? Give feedback.
-
It's what I ended up doing, I'll probably parse their options dict to add/remove fields that I pass to |
Beta Was this translation helpful? Give feedback.
-
Last question, is there an easy way to create a private key in a single string like the PEM format? Right now I get a dict when I do |
Beta Was this translation helpful? Give feedback.
-
key.export() will give you a standard json string with both private/public part (or just the secret for a symmetric key). So for example: |
Beta Was this translation helpful? Give feedback.
-
Again check out the JWK exmples in the docs |
Beta Was this translation helpful? Give feedback.
-
Thanks a ton for the timely help! |
Beta Was this translation helpful? Give feedback.
-
There's some discussion, for example in python-keycloak,to migrate away from
python-jose
as it isn't maintained anymore. I thinkjwcrypto
fits the bill but without some extra security knowledge, it's hard to make the jump.For example, the current way in the above library, to decode a JWT is:
The public key is provided by keycloak.
If we do something similar (in the API sense, not with correctness in mind) with
jwcrypto
we get:But this won't work, in this case for example that external public key is not even a
JWK
object but if we try to convert it (how?) then I could only getJWSInvalidSignature
or something similar.I think it would be an improvement to document how to use
jwcrypto
in that case and/or provide higher level functions which take the correct steps for a potential user.Please do tell if this example is bad or unclear.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions