Skip to content

Introduce JwtEncoder with JWS implementation #96

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

Merged
merged 1 commit into from
Aug 12, 2020

Conversation

jgrandja
Copy link
Collaborator

Issue gh-81

@jgrandja jgrandja added status: duplicate A duplicate of another issue type: enhancement A general enhancement labels Jul 22, 2020
@jgrandja jgrandja added this to the 0.0.1 milestone Jul 22, 2020
@jgrandja jgrandja self-assigned this Jul 22, 2020
@jgrandja jgrandja mentioned this pull request Jul 22, 2020
9 tasks
@jgrandja jgrandja marked this pull request as draft July 22, 2020 19:47
@jgrandja
Copy link
Collaborator Author

@krajcsovszkig-ms @anoopgarlapati @jzheaux Here is the initial draft of JwtEncoder with a JWS implementation.

You can run the integration test OAuth2AuthorizationCodeGrantTests.requestWhenTokenRequestValidThenResponseIncludesCacheHeaders() to test the authorization_code grant flow that returns a JWS.

Feedback would be great.

NOTE: Tests and javadoc have not been done yet.

@jgrandja jgrandja force-pushed the gh-81-jwt-encoder branch from bd1cc6c to 854176e Compare July 24, 2020 13:50
Copy link

@krajcsovszkig-ms krajcsovszkig-ms left a comment

Choose a reason for hiding this comment

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

Seems good so far!

Thanks!

}

@SuppressWarnings("unchecked")
public <T> T getKey() {

Choose a reason for hiding this comment

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

I'd prefer returning a Key here and let the caller cast it explicitly to what they think it is, or having separate getPrivateKey() and getSymmetricKey() methods that return null or throw an exception if the key field is not the correct type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was updated to:

	public <T extends Key> T getKey() {
		return (T) this.key;
	}

To enforce a Key type

* @author Joe Grandja
* @since 0.0.1
*/
public final class StaticKeyGeneratingKeyManager implements KeyManager {

Choose a reason for hiding this comment

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

I'd call this DefaultKeyManager and document that it generates HMAC and RSA keys. Or alternatively make the generated keys configurable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This implementation is strictly for development/testing and I've javadoc'd it. This is good for now but it will likely go away later on when a new implementation is provided.

}

@SuppressWarnings("unchecked")
public <T> T getHeader(String name) {

Choose a reason for hiding this comment

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

If this is public, I'd prefer returning an Object and having the caller cast it explicitly. Perhaps have a separate private one that auto-casts to make the specific getters neater.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this is cleaner than casting:

JwsAlgorithm jwsAlgorithm = joseHeader.getHeader(JoseHeaderNames.ALG)

vs.

JwsAlgorithm jwsAlgorithm = (JwsAlgorithm) joseHeader.getHeader(JoseHeaderNames.ALG)

The caller will know the type.

JWSHeader jwsHeader = convert(headers);

claims = JwtClaimsSet.from(claims)
.id(UUID.randomUUID().toString()) // TODO Is this unique enough?

Choose a reason for hiding this comment

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

It seems to be using secureRandom() so it's as good as it gets IMO.

@krajcsovszkig-ms
Copy link

Hi @jgrandja ,

Do you have an approximate ETA on this?

Do you expect the interfaces to change much? If not, I might start building on them in a week or two.

Thanks!

@jgrandja
Copy link
Collaborator Author

I'm planning on getting back to this PR this week @krajcsovszkig-ms and merging it later this week or early next week.

As an FYI, this won't make it into Spring Security 5.4 (Sep 2 release) but will be the first priority for 5.5, which will be released approx. 6 months after 5.4.

I'm not expecting major changes to the current API but likely there will be some minor changes.

I'm very interested to see how this works out on client side so please go ahead with your implementation and provide me feedback on how we can improve on the current API. Thanks!

@jgrandja jgrandja marked this pull request as ready for review August 12, 2020 20:06
@jgrandja jgrandja merged commit edefabd into spring-projects:master Aug 12, 2020
@jgrandja
Copy link
Collaborator Author

@krajcsovszkig-ms I just merged this so you're good to go on your end.

@jgrandja jgrandja deleted the gh-81-jwt-encoder branch August 12, 2020 20:20
@krajcsovszkig-ms
Copy link

@jgrandja

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants