-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from holashchand/issue-969-1.0
[LTS-1.0] Fixed issuer not matching problem
- Loading branch information
Showing
5 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
65 changes: 65 additions & 0 deletions
65
...java/dev/sunbirdrc/registry/authorization/CustomJwtDecoderProviderConfigurationUtils.java
This file contains 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,65 @@ | ||
package dev.sunbirdrc.registry.authorization; | ||
|
||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
final class CustomJwtDecoderProviderConfigurationUtils { | ||
private static final String OIDC_METADATA_PATH = "/.well-known/openid-configuration"; | ||
private static final RestTemplate rest = new RestTemplate(); | ||
private static final ParameterizedTypeReference<Map<String, Object>> typeReference = | ||
new ParameterizedTypeReference<Map<String, Object>>() {}; | ||
|
||
static Map<String, Object> getConfigurationForOidcIssuerLocation(String oidcIssuerLocation) { | ||
return getConfiguration(oidcIssuerLocation, oidc(URI.create(oidcIssuerLocation))); | ||
} | ||
|
||
static String getIssuer(Map<String, Object> configuration) { | ||
String metadataIssuer = "(unavailable)"; | ||
if (configuration.containsKey("issuer")) { | ||
metadataIssuer = configuration.get("issuer").toString(); | ||
} | ||
return metadataIssuer; | ||
} | ||
|
||
private static Map<String, Object> getConfiguration(String issuer, URI... uris) { | ||
String errorMessage = "Unable to resolve the Configuration with the provided Issuer of " + | ||
"\"" + issuer + "\""; | ||
for (URI uri : uris) { | ||
try { | ||
RequestEntity<Void> request = RequestEntity.get(uri).build(); | ||
ResponseEntity<Map<String, Object>> response = rest.exchange(request, typeReference); | ||
Map<String, Object> configuration = response.getBody(); | ||
|
||
if (configuration.get("jwks_uri") == null) { | ||
throw new IllegalArgumentException("The public JWK set URI must not be null"); | ||
} | ||
|
||
return configuration; | ||
} catch (IllegalArgumentException e) { | ||
throw e; | ||
} catch (RuntimeException e) { | ||
if (!(e instanceof HttpClientErrorException && | ||
((HttpClientErrorException) e).getStatusCode().is4xxClientError())) { | ||
throw new IllegalArgumentException(errorMessage, e); | ||
} | ||
// else try another endpoint | ||
} | ||
} | ||
throw new IllegalArgumentException(errorMessage); | ||
} | ||
|
||
private static URI oidc(URI issuer) { | ||
return UriComponentsBuilder.fromUri(issuer) | ||
.replacePath(issuer.getPath() + OIDC_METADATA_PATH) | ||
.build(Collections.emptyMap()); | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
...e/authorization/src/main/java/dev/sunbirdrc/registry/authorization/CustomJwtDecoders.java
This file contains 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,29 @@ | ||
package dev.sunbirdrc.registry.authorization; | ||
|
||
import java.util.Map; | ||
|
||
import org.springframework.security.oauth2.core.OAuth2TokenValidator; | ||
import org.springframework.security.oauth2.jwt.*; | ||
import org.springframework.util.Assert; | ||
|
||
import static org.springframework.security.oauth2.jwt.NimbusJwtDecoder.withJwkSetUri; | ||
|
||
final class CustomJwtDecoders { | ||
|
||
public static TenantJwtDecoder fromOidcIssuerLocation(String oidcIssuerLocation) { | ||
Assert.hasText(oidcIssuerLocation, "oidcIssuerLocation cannot be empty"); | ||
Map<String, Object> configuration = CustomJwtDecoderProviderConfigurationUtils.getConfigurationForOidcIssuerLocation(oidcIssuerLocation); | ||
return withProviderConfiguration(configuration); | ||
} | ||
|
||
private static TenantJwtDecoder withProviderConfiguration(Map<String, Object> configuration) { | ||
String metadataIssuer = CustomJwtDecoderProviderConfigurationUtils.getIssuer(configuration); | ||
OAuth2TokenValidator<Jwt> jwtValidator = JwtValidators.createDefaultWithIssuer(metadataIssuer); | ||
NimbusJwtDecoder jwtDecoder = withJwkSetUri(configuration.get("jwks_uri").toString()).build(); | ||
jwtDecoder.setJwtValidator(jwtValidator); | ||
return TenantJwtDecoder.from(jwtDecoder, metadataIssuer); | ||
} | ||
|
||
private CustomJwtDecoders() {} | ||
} | ||
|
This file contains 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
25 changes: 25 additions & 0 deletions
25
...re/authorization/src/main/java/dev/sunbirdrc/registry/authorization/TenantJwtDecoder.java
This file contains 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,25 @@ | ||
package dev.sunbirdrc.registry.authorization; | ||
|
||
import lombok.Getter; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.jwt.JwtDecoder; | ||
import org.springframework.security.oauth2.jwt.JwtException; | ||
|
||
public final class TenantJwtDecoder implements JwtDecoder { | ||
JwtDecoder jwtDecoder; | ||
@Getter | ||
String issuer; | ||
private TenantJwtDecoder(JwtDecoder jwtDecoder, String issuer) { | ||
this.jwtDecoder = jwtDecoder; | ||
this.issuer = issuer; | ||
} | ||
|
||
@Override | ||
public Jwt decode(String token) throws JwtException { | ||
return this.jwtDecoder.decode(token); | ||
} | ||
|
||
public static TenantJwtDecoder from(JwtDecoder jwtDecoder, String issuer) { | ||
return new TenantJwtDecoder(jwtDecoder, issuer); | ||
} | ||
} |