From 54167abf2c4b03919392805a9d49035ae756cc9e Mon Sep 17 00:00:00 2001 From: Cato Olsen Date: Wed, 11 Sep 2024 11:37:12 +0200 Subject: [PATCH] Some minor refactoring to remove duplicates/warnings. --- .../MaskinportenMockApplicationStarter.java | 43 +++++++++++++ .../config/OpenApiConfig.java | 6 +- .../config/SecurityConfig.java | 7 +- .../controller/MockController.java | 31 +++++++++ .../OauthAuthorizationServiceController.java | 47 ++++++++++++++ .../domain/AccessToken.java | 2 +- .../domain/Arguments.java | 2 +- .../service/JwtService.java | 33 +++------- .../MaskinportenMockApplicationStarter.java | 19 ------ .../controller/MockController.java | 27 -------- .../OauthAuthorizationServiceController.java | 64 ------------------- .../TokendingsMockApplicationStarter.java | 43 +++++++++++++ .../config/OpenApiConfig.java | 4 +- .../config/SecurityConfig.java | 7 +- .../controller/MockController.java | 16 +++-- .../OauthAuthorizationServiceController.java | 44 +++++-------- .../mocks/tokendings/domain/Arguments.java | 16 +++++ .../service/JwtService.java | 43 ++++--------- .../TokendingsMockApplicationStarter.java | 19 ------ .../tokendingsmock/domain/Arguments.java | 12 ---- 20 files changed, 243 insertions(+), 242 deletions(-) create mode 100644 mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/MaskinportenMockApplicationStarter.java rename mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => maskinporten}/config/OpenApiConfig.java (96%) rename mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => maskinporten}/config/SecurityConfig.java (82%) create mode 100644 mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/MockController.java create mode 100644 mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/OauthAuthorizationServiceController.java rename mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => maskinporten}/domain/AccessToken.java (86%) rename mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => maskinporten}/domain/Arguments.java (69%) rename mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => maskinporten}/service/JwtService.java (53%) delete mode 100644 mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/MaskinportenMockApplicationStarter.java delete mode 100644 mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java delete mode 100644 mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java create mode 100644 mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/TokendingsMockApplicationStarter.java rename mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => tokendings}/config/OpenApiConfig.java (96%) rename mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => tokendings}/config/SecurityConfig.java (83%) rename mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => tokendings}/controller/MockController.java (57%) rename mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => tokendings}/controller/OauthAuthorizationServiceController.java (52%) create mode 100644 mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/domain/Arguments.java rename mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/{tokendingsmock => tokendings}/service/JwtService.java (53%) delete mode 100644 mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/TokendingsMockApplicationStarter.java delete mode 100644 mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/MaskinportenMockApplicationStarter.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/MaskinportenMockApplicationStarter.java new file mode 100644 index 00000000000..12d4b4b1e52 --- /dev/null +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/MaskinportenMockApplicationStarter.java @@ -0,0 +1,43 @@ +package no.nav.testnav.mocks.maskinporten; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.ClassPathResource; +import org.springframework.web.reactive.config.EnableWebFlux; + +import no.nav.testnav.libs.reactivecore.config.CoreConfig; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + +@Import({ + CoreConfig.class, +}) +@EnableWebFlux +@SpringBootApplication +public class MaskinportenMockApplicationStarter { + public static void main(String[] args) { + SpringApplication.run(MaskinportenMockApplicationStarter.class, args); + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + public static class Utils { + + @SneakyThrows + public static String loadJson(String path) { + try (final InputStreamReader stream = new InputStreamReader(new ClassPathResource(path).getInputStream(), StandardCharsets.UTF_8)) { + return new BufferedReader(stream) + .lines() + .collect(Collectors.joining("\n")); + } + } + + } + +} diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/OpenApiConfig.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/config/OpenApiConfig.java similarity index 96% rename from mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/OpenApiConfig.java rename to mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/config/OpenApiConfig.java index 89e4fb61beb..f92d13d47b1 100644 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/OpenApiConfig.java +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/config/OpenApiConfig.java @@ -1,15 +1,13 @@ -package no.nav.testnav.mocks.tokendingsmock.config; +package no.nav.testnav.mocks.maskinporten.config; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; +import no.nav.testnav.libs.reactivecore.config.ApplicationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import no.nav.testnav.libs.reactivecore.config.ApplicationProperties; - - @Configuration public class OpenApiConfig { diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/SecurityConfig.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/config/SecurityConfig.java similarity index 82% rename from mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/SecurityConfig.java rename to mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/config/SecurityConfig.java index d6d96ac346e..10095310de9 100644 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/SecurityConfig.java +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/config/SecurityConfig.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.config; +package no.nav.testnav.mocks.maskinporten.config; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -18,6 +18,9 @@ public class SecurityConfig { @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { - return http.csrf(ServerHttpSecurity.CsrfSpec::disable).build(); + return http + .csrf(ServerHttpSecurity.CsrfSpec::disable) + .build(); } + } diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/MockController.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/MockController.java new file mode 100644 index 00000000000..e44b12e9dd7 --- /dev/null +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/MockController.java @@ -0,0 +1,31 @@ +package no.nav.testnav.mocks.maskinporten.controller; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import no.nav.testnav.mocks.maskinporten.domain.AccessToken; +import no.nav.testnav.mocks.maskinporten.domain.Arguments; +import no.nav.testnav.mocks.maskinporten.service.JwtService; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("/mock") +public class MockController { + + private final JwtService service; + + @PostMapping( + value = "/token", + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) + public Mono getToken(Arguments arguments) { + return Mono.just(service.createAccessToken(arguments.getAudience())); + } + +} \ No newline at end of file diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/OauthAuthorizationServiceController.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/OauthAuthorizationServiceController.java new file mode 100644 index 00000000000..b5dc0d0e8da --- /dev/null +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/controller/OauthAuthorizationServiceController.java @@ -0,0 +1,47 @@ +package no.nav.testnav.mocks.maskinporten.controller; + + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import no.nav.testnav.mocks.maskinporten.domain.AccessToken; +import no.nav.testnav.mocks.maskinporten.domain.Arguments; +import no.nav.testnav.mocks.maskinporten.service.JwtService; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; +import reactor.core.publisher.Mono; + +import static no.nav.testnav.mocks.maskinporten.MaskinportenMockApplicationStarter.Utils.loadJson; + +@Slf4j +@RestController +@RequiredArgsConstructor +public class OauthAuthorizationServiceController { + + private static final String JWKS; + private static final String WELL_KNOWN; + + static { + JWKS = loadJson("static/jwks.json"); + WELL_KNOWN = loadJson("static/well-known.json"); + } + + private final JwtService jwtService; + + @GetMapping(value = "/jwks", produces = MediaType.APPLICATION_JSON_VALUE) + public Mono getJWKS() { + return Mono.just(JWKS); + } + + @GetMapping(value = "/.well-known/oauth-authorization-server", produces = MediaType.APPLICATION_JSON_VALUE) + public Mono getWellKnown() { + return Mono.just(WELL_KNOWN); + } + + @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + public Mono createToken(Arguments arguments) { + return Mono.just(jwtService.createAccessToken(arguments.getAudience())); + } + +} diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/AccessToken.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/domain/AccessToken.java similarity index 86% rename from mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/AccessToken.java rename to mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/domain/AccessToken.java index 191fb10a917..fc2a7301ac4 100644 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/AccessToken.java +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/domain/AccessToken.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.domain; +package no.nav.testnav.mocks.maskinporten.domain; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/domain/Arguments.java similarity index 69% rename from mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java rename to mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/domain/Arguments.java index cdf1de74150..9f84dafddec 100644 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/domain/Arguments.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.domain; +package no.nav.testnav.mocks.maskinporten.domain; import lombok.Getter; import lombok.Setter; diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/service/JwtService.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/service/JwtService.java similarity index 53% rename from mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/service/JwtService.java rename to mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/service/JwtService.java index 64a886f0d58..19e99cb53df 100644 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/service/JwtService.java +++ b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/maskinporten/service/JwtService.java @@ -1,47 +1,32 @@ -package no.nav.testnav.mocks.tokendingsmock.service; +package no.nav.testnav.mocks.maskinporten.service; import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import com.nimbusds.jose.jwk.RSAKey; import lombok.SneakyThrows; -import org.springframework.core.io.ClassPathResource; +import no.nav.testnav.mocks.maskinporten.domain.AccessToken; import org.springframework.stereotype.Service; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.security.interfaces.RSAPrivateKey; import java.util.Calendar; import java.util.Date; import java.util.UUID; -import java.util.stream.Collectors; -import no.nav.testnav.mocks.tokendingsmock.domain.AccessToken; +import static no.nav.testnav.mocks.maskinporten.MaskinportenMockApplicationStarter.Utils.loadJson; @Service public class JwtService { - private static final String jwtSecret; + private static final String JWK; static { - jwtSecret = loadJson("static/jwk.json"); - } - - private static String loadJson(String path) { - var resource = new ClassPathResource(path); - try (final InputStreamReader stream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { - return new BufferedReader(stream) - .lines().collect(Collectors.joining("\n")); - - } catch (IOException e) { - throw new RuntimeException("Feil med paring av " + path + ".", e); - } + JWK = loadJson("static/jwk.json"); } @SneakyThrows public AccessToken createAccessToken(String audience) { + var date = Calendar.getInstance(); var expiresAt = date.getTimeInMillis() + (60 * 60 * 1000); var builder = JWT @@ -52,16 +37,14 @@ public AccessToken createAccessToken(String audience) { .withAudience(audience) .withJWTId(UUID.randomUUID().toString()) .withExpiresAt(new Date(expiresAt)); - - var privateKey = RSAKey.parse(jwtSecret).toPrivateKey(); - + var privateKey = RSAKey.parse(JWK).toPrivateKey(); return new AccessToken( builder.sign(Algorithm.RSA256(null, (RSAPrivateKey) privateKey)), "Bearer", 60 * 60 * 1000, audience - ); + } } diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/MaskinportenMockApplicationStarter.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/MaskinportenMockApplicationStarter.java deleted file mode 100644 index be270acb6ca..00000000000 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/MaskinportenMockApplicationStarter.java +++ /dev/null @@ -1,19 +0,0 @@ -package no.nav.testnav.mocks.tokendingsmock; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Import; -import org.springframework.web.reactive.config.EnableWebFlux; - -import no.nav.testnav.libs.reactivecore.config.CoreConfig; - -@Import({ - CoreConfig.class, -}) -@EnableWebFlux -@SpringBootApplication -public class MaskinportenMockApplicationStarter { - public static void main(String[] args) { - SpringApplication.run(MaskinportenMockApplicationStarter.class, args); - } -} diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java deleted file mode 100644 index 4c761725f52..00000000000 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java +++ /dev/null @@ -1,27 +0,0 @@ -package no.nav.testnav.mocks.tokendingsmock.controller; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Mono; - -import no.nav.testnav.mocks.tokendingsmock.domain.AccessToken; -import no.nav.testnav.mocks.tokendingsmock.domain.Arguments; -import no.nav.testnav.mocks.tokendingsmock.service.JwtService; - -@Slf4j -@RestController -@RequiredArgsConstructor -@RequestMapping("/mock") -public class MockController { - - private final JwtService jwtService; - - @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public Mono getToken(Arguments arguments) { - return Mono.just(jwtService.createAccessToken(arguments.getAudience())); - } -} \ No newline at end of file diff --git a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java b/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java deleted file mode 100644 index 3c3cdb5d91e..00000000000 --- a/mocks/maskinporten-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java +++ /dev/null @@ -1,64 +0,0 @@ -package no.nav.testnav.mocks.tokendingsmock.controller; - - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.ClassPathResource; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Mono; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.stream.Collectors; - -import no.nav.testnav.mocks.tokendingsmock.domain.AccessToken; -import no.nav.testnav.mocks.tokendingsmock.domain.Arguments; -import no.nav.testnav.mocks.tokendingsmock.service.JwtService; - -@Slf4j -@RestController -@RequiredArgsConstructor -public class OauthAuthorizationServiceController { - - private static final String jwks; - private static final String wellKnwon; - - static { - jwks = loadJson("static/jwks.json"); - wellKnwon = loadJson("static/well-known.json"); - } - - private final JwtService jwtService; - - private static String loadJson(String path) { - var resource = new ClassPathResource(path); - try (final InputStreamReader stream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { - return new BufferedReader(stream) - .lines().collect(Collectors.joining("\n")); - - } catch (IOException e) { - throw new RuntimeException("Feil med paring av " + path + ".", e); - } - } - - @GetMapping(value = "/jwks", produces = MediaType.APPLICATION_JSON_VALUE) - public Mono getJwks() { - return Mono.just(jwks); - } - - @GetMapping(value = "/.well-known/oauth-authorization-server", produces = MediaType.APPLICATION_JSON_VALUE) - public Mono getWellKnown() { - return Mono.just(wellKnwon); - } - - @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) - public Mono createToken(Arguments arguments) { - return Mono.just(jwtService.createAccessToken(arguments.getAudience())); - } - -} diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/TokendingsMockApplicationStarter.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/TokendingsMockApplicationStarter.java new file mode 100644 index 00000000000..d0ad59ff89c --- /dev/null +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/TokendingsMockApplicationStarter.java @@ -0,0 +1,43 @@ +package no.nav.testnav.mocks.tokendings; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; +import no.nav.testnav.libs.reactivecore.config.CoreConfig; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Import; +import org.springframework.core.io.ClassPathResource; +import org.springframework.web.reactive.config.EnableWebFlux; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.stream.Collectors; + +@Import({ + CoreConfig.class, +}) +@EnableWebFlux +@SpringBootApplication +public class TokendingsMockApplicationStarter { + + public static void main(String[] args) { + SpringApplication.run(TokendingsMockApplicationStarter.class, args); + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + public static class Utils { + + @SneakyThrows + public static String loadJson(String path) { + try (final InputStreamReader stream = new InputStreamReader(new ClassPathResource(path).getInputStream(), StandardCharsets.UTF_8)) { + return new BufferedReader(stream) + .lines() + .collect(Collectors.joining("\n")); + } + } + + } + +} diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/OpenApiConfig.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/config/OpenApiConfig.java similarity index 96% rename from mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/OpenApiConfig.java rename to mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/config/OpenApiConfig.java index 89e4fb61beb..cf907ab7847 100644 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/OpenApiConfig.java +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/config/OpenApiConfig.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.config; +package no.nav.testnav.mocks.tokendings.config; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; @@ -9,7 +9,6 @@ import no.nav.testnav.libs.reactivecore.config.ApplicationProperties; - @Configuration public class OpenApiConfig { @@ -32,4 +31,5 @@ public OpenAPI openApi(ApplicationProperties applicationProperties) { ) ); } + } \ No newline at end of file diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/SecurityConfig.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/config/SecurityConfig.java similarity index 83% rename from mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/SecurityConfig.java rename to mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/config/SecurityConfig.java index d6d96ac346e..056091cd371 100644 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/config/SecurityConfig.java +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/config/SecurityConfig.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.config; +package no.nav.testnav.mocks.tokendings.config; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -18,6 +18,9 @@ public class SecurityConfig { @Bean public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) { - return http.csrf(ServerHttpSecurity.CsrfSpec::disable).build(); + return http + .csrf(ServerHttpSecurity.CsrfSpec::disable) + .build(); } + } diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/controller/MockController.java similarity index 57% rename from mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java rename to mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/controller/MockController.java index a9729decf70..83ab0b77a1e 100644 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/MockController.java +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/controller/MockController.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.controller; +package no.nav.testnav.mocks.tokendings.controller; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -11,8 +11,8 @@ import java.util.Map; import no.nav.testnav.libs.securitycore.domain.AccessToken; -import no.nav.testnav.mocks.tokendingsmock.domain.Arguments; -import no.nav.testnav.mocks.tokendingsmock.service.JwtService; +import no.nav.testnav.mocks.tokendings.domain.Arguments; +import no.nav.testnav.mocks.tokendings.service.JwtService; @Slf4j @RestController @@ -20,11 +20,15 @@ @RequestMapping("/mock") public class MockController { - private final JwtService jwtService; + private final JwtService service; - @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) + @PostMapping( + value = "/token", + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, + produces = MediaType.APPLICATION_JSON_VALUE + ) public Mono createToken(Arguments arguments) { - var jwt = jwtService.jwtWith(Map.of("pid", arguments.getPid()), arguments.getAudience()); + var jwt = service.jwtWith(Map.of("pid", arguments.getPid()), arguments.getAudience()); return Mono.just(new AccessToken(jwt)); } diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/controller/OauthAuthorizationServiceController.java similarity index 52% rename from mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java rename to mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/controller/OauthAuthorizationServiceController.java index a04c6136991..4ef98ea8f78 100644 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/controller/OauthAuthorizationServiceController.java +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/controller/OauthAuthorizationServiceController.java @@ -1,67 +1,53 @@ -package no.nav.testnav.mocks.tokendingsmock.controller; +package no.nav.testnav.mocks.tokendings.controller; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import reactor.core.publisher.Mono; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import no.nav.testnav.libs.securitycore.domain.AccessToken; -import no.nav.testnav.mocks.tokendingsmock.domain.Arguments; -import no.nav.testnav.mocks.tokendingsmock.service.JwtService; +import no.nav.testnav.mocks.tokendings.domain.Arguments; +import no.nav.testnav.mocks.tokendings.service.JwtService; + +import static no.nav.testnav.mocks.tokendings.TokendingsMockApplicationStarter.Utils.loadJson; @Slf4j @RestController @RequiredArgsConstructor public class OauthAuthorizationServiceController { - private static final String jwks; - private static final String wellknown; + private static final String JWKS; + private static final String WELL_KNOWN; static { - jwks = loadJson("static/jwks.json"); - wellknown = loadJson("static/well-known.json"); + JWKS = loadJson("static/jwks.json"); + WELL_KNOWN = loadJson("static/well-known.json"); } - private final JwtService jwtService; - - private static String loadJson(String path) { - var resource = new ClassPathResource(path); - try (final InputStreamReader stream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { - return new BufferedReader(stream) - .lines().collect(Collectors.joining("\n")); - - } catch (IOException e) { - throw new RuntimeException("Feil med paring av " + path + ".", e); - } - } + private final JwtService service; @GetMapping(value = "/jwks", produces = MediaType.APPLICATION_JSON_VALUE) - public Mono getJwks() { - return Mono.just(jwks); + public Mono getJWKS() { + return Mono.just(JWKS); } @GetMapping(value = "/.well-known/oauth-authorization-server", produces = MediaType.APPLICATION_JSON_VALUE) public Mono getWellKnown() { - return Mono.just(wellknown); + return Mono.just(WELL_KNOWN); } @PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public Mono createToken(Arguments arguments) { var excludedClaims = Set.of("aud", "nbf", "iss", "exp", "iat", "jtl"); - var verify = jwtService.verify(arguments.getSubject_token()); + var verify = service.verify(arguments.getSubjectToken()); var claims = verify .getClaims() .entrySet() @@ -69,7 +55,7 @@ public Mono createToken(Arguments arguments) { .filter(set -> !excludedClaims.contains(set.getKey())) .map(entry -> Map.entry(entry.getKey(), entry.getValue().asString())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - return Mono.just(new AccessToken(jwtService.jwtWith(claims, arguments.getAudience()))); + return Mono.just(new AccessToken(service.jwtWith(claims, arguments.getAudience()))); } } diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/domain/Arguments.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/domain/Arguments.java new file mode 100644 index 00000000000..28d83550d46 --- /dev/null +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/domain/Arguments.java @@ -0,0 +1,16 @@ +package no.nav.testnav.mocks.tokendings.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Arguments { + @JsonProperty("audience") + private String audience; + @JsonProperty("subject_token") + private String subjectToken; + @JsonProperty("pid") + private String pid; +} diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/service/JwtService.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/service/JwtService.java similarity index 53% rename from mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/service/JwtService.java rename to mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/service/JwtService.java index 2394e15b317..47229f569ad 100644 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/service/JwtService.java +++ b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendings/service/JwtService.java @@ -1,4 +1,4 @@ -package no.nav.testnav.mocks.tokendingsmock.service; +package no.nav.testnav.mocks.tokendings.service; import com.auth0.jwt.JWT; @@ -7,48 +7,33 @@ import com.nimbusds.jose.jwk.RSAKey; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; -import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.security.interfaces.RSAPrivateKey; import java.util.Calendar; import java.util.Date; import java.util.Map; import java.util.UUID; -import java.util.stream.Collectors; + +import static no.nav.testnav.mocks.tokendings.TokendingsMockApplicationStarter.Utils.loadJson; @Service @RequiredArgsConstructor public class JwtService { - private static final String jwtSecret; - - static { - jwtSecret = loadJson("static/jwk.json"); - } - private static String loadJson(String path) { - var resource = new ClassPathResource(path); - try (final InputStreamReader stream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) { - return new BufferedReader(stream) - .lines().collect(Collectors.joining("\n")); + private static final String JWK; - } catch (IOException e) { - throw new RuntimeException("Feil med paring av " + path + ".", e); - } + static { + JWK = loadJson("static/jwk.json"); } - @SneakyThrows public DecodedJWT verify(String jwt) { - var key = RSAKey.parse(jwtSecret); - var verifier = JWT + var key = RSAKey.parse(JWK); + return JWT .require(Algorithm.RSA256(key.toRSAPublicKey(), (RSAPrivateKey) key.toPrivateKey())) - .build(); - return verifier.verify(jwt); + .build() + .verify(jwt); } @@ -64,11 +49,11 @@ public String jwtWith(Map claims, String audience) { .withJWTId(UUID.randomUUID().toString()) .withExpiresAt(new Date(date.getTimeInMillis() + (2 * 60 * 60 * 1000))); claims.forEach(builder::withClaim); - - var privateKey = RSAKey.parse(jwtSecret).toPrivateKey(); - + var privateKey = (RSAPrivateKey) RSAKey + .parse(JWK) + .toPrivateKey(); return builder - .sign(Algorithm.RSA256(null, (RSAPrivateKey) privateKey)); + .sign(Algorithm.RSA256(null, privateKey)); } } diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/TokendingsMockApplicationStarter.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/TokendingsMockApplicationStarter.java deleted file mode 100644 index 3d844aca42f..00000000000 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/TokendingsMockApplicationStarter.java +++ /dev/null @@ -1,19 +0,0 @@ -package no.nav.testnav.mocks.tokendingsmock; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Import; -import org.springframework.web.reactive.config.EnableWebFlux; - -import no.nav.testnav.libs.reactivecore.config.CoreConfig; - -@Import({ - CoreConfig.class, -}) -@EnableWebFlux -@SpringBootApplication -public class TokendingsMockApplicationStarter { - public static void main(String[] args) { - SpringApplication.run(TokendingsMockApplicationStarter.class, args); - } -} diff --git a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java b/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java deleted file mode 100644 index 36bfe3d6ab9..00000000000 --- a/mocks/tokendings-mock/src/main/java/no/nav/testnav/mocks/tokendingsmock/domain/Arguments.java +++ /dev/null @@ -1,12 +0,0 @@ -package no.nav.testnav.mocks.tokendingsmock.domain; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class Arguments { - private String audience; - private String subject_token; - private String pid; -}