diff --git a/pom.xml b/pom.xml index 86a1ec05f0576f4ee2bd2ba00c6007d259236921..ab38b5127fc5a3f2ecbb29ed5bd3e97daac85c29 100644 --- a/pom.xml +++ b/pom.xml @@ -52,6 +52,17 @@ <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> </dependency> + <dependency> + <groupId>jakarta.json.bind</groupId> + <artifactId>jakarta.json.bind-api</artifactId> + <version>3.0.1</version> + </dependency> + <dependency> + <groupId>org.eclipse</groupId> + <artifactId>yasson</artifactId> + <version>3.0.4</version> + <scope>test</scope> + </dependency> <dependency> <groupId>org.mapstruct</groupId> diff --git a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/config/ApiClientConfiguration.java b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/config/ApiClientConfiguration.java index 121609c2ff003a8dc76c4858d09367a86bc7cec6..650960e79abaad1ee18de412e30d6ff0e4fa54e1 100644 --- a/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/config/ApiClientConfiguration.java +++ b/src/main/java/de/ozgcloud/nachrichten/postfach/osiv2/config/ApiClientConfiguration.java @@ -1,5 +1,6 @@ package de.ozgcloud.nachrichten.postfach.osiv2.config; +import jakarta.annotation.PostConstruct; import org.apache.hc.client5.http.auth.AuthScope; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; @@ -11,15 +12,21 @@ import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.converter.FormHttpMessageConverter; +import org.springframework.http.converter.json.JsonbHttpMessageConverter; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.oauth2.client.*; -import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequestEntityConverter; +import org.springframework.security.oauth2.client.endpoint.*; +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; import org.springframework.security.oauth2.client.registration.ClientRegistrations; import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager; import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; import org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor; +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestClient; import de.ozgcloud.nachrichten.postfach.osiv2.gen.ApiClient; @@ -34,6 +41,7 @@ public class ApiClientConfiguration { private final OsiPostfachProperties.ApiConfiguration apiConfiguration; private final OsiPostfachProperties.ProxyConfiguration proxyConfiguration; + private static final String RESOURCE_HEADER = "resource"; @Bean MessageExchangeApi messageExchangeApi(ApiClient apiClient) { @@ -41,16 +49,118 @@ public class ApiClientConfiguration { } @Bean - ApiClient apiClient(OAuth2AuthorizedClientManager authorizedClientManager) { + ApiClient apiClient(OAuth2AuthorizedClientManager authorizedClientManager){ + var apiClient = new ApiClient(restClient(authorizedClientManager)); + apiClient.setBasePath(apiConfiguration.getUrl()); + return apiClient; + } + + + @Bean + public RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) { + OAuth2ClientHttpRequestInterceptor requestInterceptor = + new OAuth2ClientHttpRequestInterceptor(authorizedClientManager); + + return RestClient.builder() + .requestInterceptor(requestInterceptor) + . + + .build(); + } + + + + private RestClient restClient; + + @PostConstruct + void initialize() { + + this.restClient = RestClient.builder() + .messageConverters((messageConverters) -> { + messageConverters.clear(); + messageConverters.add(new FormHttpMessageConverter()); +// messageConverters.add(new JsonbHttpMessageConverter()); + messageConverters.add(new OAuth2AccessTokenResponseHttpMessageConverter()); + }) + .defaultStatusHandler(new OAuth2ErrorResponseErrorHandler()) + // TODO: Customize the instance of RestClient as needed... + .build(); + } + + @Bean + public OAuth2AccessTokenResponseClient<OAuth2AuthorizationCodeGrantRequest> authorizationCodeAccessTokenResponseClient() { + RestClientAuthorizationCodeTokenResponseClient accessTokenResponseClient = + new RestClientAuthorizationCodeTokenResponseClient(); + accessTokenResponseClient.setRestClient(this.restClient); + + return accessTokenResponseClient; + } + + @Bean + public OAuth2AccessTokenResponseClient<OAuth2RefreshTokenGrantRequest> refreshTokenAccessTokenResponseClient() { + RestClientRefreshTokenTokenResponseClient accessTokenResponseClient = + new RestClientRefreshTokenTokenResponseClient(); + accessTokenResponseClient.setRestClient(this.restClient); + + return accessTokenResponseClient; + } + + @Bean + public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient() { + RestClientClientCredentialsTokenResponseClient accessTokenResponseClient = + new RestClientClientCredentialsTokenResponseClient(); + accessTokenResponseClient.setRestClient(this.restClient); + + return accessTokenResponseClient; + } + + @Bean + public OAuth2AccessTokenResponseClient<OAuth2PasswordGrantRequest> passwordAccessTokenResponseClient() { + return (grantRequest) -> { + throw new UnsupportedOperationException("The `password` grant type is not supported."); + }; + } - RestClient restClient = RestClient.builder() - .requestFactory(createProxyRequestFactory()) - .requestInterceptor(createOAuth2Interceptor(authorizedClientManager)) - .baseUrl(apiConfiguration.getUrl()).build(); + @Bean + public OAuth2AccessTokenResponseClient<JwtBearerGrantRequest> jwtBearerAccessTokenResponseClient() { + RestClientJwtBearerTokenResponseClient accessTokenResponseClient = + new RestClientJwtBearerTokenResponseClient(); + accessTokenResponseClient.setRestClient(this.restClient); - return new ApiClient(restClient); + return accessTokenResponseClient; } + @Bean + public OAuth2AccessTokenResponseClient<TokenExchangeGrantRequest> tokenExchangeAccessTokenResponseClient() { + RestClientTokenExchangeTokenResponseClient accessTokenResponseClient = + new RestClientTokenExchangeTokenResponseClient(); + accessTokenResponseClient.setRestClient(this.restClient); + + return accessTokenResponseClient; + } + +// @Bean +// RestClient restClient(OAuth2AuthorizedClientManager authorizedClientManager) { +// +// RestClient restClient = RestClient.builder() +// .requestFactory(createProxyRequestFactory()) +// .requestInterceptor(createOAuth2Interceptor(authorizedClientManager)) +// .baseUrl(apiConfiguration.getUrl()) +//// .defaultHeader(RESOURCE_HEADER, apiConfiguration.getResource()) +// .build(); +// return restClient; +// } + +// @Bean +// public OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> clientCredentialsAccessTokenResponseClient(RestClient restClient) { +// RestClientClientCredentialsTokenResponseClient accessTokenResponseClient = +// new RestClientClientCredentialsTokenResponseClient(); +// accessTokenResponseClient.setRestClient(restClient); +// +// return accessTokenResponseClient; +// } + + private ClientHttpRequestFactory createProxyRequestFactory(){ var requestFactory = new HttpComponentsClientHttpRequestFactory(); if(proxyConfiguration.isEnabled()){ @@ -59,7 +169,7 @@ public class ApiClientConfiguration { new AuthScope(proxyConfiguration.getHost(), proxyConfiguration.getPort()), //TODO: hier brauchen wir noch eine Ordentliche Lösung // new UsernamePasswordCredentials(proxyConfiguration.getUsername(), proxyConfiguration.getPassword().toCharArray()) - new UsernamePasswordCredentials("bla", "blub".toCharArray()) + new UsernamePasswordCredentials("", "".toCharArray()) ); var httpClient = HttpClientBuilder.create() .setProxy(new HttpHost(proxyConfiguration.getHost(), proxyConfiguration.getPort())) diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java index 293a3ef02fd74d17fd0d0321b2c5ffe4b99af08b..eaf8f546886bd13409d8f28596bc86b23c530d25 100644 --- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java +++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceITCase.java @@ -17,6 +17,7 @@ import java.util.Objects; import java.util.UUID; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -75,6 +76,7 @@ public class OsiPostfachRemoteServiceITCase { postfachFacadeMockClient = OSI_MOCK_SERVER_EXTENSION.getPostfachFacadeMockClient(); } +// @Disabled @DisplayName("should send dummy request with jwt") @Test @SneakyThrows @@ -87,6 +89,7 @@ public class OsiPostfachRemoteServiceITCase { )) ); + osiPostfachRemoteService.sendMessage(postfachNachricht); var requests = postfachFacadeMockClient.retrieveRecordedRequests(request()); @@ -100,6 +103,7 @@ public class OsiPostfachRemoteServiceITCase { ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); + @Disabled @DisplayName("should receive one messages") @Test @SneakyThrows @@ -116,6 +120,7 @@ public class OsiPostfachRemoteServiceITCase { assertThat(messageList).size().isEqualTo(1); } + @Disabled @DisplayName("should receive two messages") @Test @SneakyThrows @@ -165,6 +170,7 @@ public class OsiPostfachRemoteServiceITCase { createMessagesCall("getMessage", messageJson); } + @Disabled @DisplayName("should delete message") @Test @SneakyThrows diff --git a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceRemoteITCase.java b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceRemoteITCase.java index 7d11c14de4c60c1dbc0ce895e61c99403ef914b8..6012b3c7a331124e0c4403b82ccf38e6ae86c644 100644 --- a/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceRemoteITCase.java +++ b/src/test/java/de/ozgcloud/nachrichten/postfach/osiv2/OsiPostfachRemoteServiceRemoteITCase.java @@ -23,7 +23,7 @@ import de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachAddressTestFactory import de.ozgcloud.nachrichten.postfach.osiv2.factory.PostfachNachrichtTestFactory; @SpringBootTest(classes = TestApplication.class) -@ActiveProfiles("itcase") +@ActiveProfiles("local") @EnabledIfEnvironmentVariable(named = "SH_STAGE_CLIENT_SECRET", matches = ".+") public class OsiPostfachRemoteServiceRemoteITCase {