Skip to content

Support symmetric key for JwtDecoder #6495

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

Closed

Conversation

jgrandja
Copy link
Contributor

Resolves #5465

@jgrandja jgrandja requested a review from rwinch January 31, 2019 03:10
Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @jgrandja I have provided feedback inline.

@jgrandja
Copy link
Contributor Author

Thanks for the feedback @rwinch @jzheaux. I've resolved all comments and pushed updates.

}

public static SecretKey secretKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to have this be generated? It would be nice if it were a static value, e.g.

Suggested change
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
return new SecretKeySpec(Base64.getDecoder().decode("Ky8xmu1fg/OqVlUr9PRKhutauHvuj0rLHExRk+5XkSU="), "AES")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no particular reason. I feel secretKey() can be useful as some tests may require their own secret. Is there a benefit to having a static value other than the one-time optimization at class load?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a "pre-generated" one is faster. To be clear, I think secretKey(), should stay, it should just use load a pre-generated key instead of a live-generated key.

Also, taking a look at a few other tests, it is fairly common to use a pre-generated value, for example, NimbusReactiveJwtDecoderTests#decodeWhenRSAPublicKeyThenSuccess, and the resource server tests.

Copy link
Contributor Author

@jgrandja jgrandja Mar 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a "pre-generated" one is faster

True, but the generated key is done once at class load so I don't think this will make the tests run slower.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have this be a static value too. This is not only faster, but it ensures the tests are consistent and predictable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through the tests, I don't see this method being used. Let's remove it, adding it back in when there is a clear need for a test to have a dynamically-generated secret key.

@jgrandja
Copy link
Contributor Author

jgrandja commented Mar 8, 2019

@rwinch I've implemented dedicated types for the JWS algorithms.

package org.springframework.security.oauth2.jose.jws;

public enum MacAlgorithm implements JwaAlgorithm {
   ...

public enum SignatureAlgorithm implements JwaAlgorithm {
   ...

I've also considered this design for when we implement support for JWE.
I'm thinking we would introduce the following:

package org.springframework.security.oauth2.jose.jwe;

public enum SecreKeyEncryptionAlgorithm implements JwaAlgorithm {
   ...

public enum PublicKeyEncryptionAlgorithm implements JwaAlgorithm {
   ...

Copy link
Member

@rwinch rwinch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @jgrandja! I've provided feedback inline

* @param jwsAlgorithm the JWS algorithm to use
* @return a {@link SecretKeyJwtProcessorBuilder} for further configurations
*/
public SecretKeyJwtProcessorBuilder jwsAlgorithm(MacAlgorithm jwsAlgorithm) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to rename this method and argument now that it is of type MacAlgorithm?

}

public static SecretKey secretKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to have this be a static value too. This is not only faster, but it ensures the tests are consistent and predictable.

* @param secretKey the {@code SecretKey}
* @param jwsAlgorithm the {@link MacAlgorithm JWS algorithm}
*/
public NimbusReactiveJwtDecoder(SecretKey secretKey, MacAlgorithm jwsAlgorithm) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename jwsAlgorithm to macAlgorithm?

@jgrandja
Copy link
Contributor Author

@rwinch @jzheaux I've addressed all the feedback so please go ahead with the review.

If we can get this into M2, that would be ideal.

@jgrandja jgrandja force-pushed the gh-5465-symmetric-decoder branch from 0b66889 to 92dcb52 Compare April 11, 2019 17:57
@jgrandja
Copy link
Contributor Author

jgrandja commented Apr 11, 2019

@rwinch @jzheaux There were quite a few changes to NimbusJwtDecoder and NimbusReactiveJwtDecoder since I initially submitted this PR, e.g. JwtProcessors was removed in 9478abe which my PR was using.

This resulted in quite a few conflicts and was very difficult to merge. So I had no choice but to start from master and apply my updates manually and force push.

I know timing is tight to get this into M2 but it would be ideal as this one has been around for quite some time.

Copy link
Contributor

@jzheaux jzheaux left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgrandja, this looks great - I've left just one comment inline.

}

public static SecretKey secretKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking through the tests, I don't see this method being used. Let's remove it, adding it back in when there is a clear need for a test to have a dynamically-generated secret key.

@jgrandja jgrandja added this to the 5.2.0.M2 milestone Apr 12, 2019
@jgrandja
Copy link
Contributor Author

Merged via bed3371

@jgrandja jgrandja closed this Apr 12, 2019
@jgrandja jgrandja deleted the gh-5465-symmetric-decoder branch April 12, 2019 18:26
@rwinch rwinch added type: enhancement A general enhancement in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) labels May 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: oauth2 An issue in OAuth2 modules (oauth2-core, oauth2-client, oauth2-resource-server, oauth2-jose) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide support for symmetric key verification via JwtDecoder
3 participants