-
Notifications
You must be signed in to change notification settings - Fork 374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle parsed JSON JWKS input with string keys #348
Conversation
Hello, @martinemde! This is your first Pull Request that will be reviewed by SourceLevel, an automatic Code Review service. It will leave comments on this diff with potential issues and style violations found in the code as you push new commits. You can also see all the issues found on this Pull Request on its review page. Please check our documentation for more information. |
key_n = decode_open_ssl_bn(jwk_n) | ||
key_e = decode_open_ssl_bn(jwk_e) | ||
|
||
if key.respond_to?(:set_key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JWT::JWK::RSA#self.rsa_pkey manually dispatches method call
SourceLevel has finished reviewing this Pull Request and has found:
|
Hi, this feature is great. It's always a hassle to ensure the keys are the correct type. Im wondering if it would be a good idea to transform the keys into either strings or symbols before using the hash, that would remove the need to always check for both key types whenever the values are accessed. |
I think most of the hash lookups are during import so it wouldn't buy us much. The example in the readme for loading JWK indicates that caching should be performed inside the loader (before this import step). Unless I'm mistaken, after importing, a single key is grabbed from the key set and used to check the signature, then the entire thing is discarded. Given just how very fast and lightweight 2 hash lookups is compared to deep copying a parsed json object of unknown size, I'd recommend we don't convert the input in any way. |
This fixes an issue where JSON.parse results of an actual .jwks file fail to import into the key finder because they have string keys. Errors also improved in a way that helps debugging issues with JWKS loading.
Yay, thank you! |
I thank you for the contribution! Thanks! 👍 |
When decoded JSON is passed to the JWK.import, there are difficult to diagnose failures that occur.
Since the goal of JWKS is to support loading of json files, we should cleanly support string keys as that is the natural input to these methods.
This PR creates support for directly parsed JSON with string keys.
I have copied below the spec output without the fixes I added. You can see from the failures that the errors are very difficult to understand when all that has happened is the keys were missed.
For instance, "could not find public key" is confusing because debugging shows a perfect match between your JWK and the JWT kid. It's simply the fact that the key is a string and this is not expected.
Failure/Error: raise ::JWT::DecodeError, "Could not find public key for kid #{kid}" unless jwk
Failure/Error: expect { subject }.to raise_error(JWT::JWKError, "Key format is invalid for RSA")
Linking to #95 also because it is somewhat related to the string/symbol issue here.
EDIT:
The source of this problem could be tied back to the README example which uses
.export
instead of parsing an actual parsed JSON Web Key as the input to the key finder. We could updateexport
to return string keys to make it more clear.