Skip to content

Commit

Permalink
Added JwtDecoder beans for test profile, to avoid boilerplate mocks i…
Browse files Browse the repository at this point in the history
…n tests.
  • Loading branch information
rfc3092 committed Jan 9, 2025
1 parent 439b712 commit cc0189d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import no.nav.testnav.libs.standalone.servletsecurity.properties.ResourceServerProperties;
import no.nav.testnav.libs.standalone.servletsecurity.properties.TokenXResourceServerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.*;
import org.springframework.security.oauth2.jwt.JwtDecoder;

import java.util.List;
Expand All @@ -24,8 +22,17 @@
public class InsecureJwtServerToServerConfiguration {

@Bean
@Profile("!test")
@ConditionalOnMissingBean
public JwtDecoder jwtDecoder(List<ResourceServerProperties> properties) {
JwtDecoder jwtDecoder(List<ResourceServerProperties> properties) {
return new MultipleIssuersJwtDecoder(properties);
}

@Bean
@Profile("test")
@ConditionalOnMissingBean
JwtDecoder jwtDecoderForTesting() {
return token -> null;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package no.nav.testnav.libs.servletsecurity.config;

import no.nav.testnav.libs.securitycore.domain.tokenx.TokenXProperties;
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedId;
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedResourceServerType;
import no.nav.testnav.libs.servletsecurity.action.GetAuthenticatedToken;
import no.nav.testnav.libs.servletsecurity.action.GetUserInfo;
import no.nav.testnav.libs.servletsecurity.action.GetUserJwt;
import no.nav.testnav.libs.servletsecurity.action.*;
import no.nav.testnav.libs.servletsecurity.decoder.MultipleIssuersJwtDecoder;
import no.nav.testnav.libs.servletsecurity.exchange.AzureAdTokenService;
import no.nav.testnav.libs.servletsecurity.exchange.TokenExchange;
Expand All @@ -14,9 +10,7 @@
import no.nav.testnav.libs.servletsecurity.properties.ResourceServerProperties;
import no.nav.testnav.libs.servletsecurity.properties.TokenXResourceServerProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.*;
import org.springframework.security.oauth2.jwt.JwtDecoder;

import java.util.List;
Expand All @@ -39,9 +33,17 @@ public class SecureOAuth2ServerToServerConfiguration {

// TODO: There's also one in no.nav.testnav.libs.standalone.servletsecurity.config.SecureOAuth2ServerToServerConfiguration. testnav-ident-pool (and possibly others) includes both libraries. This should be fixed.
@Bean
@Profile("!test")
@ConditionalOnMissingBean
public JwtDecoder jwtDecoder(List<ResourceServerProperties> properties) {
JwtDecoder jwtDecoder(List<ResourceServerProperties> properties) {
return new MultipleIssuersJwtDecoder(properties);
}

@Bean
@Profile("test")
@ConditionalOnMissingBean
JwtDecoder jwtDecoderForTesting() {
return token -> null;
}

}

0 comments on commit cc0189d

Please sign in to comment.