Replies: 10 comments 1 reply
-
I'm also wondering how to do this. In fastapi_azure_auth, I can pass a an "iss_callable" to the authorization scheme. I use this to whitelist tenants in a multi tenant setup. |
Beta Was this translation helpful? Give feedback.
-
The
|
Beta Was this translation helpful? Give feedback.
-
Hello @lepture, Thank you for your suggestion, I believe this is not sufficient, I will try to explain why... The secure process as far as I understand should be:
Having this done in the I also could not find how to do so in the new I believe that the simplest method would be something like:
What do you think? |
Beta Was this translation helpful? Give feedback.
-
@alonbl actually, with joserfc you can do it step by step. I'll update the documentation at https://jose.authlib.org/en/dev/recipes/azure/ |
Beta Was this translation helpful? Give feedback.
-
Hello @lepture , Thank you for your example: def load_key(obj: CompactSignature):
claims = json.loads(obj.payload)
issuer_url = claims['iss']
<snip>
# pick the key with kid
key_set = KeySet.import_key_set(resp.json())
key = key_set.get_by_kid(obj.headers()['kid'])
return key
# pass load_key as a callable key to `jwt.decode` method
jwt.decode(token_string, load_key) I am still missing the last step I outlined: Acquire the iss claim again (securely) and compare it to (1), so we know we have used the correct value to obtain the key. I also would like to suggest again to support Per example: - key_set = KeySet.import_key_set(resp.json())
- key = key_set.get_by_kid(obj.headers()['kid'])
- return key
+ return KeySet.import_key_set(resp.json()) The following codes should behave exactly the same using the library logic: jwt.decode(token, keyset)
jwt.decode(token, callback) # callback return keyset in parity with previous If the callback wishes to apply its own logic it may return a specific key, however, default behavior should avoid duplication of library implementation. Thanks! |
Beta Was this translation helpful? Give feedback.
-
@alonbl good suggestion. The callable key can return |
Beta Was this translation helpful? Give feedback.
-
Thanks @lepture : we should simplify this even more. Looking the following hunk: def load_key(obj: CompactSignature):
claims = json.loads(obj.payload)
issuer_url = claims['iss'] Parsing the claim in a safe way is already a feature of the library, there is no reason why the callback should duplicate the logic and parse the payload by itself. Also, we should keep this iss claim to later compare it to the iss after signature validation, so we make sure the insecure parser extracted the correct field. If we do this in the callback we should have somekind of context to be able to expose variables out. |
Beta Was this translation helpful? Give feedback.
-
Hello @lepture, Thank you so much for the discussion. I would like to address the following statement:
I argue that a callback to jwt is aware of jwt, it should be able to handle jwt. Lower level library classes may be unaware. The import functools
def jwt_guess_wrapper(original, arg):
obj.context["insecure_claims"] = json.loads(obj.payload)
original(obj)
def jwt_decode(key):
if callable(key):
key = functools.partial(jwt_guess_wrapper, key)
...
def mycallback(arg):
print(arg.context["insecure_claims"])
token = test1(mycallback)
print(token.context["insecure_claims"]) This actually also solves the issue of validate the insecure parsing with secure parsing as the context is available after the decode. What do you think? |
Beta Was this translation helpful? Give feedback.
-
BTW: As far as I understand the callback should return def load_key(obj: CompactSignature) -> KeyBase:
... |
Beta Was this translation helpful? Give feedback.
-
@alonbl 0.9.0 is released. Documentation on this case: https://jose.authlib.org/en/dev/recipes/azure/ |
Beta Was this translation helpful? Give feedback.
-
Hello,
If I support multiple ISS, I imagine the following sequence:
iss
claim from the jwt decode.iss
claim value is one of the allowed one (whitelist)$iss/.well-known/openid-configuration
jwks_uri
from body$jwks_uri
I believe the
authlib
library is missing the ability to extract claims before signature validation.Is there different sequence to accomplish the requirements?
I can pre-download jwks of every iss and verify the jwt against each, but this is a waste of resources (performing unnecessary RSA).
Thanks,
Beta Was this translation helpful? Give feedback.
All reactions