Skip to content
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
@@ -1,6 +1,7 @@
package com.digitalbank.apigateway.security;

import java.util.Base64;
import java.util.List;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -12,6 +13,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
import org.springframework.security.oauth2.jwt.JwtClaimValidator;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtValidators;
import org.springframework.security.oauth2.jwt.NimbusReactiveJwtDecoder;
Expand All @@ -30,21 +32,28 @@ class GatewayJwtDecoderConfiguration {
ReactiveJwtDecoder gatewayReactiveJwtDecoder(
@Value("${auth.jwt.issuer:}") String issuer,
@Value("${auth.jwt.secret:}") String secret,
@Value("${auth.jwt.jwk-set-uri:}") String jwkSetUri) {
@Value("${auth.jwt.jwk-set-uri:}") String jwkSetUri,
@Value("${auth.jwt.audience:}") String audience,
@Value("${auth.jwt.token-purpose:}") String tokenPurpose) {
if (!StringUtils.hasText(audience) || !StringUtils.hasText(tokenPurpose)) {
throw new IllegalStateException(
"Gateway security is enabled but auth.jwt.audience and auth.jwt.token-purpose are not configured");
}
if (StringUtils.hasText(secret)) {
return hmacDecoder(secret, issuer);
return hmacDecoder(secret, issuer, audience, tokenPurpose);
}
if (StringUtils.hasText(jwkSetUri)) {
return jwkDecoder(jwkSetUri, issuer);
return jwkDecoder(jwkSetUri, issuer, audience, tokenPurpose);
}
if (StringUtils.hasText(issuer)) {
return ReactiveJwtDecoders.fromIssuerLocation(issuer);
return configuredDecoder((NimbusReactiveJwtDecoder) ReactiveJwtDecoders.fromIssuerLocation(issuer), issuer,
audience, tokenPurpose);
}
throw new IllegalStateException(
"Gateway security is enabled but no auth.jwt.secret, auth.jwt.jwk-set-uri, or auth.jwt.issuer is configured");
}

private ReactiveJwtDecoder hmacDecoder(String encodedSecret, String issuer) {
private ReactiveJwtDecoder hmacDecoder(String encodedSecret, String issuer, String audience, String tokenPurpose) {
final byte[] secret;
try {
secret = Base64.getDecoder().decode(encodedSecret);
Expand All @@ -54,22 +63,30 @@ private ReactiveJwtDecoder hmacDecoder(String encodedSecret, String issuer) {
if (secret.length < MINIMUM_HMAC_KEY_BYTES) {
throw new IllegalStateException("auth.jwt.secret must decode to at least 32 bytes");
}
return configuredDecoder(new SecretKeySpec(secret, "HmacSHA256"), issuer);
return configuredDecoder(new SecretKeySpec(secret, "HmacSHA256"), issuer, audience, tokenPurpose);
}

private ReactiveJwtDecoder jwkDecoder(String jwkSetUri, String issuer) {
private ReactiveJwtDecoder jwkDecoder(String jwkSetUri, String issuer, String audience, String tokenPurpose) {
NimbusReactiveJwtDecoder decoder = NimbusReactiveJwtDecoder.withJwkSetUri(jwkSetUri).build();
if (StringUtils.hasText(issuer)) {
decoder.setJwtValidator(JwtValidators.createDefaultWithIssuer(issuer));
}
return decoder;
return configuredDecoder(decoder, issuer, audience, tokenPurpose);
}

private ReactiveJwtDecoder configuredDecoder(SecretKey key, String issuer) {
private ReactiveJwtDecoder configuredDecoder(SecretKey key, String issuer, String audience, String tokenPurpose) {
NimbusReactiveJwtDecoder decoder = NimbusReactiveJwtDecoder.withSecretKey(key).build();
return configuredDecoder(decoder, issuer, audience, tokenPurpose);
}

private ReactiveJwtDecoder configuredDecoder(
NimbusReactiveJwtDecoder decoder, String issuer, String audience, String tokenPurpose) {
OAuth2TokenValidator<Jwt> audienceValidator = new JwtClaimValidator<List<String>>(
"aud", values -> values != null && values.contains(audience));
OAuth2TokenValidator<Jwt> purposeValidator = new JwtClaimValidator<String>(
"token_purpose", tokenPurpose::equals);
if (StringUtils.hasText(issuer)) {
OAuth2TokenValidator<Jwt> issuerValidator = JwtValidators.createDefaultWithIssuer(issuer);
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(issuerValidator));
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(issuerValidator, audienceValidator, purposeValidator));
} else {
decoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(audienceValidator, purposeValidator));
}
return decoder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"spring.cloud.config.enabled=false",
"gateway.security.enabled=true",
"auth.jwt.issuer=https://issuer.test",
"auth.jwt.audience=api-gateway",
"auth.jwt.token-purpose=user-access",
"management.health.redis.enabled=false" })
@Import(GatewaySecurityIntegrationTests.TestJwtConfiguration.class)
class GatewaySecurityIntegrationTests {
Expand Down
Loading