4
4
import lombok .extern .slf4j .Slf4j ;
5
5
import no .nav .testnav .libs .servletsecurity .properties .ResourceServerProperties ;
6
6
import org .springframework .security .oauth2 .core .DelegatingOAuth2TokenValidator ;
7
- import org .springframework .security .oauth2 .core .OAuth2Error ;
8
7
import org .springframework .security .oauth2 .core .OAuth2TokenValidator ;
9
8
import org .springframework .security .oauth2 .core .OAuth2TokenValidatorResult ;
10
9
import org .springframework .security .oauth2 .jwt .*;
@@ -21,12 +20,13 @@ class MultipleIssuersJwtDecoder implements JwtDecoder {
21
20
MultipleIssuersJwtDecoder (List <ResourceServerProperties > properties ) {
22
21
this .decoderMap = properties
23
22
.stream ()
24
- .peek (config -> log .info ("Configuring decoder for issuer {}" , config .getIssuerUri ()))
25
23
.collect (Collectors .toMap (
26
24
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 )));
30
30
return jwtDecoder ;
31
31
}
32
32
));
@@ -35,15 +35,13 @@ class MultipleIssuersJwtDecoder implements JwtDecoder {
35
35
@ Override
36
36
public Jwt decode (String token ) throws JwtException {
37
37
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 );
47
45
} catch (ParseException e ) {
48
46
log .error ("Feil ved parsing av token" , e );
49
47
throw new JwtException ("Feil ved parsing av token" , e );
@@ -56,23 +54,33 @@ public Jwt decode(String token) throws JwtException {
56
54
}
57
55
}
58
56
59
- private static OAuth2TokenValidator <Jwt > issuerValidator (ResourceServerProperties properties ) {
57
+ private static OAuth2TokenValidator <Jwt > defaultIssuerValidator (ResourceServerProperties properties ) {
60
58
return JwtValidators .createDefaultWithIssuer (properties .getIssuerUri ());
61
59
}
62
60
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()));
70
78
}
71
79
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
+ // }
77
85
78
86
}
0 commit comments