Skip to content

JWT encoding support for OAuth 2.0 #8583

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.jwt;

import java.util.Map;

/**
* Implementations of this interface are responsible for "encoding"
* a JSON Web Token (JWT) from a {@link Jwt} to it's compact claims representation format.
*
* <p>
* JWTs may be represented using the JWS Compact Serialization format for a
* JSON Web Signature (JWS) structure or JWE Compact Serialization format for a
* JSON Web Encryption (JWE) structure. Implementors can pick which format to produce.
*
* @author Gergely Krajcsovszki
* @since TODO
* @see Jwt
* @see JwtDecoder
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7519">JSON Web Token (JWT)</a>
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7515">JSON Web Signature (JWS)</a>
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7516">JSON Web Encryption (JWE)</a>
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7515#section-3.1">JWS Compact Serialization</a>
* @see <a target="_blank" href="https://tools.ietf.org/html/rfc7516#section-3.1">JWE Compact Serialization</a>
*/
@FunctionalInterface
public interface JwtEncoder {

// TODO: should the claims be a new type, or is a Map OK?

/**
* Encodes the JWT from a set of claims to it's compact claims representation format.
*
* @param claims the JWT claims
* @return a {@link Jwt}, its {@code tokenValue} containing its compact claims representation format
* @throws JwtException if an error occurs while attempting to encode the JWT
*/
Jwt encode(Map<String, Object> claims) throws JwtException;
Copy link
Contributor

Choose a reason for hiding this comment

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

It might give a more type-safe experience to do:

String encode(Consumer<Jwt.Builder> jwtBuilderConsumer) throws JwtException

The other nice thing about this is that it makes it easier for the caller to override whatever defaults the JwtEncoder implementation sets since the implementation would apply the Consumer just before signing.

cc @jgrandja

Copy link
Author

Choose a reason for hiding this comment

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

@jzheaux I don't think the JwtEncoder should set any default claims, those are going to come from the ClientRegistration, so a Consumer doesn't really feel intuitive to me here. For type safety we could create a JwtClaimsSet class, which could be used for the overrides as well in the ClientRegistration.

}

// TODO: JwtEncoders a' la JwtDecoders?

// TODO: reactive stuff
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.oauth2.jwt;

/**
* A factory for {@link JwtEncoder}(s).
* This factory should be supplied with a type that provides
* contextual information used to create a specific {@code JwtEncoder}.
*
* @author Gergely Krajcsovszki
* @since TODO
* @see JwtEncoder
*
* @param <C> The type that provides contextual information used to create a specific {@code JwtEncoder}.
*/
@FunctionalInterface
public interface JwtEncoderFactory<C> {

/**
* Creates a {@code JwtEncoder} using the supplied "contextual" type.
*
* @param context the type that provides contextual information
* @return a {@link JwtEncoder}
*/
JwtEncoder createEncoder(C context);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.oauth2.jwt;

/**
* An exception thrown when a JWT signing-related operation fails.
*
* @author Gergely Krajcsovszki
* @since TODO
*/
public class JwtSigningException extends JwtException {
public JwtSigningException(String message) {
super(message);
}

public JwtSigningException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ JWTProcessor<SecurityContext> processor() {
this.jwsAlgorithm + ". Please indicate one of RS256, RS384, or RS512.");
}

// TODO: support EC? others?

JWSKeySelector<SecurityContext> jwsKeySelector =
new SingleKeyJWSKeySelector<>(this.jwsAlgorithm, this.key);
DefaultJWTProcessor<SecurityContext> jwtProcessor = new DefaultJWTProcessor<>();
Expand Down
Loading