-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Resource Server - Multi-Tenant Jwt Decoder by Issuer #6779
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
gburboz
wants to merge
1
commit into
spring-projects:master
from
gburboz:feature/rs-multi-tenant-jwt
Closed
Changes from all commits
Commits
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
76 changes: 76 additions & 0 deletions
76
...rc/main/java/org/springframework/security/oauth2/jwt/MultiTenantDelegatingJwtDecoder.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,76 @@ | ||
package org.springframework.security.oauth2.jwt; | ||
|
||
import com.nimbusds.jose.JOSEObject; | ||
import com.nimbusds.jose.util.Base64URL; | ||
import com.nimbusds.jose.util.JSONObjectUtils; | ||
import net.minidev.json.JSONObject; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.text.ParseException; | ||
import java.util.Map; | ||
|
||
public class MultiTenantDelegatingJwtDecoder implements JwtDecoder { | ||
private static final String DECODING_ERROR_MESSAGE_TEMPLATE = | ||
"An error occurred while attempting to decode the Jwt: %s"; | ||
|
||
private JwtDecoder decoderDefault; | ||
|
||
private Map<String, JwtDecoder> decoderByIssuer; | ||
|
||
public MultiTenantDelegatingJwtDecoder(JwtDecoder decoderDefault) { | ||
this(decoderDefault, null); | ||
} | ||
|
||
public MultiTenantDelegatingJwtDecoder( | ||
Map<String, JwtDecoder> decoderByIssuer) { | ||
this(null, decoderByIssuer); | ||
} | ||
|
||
public MultiTenantDelegatingJwtDecoder( | ||
JwtDecoder decoderDefault, | ||
Map<String, JwtDecoder> decoderByIssuer) { | ||
Assert.isTrue(decoderDefault != null || !CollectionUtils.isEmpty(decoderByIssuer), | ||
"At least one of decoderDefault or decoderByIssuer must be provided"); | ||
this.decoderDefault = decoderDefault; | ||
this.decoderByIssuer = decoderByIssuer; | ||
} | ||
|
||
@Override | ||
public Jwt decode(String token) throws JwtException { | ||
JwtDecoder jwtDecoder = null; | ||
if (!CollectionUtils.isEmpty(decoderByIssuer)) { | ||
String issuer = parseAndFindIssuer(token); | ||
if (issuer == null && decoderDefault == null) { | ||
throw new JwtException( | ||
"Unable to determine issuer for the token"); | ||
} else { | ||
jwtDecoder = decoderByIssuer.get(issuer); | ||
if (jwtDecoder == null && decoderDefault == null) { | ||
throw new JwtException(String.format( | ||
"JwtDecoder has not been configured for issuer %s", issuer)); | ||
} | ||
} | ||
} | ||
if (jwtDecoder == null && decoderDefault != null) { | ||
jwtDecoder = decoderDefault; | ||
} else { | ||
throw new JwtException(String.format("Unable to determine JwtDecoder")); | ||
} | ||
return jwtDecoder.decode(token); | ||
} | ||
|
||
private String parseAndFindIssuer(String token) { | ||
try { | ||
Base64URL[] parts = JOSEObject.split(token); | ||
JSONObject payload = JSONObjectUtils.parse(parts[1].decodeToString()); | ||
return payload.getAsString("iss"); | ||
} catch (ArrayIndexOutOfBoundsException | ||
| NullPointerException | ||
| ParseException ex) { | ||
throw new JwtException(String.format( | ||
DECODING_ERROR_MESSAGE_TEMPLATE, ex.getMessage()), ex); | ||
} | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...ot/oauth2resourceserver-multitenancy/src/main/java/sample/OAuth2ResourceServerConfig.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,77 @@ | ||
package sample; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.NestedConfigurationProperty; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@Configuration | ||
@ConfigurationProperties("spring.security.oauth2.resourceserver") | ||
public class OAuth2ResourceServerConfig { | ||
|
||
@NestedConfigurationProperty | ||
private JwtConfig jwt; | ||
|
||
@NestedConfigurationProperty | ||
private Set<JwtConfig> multiTenantJwt = new HashSet<>(); | ||
|
||
@NestedConfigurationProperty | ||
private OpaqueConfig opaque; | ||
|
||
public JwtConfig getJwt() { | ||
return jwt; | ||
} | ||
|
||
public void setJwt(JwtConfig jwt) { | ||
this.jwt = jwt; | ||
} | ||
|
||
public Set<JwtConfig> getMultiTenantJwt() { | ||
return multiTenantJwt; | ||
} | ||
|
||
public OpaqueConfig getOpaque() { | ||
return opaque; | ||
} | ||
|
||
public void setOpaque(OpaqueConfig opaque) { | ||
this.opaque = opaque; | ||
} | ||
|
||
public static class JwtConfig { | ||
private String issuerUri; | ||
private String jwkSetUri; | ||
|
||
public String getIssuerUri() { | ||
return issuerUri; | ||
} | ||
|
||
public void setIssuerUri(String issuerUri) { | ||
this.issuerUri = issuerUri; | ||
} | ||
|
||
public String getJwkSetUri() { | ||
return jwkSetUri; | ||
} | ||
|
||
public void setJwkSetUri(String jwkSetUri) { | ||
this.jwkSetUri = jwkSetUri; | ||
} | ||
} | ||
|
||
public static class OpaqueConfig { | ||
|
||
private String introspectionUri; | ||
|
||
public String getIntrospectionUri() { | ||
return introspectionUri; | ||
} | ||
|
||
public void setIntrospectionUri(String introspectionUri) { | ||
this.introspectionUri = introspectionUri; | ||
} | ||
|
||
} | ||
} |
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
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 |
---|---|---|
|
@@ -3,6 +3,14 @@ spring: | |
oauth2: | ||
resourceserver: | ||
jwt: | ||
issuer-uri: ${mockwebserver.url} | ||
jwk-set-uri: ${mockwebserver.url}/.well-known/jwks.json | ||
multi-tenant-jwt: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not have mock server that supports JWTs with iss claims hence have put these as sample. They need not necessarily support JWT access tokens with iss claim in it. |
||
- | ||
issuer-uri: "https://accounts.google.com" | ||
jwk-set-uri: "https://www.googleapis.com/oauth2/v3/certs" | ||
- | ||
issuer-uri: "https://contoso.auth0.com/" | ||
jwk-set-uri: "https://contoso.auth0.com/.well-known/jwks.json" | ||
opaque: | ||
introspection-uri: ${mockwebserver.url}/introspect |
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.
Unfortunately this parsing occurs twice, once here and also when NimbusJwtDecoder is invoked