Skip to content

Commit 7d1512e

Browse files
committed
Deactivating customAudienceValidator (token audience is AZURE_APP_CLIENT_ID of caller, e.g. dolly-frontend).
1 parent 8014269 commit 7d1512e

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

libs/servlet-security/src/main/java/no/nav/testnav/libs/servletsecurity/jwt/MultipleIssuersJwtDecoder.java

+35-27
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import lombok.extern.slf4j.Slf4j;
55
import no.nav.testnav.libs.servletsecurity.properties.ResourceServerProperties;
66
import org.springframework.security.oauth2.core.DelegatingOAuth2TokenValidator;
7-
import org.springframework.security.oauth2.core.OAuth2Error;
87
import org.springframework.security.oauth2.core.OAuth2TokenValidator;
98
import org.springframework.security.oauth2.core.OAuth2TokenValidatorResult;
109
import org.springframework.security.oauth2.jwt.*;
@@ -21,12 +20,13 @@ class MultipleIssuersJwtDecoder implements JwtDecoder {
2120
MultipleIssuersJwtDecoder(List<ResourceServerProperties> properties) {
2221
this.decoderMap = properties
2322
.stream()
24-
.peek(config -> log.info("Configuring decoder for issuer {}", config.getIssuerUri()))
2523
.collect(Collectors.toMap(
2624
ResourceServerProperties::getIssuerUri,
27-
props -> {
28-
NimbusJwtDecoder jwtDecoder = JwtDecoders.fromIssuerLocation(props.getIssuerUri());
29-
jwtDecoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(issuerValidator(props), audienceValidator(props)));
25+
config -> {
26+
var jwtDecoder = NimbusJwtDecoder
27+
.withIssuerLocation(config.getIssuerUri())
28+
.build();
29+
jwtDecoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(defaultIssuerValidator(config), customAudienceValidator(config)));
3030
return jwtDecoder;
3131
}
3232
));
@@ -35,15 +35,13 @@ class MultipleIssuersJwtDecoder implements JwtDecoder {
3535
@Override
3636
public Jwt decode(String token) throws JwtException {
3737
try {
38-
var parsed = JWTParser.parse(token);
39-
var claims = parsed.getJWTClaimsSet();
40-
var issuer = claims.getIssuer();
41-
log.info("Decoding token from issuer {}", issuer);
42-
var decoder = decoderMap.get(issuer);
43-
log.info("Decoding using decoder {} instanceof {}", decoder, decoder.getClass());
44-
var decoded = decoder.decode(token);
45-
log.info("Decoded token with claims {}", decoded == null ? "null!?" : decoded.getClaims());
46-
return decoded;
38+
var issuer = JWTParser
39+
.parse(token)
40+
.getJWTClaimsSet()
41+
.getIssuer();
42+
return decoderMap
43+
.get(issuer)
44+
.decode(token);
4745
} catch (ParseException e) {
4846
log.error("Feil ved parsing av token", e);
4947
throw new JwtException("Feil ved parsing av token", e);
@@ -56,23 +54,33 @@ public Jwt decode(String token) throws JwtException {
5654
}
5755
}
5856

59-
private static OAuth2TokenValidator<Jwt> issuerValidator(ResourceServerProperties properties) {
57+
private static OAuth2TokenValidator<Jwt> defaultIssuerValidator(ResourceServerProperties properties) {
6058
return JwtValidators.createDefaultWithIssuer(properties.getIssuerUri());
6159
}
6260

63-
private static OAuth2TokenValidator<Jwt> audienceValidator(ResourceServerProperties properties) {
64-
return token -> token
65-
.getAudience()
66-
.stream()
67-
.anyMatch(audience -> properties.getAcceptedAudience().contains(audience)) ?
68-
OAuth2TokenValidatorResult.success() :
69-
OAuth2TokenValidatorResult.failure(error(properties.getAcceptedAudience(), token.getAudience()));
61+
private static OAuth2TokenValidator<Jwt> customAudienceValidator(ResourceServerProperties properties) {
62+
return token -> {
63+
var valid = token
64+
.getAudience()
65+
.stream()
66+
.anyMatch(audience -> properties.getAcceptedAudience().contains(audience));
67+
if (!valid) {
68+
log.error("Fant ikke påkrevd audience {} i tokenet, bare {}", properties.getAcceptedAudience(), token.getAudience());
69+
}
70+
return OAuth2TokenValidatorResult.success();
71+
};
72+
// return token -> token
73+
// .getAudience()
74+
// .stream()
75+
// .anyMatch(audience -> properties.getAcceptedAudience().contains(audience)) ?
76+
// OAuth2TokenValidatorResult.success() :
77+
// OAuth2TokenValidatorResult.failure(error(properties.getAcceptedAudience(), token.getAudience()));
7078
}
7179

72-
private static OAuth2Error error(List<String> acceptedAudiences, List<String> tokenAudiences) {
73-
var message = "Fant ikke påkrevd audience %s i tokenet, bare %s".formatted(acceptedAudiences, tokenAudiences);
74-
log.error(message);
75-
return new OAuth2Error("invalid_token", message, null);
76-
}
80+
// private static OAuth2Error error(List<String> acceptedAudiences, List<String> tokenAudiences) {
81+
// var message = "Fant ikke påkrevd audience %s i tokenet, bare %s".formatted(acceptedAudiences, tokenAudiences);
82+
// log.error(message);
83+
// return new OAuth2Error("invalid_token", message, null);
84+
// }
7785

7886
}

0 commit comments

Comments
 (0)