-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
krajcsovszkig-ms
wants to merge
3
commits into
spring-projects:master
from
krajcsovszkig-ms:gh-6881-jose
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
oauth2/oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtEncoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
// TODO: JwtEncoders a' la JwtDecoders? | ||
|
||
// TODO: reactive stuff |
40 changes: 40 additions & 0 deletions
40
.../oauth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtEncoderFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...auth2-jose/src/main/java/org/springframework/security/oauth2/jwt/JwtSigningException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It might give a more type-safe experience to do:
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 theConsumer
just before signing.cc @jgrandja
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.
@jzheaux I don't think the
JwtEncoder
should set any default claims, those are going to come from theClientRegistration
, so aConsumer
doesn't really feel intuitive to me here. For type safety we could create aJwtClaimsSet
class, which could be used for the overrides as well in theClientRegistration
.