Search in sources :

Example 1 with HttpClient

use of io.micronaut.http.client.HttpClient in project micronaut-security by micronaut-projects.

the class OpenIdClientFactory method openIdConfiguration.

/**
 * Retrieves OpenID configuration from the provided issuer.
 *
 * @param oauthClientConfiguration The client configuration
 * @param openIdClientConfiguration The openid client configuration
 * @param issuerClient The client to request the metadata
 * @return The OpenID configuration
 */
@EachBean(OpenIdClientConfiguration.class)
DefaultOpenIdProviderMetadata openIdConfiguration(@Parameter OauthClientConfiguration oauthClientConfiguration, @Parameter OpenIdClientConfiguration openIdClientConfiguration, @Client HttpClient issuerClient) {
    DefaultOpenIdProviderMetadata providerMetadata = openIdClientConfiguration.getIssuer().map(issuer -> {
        try {
            URL configurationUrl = new URL(issuer, StringUtils.prependUri(issuer.getPath(), openIdClientConfiguration.getConfigurationPath()));
            if (LOG.isDebugEnabled()) {
                LOG.debug("Sending request for OpenID configuration for provider [{}] to URL [{}]", openIdClientConfiguration.getName(), configurationUrl);
            }
            // TODO NOSONAR this returns ReadTimeoutException - return issuerClient.toBlocking().retrieve(configurationUrl.toString(), DefaultOpenIdProviderMetadata.class);
            String json = issuerClient.toBlocking().retrieve(configurationUrl.toString(), String.class);
            return jsonMapper.readValue(json.getBytes(StandardCharsets.UTF_8), Argument.of(DefaultOpenIdProviderMetadata.class));
        } catch (HttpClientResponseException e) {
            throw new BeanInstantiationException("Failed to retrieve OpenID configuration for " + openIdClientConfiguration.getName(), e);
        } catch (MalformedURLException e) {
            throw new BeanInstantiationException("Failure parsing issuer URL " + issuer.toString(), e);
        } catch (IOException e) {
            throw new BeanInstantiationException("JSON Processing Exception parsing issuer URL returned JSON " + issuer.toString(), e);
        }
    }).orElse(new DefaultOpenIdProviderMetadata());
    overrideFromConfig(providerMetadata, openIdClientConfiguration, oauthClientConfiguration);
    return providerMetadata;
}
Also used : Parameter(io.micronaut.context.annotation.Parameter) EndSessionEndpointResolver(io.micronaut.security.oauth2.endpoint.endsession.request.EndSessionEndpointResolver) BeanContext(io.micronaut.context.BeanContext) URL(java.net.URL) JacksonDatabindMapper(io.micronaut.jackson.databind.JacksonDatabindMapper) LoggerFactory(org.slf4j.LoggerFactory) EndSessionCallbackUrlBuilder(io.micronaut.security.oauth2.endpoint.endsession.response.EndSessionCallbackUrlBuilder) Client(io.micronaut.http.client.annotation.Client) Internal(io.micronaut.core.annotation.Internal) Supplier(java.util.function.Supplier) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) EachBean(io.micronaut.context.annotation.EachBean) Nullable(io.micronaut.core.annotation.Nullable) JsonMapper(io.micronaut.json.JsonMapper) Requires(io.micronaut.context.annotation.Requires) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) Argument(io.micronaut.core.type.Argument) SupplierUtil(io.micronaut.core.util.SupplierUtil) HttpClient(io.micronaut.http.client.HttpClient) EndSessionEndpoint(io.micronaut.security.oauth2.endpoint.endsession.request.EndSessionEndpoint) OpenIdClientConfiguration(io.micronaut.security.oauth2.configuration.OpenIdClientConfiguration) OpenIdClientCondition(io.micronaut.security.oauth2.client.condition.OpenIdClientCondition) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) AuthorizationRedirectHandler(io.micronaut.security.oauth2.endpoint.authorization.request.AuthorizationRedirectHandler) OauthClientConfiguration(io.micronaut.security.oauth2.configuration.OauthClientConfiguration) StandardCharsets(java.nio.charset.StandardCharsets) StringUtils(io.micronaut.core.util.StringUtils) EndSessionEndpointConfiguration(io.micronaut.security.oauth2.configuration.endpoints.EndSessionEndpointConfiguration) Factory(io.micronaut.context.annotation.Factory) EndpointConfiguration(io.micronaut.security.oauth2.configuration.endpoints.EndpointConfiguration) BeanProvider(io.micronaut.context.BeanProvider) Inject(jakarta.inject.Inject) OpenIdAuthenticationMapper(io.micronaut.security.oauth2.endpoint.token.response.OpenIdAuthenticationMapper) Collections(java.util.Collections) OpenIdAuthorizationResponseHandler(io.micronaut.security.oauth2.endpoint.authorization.response.OpenIdAuthorizationResponseHandler) MalformedURLException(java.net.MalformedURLException) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) IOException(java.io.IOException) URL(java.net.URL) EachBean(io.micronaut.context.annotation.EachBean)

Example 2 with HttpClient

use of io.micronaut.http.client.HttpClient in project micronaut-views by micronaut-projects.

the class SecurityViewModelProcessorTest method aCustomSecurityPropertyNameCanBeInjectedToTheModel.

@Test
void aCustomSecurityPropertyNameCanBeInjectedToTheModel() {
    // given:
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "SecurityViewModelProcessorSpec", "micronaut.views.soy.enabled", StringUtils.FALSE, "micronaut.security.views-model-decorator.security-key", "securitycustom"));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(BooksController.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(MockAuthenticationProvider.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(SecurityViewModelProcessor.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/").basicAuth("john", "secret");
    HttpResponse<String> response = httpClient.toBlocking().exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    // and:
    assertFalse(html.contains("User: john"));
    // and:
    assertTrue(html.contains("Custom: john"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : SecurityViewModelProcessor(io.micronaut.views.model.security.SecurityViewModelProcessor) MockAuthenticationProvider(io.micronaut.views.model.security.MockAuthenticationProvider) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) BooksController(io.micronaut.views.model.security.BooksController) Test(org.junit.jupiter.api.Test)

Example 3 with HttpClient

use of io.micronaut.http.client.HttpClient in project micronaut-views by micronaut-projects.

the class UnmodifiableModelAndViewTest method aModifiableViewModelStillAddsSecurity.

@Test
void aModifiableViewModelStillAddsSecurity() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "UnmodifiableModelAndViewSpec", "micronaut.views.soy.enabled", StringUtils.FALSE));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // given:
    BlockingHttpClient client = httpClient.toBlocking();
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(UnmodifiableFruitsController.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/modifiable").basicAuth("john", "secret");
    HttpResponse<String> response = client.exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    // and:
    assertTrue(html.contains("<blink>Security was added</blink>"));
    // and:
    assertTrue(html.contains("<h1>fruit: plum</h1>"));
    // and:
    assertTrue(html.contains("<h1>color: plum</h1>"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : UnmodifiableFruitsController(io.micronaut.views.model.UnmodifiableFruitsController) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 4 with HttpClient

use of io.micronaut.http.client.HttpClient in project micronaut-views by micronaut-projects.

the class UnmodifiableModelAndViewTest method anUnmodifiableViewModelReportsErrorButDoesNotCrash.

@Test
void anUnmodifiableViewModelReportsErrorButDoesNotCrash() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "UnmodifiableModelAndViewSpec", "micronaut.views.soy.enabled", StringUtils.FALSE));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // given:
    BlockingHttpClient client = httpClient.toBlocking();
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(UnmodifiableFruitsController.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/unmodifiable").basicAuth("john", "secret");
    HttpResponse<String> response = client.exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    // and:
    assertTrue(html.contains("<blink>Security was added</blink>"));
    // and:
    assertTrue(html.contains("<h1>fruit: plum</h1>"));
    // and:
    assertTrue(html.contains("<h1>color: plum</h1>"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : UnmodifiableFruitsController(io.micronaut.views.model.UnmodifiableFruitsController) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) BlockingHttpClient(io.micronaut.http.client.BlockingHttpClient) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) MicronautTest(io.micronaut.test.extensions.junit5.annotation.MicronautTest) Test(org.junit.jupiter.api.Test)

Example 5 with HttpClient

use of io.micronaut.http.client.HttpClient in project micronaut-views by micronaut-projects.

the class SecurityViewModelProcessorTest method securityPropertyIsInjectedToTheModel.

@Test
void securityPropertyIsInjectedToTheModel() {
    // given:
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf("spec.name", "SecurityViewModelProcessorSpec", "micronaut.views.soy.enabled", StringUtils.FALSE));
    HttpClient httpClient = HttpClient.create(embeddedServer.getURL());
    // expect:
    assertTrue(embeddedServer.getApplicationContext().containsBean(BooksController.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(MockAuthenticationProvider.class));
    // and:
    assertTrue(embeddedServer.getApplicationContext().containsBean(SecurityViewModelProcessor.class));
    // when:
    HttpRequest<?> request = HttpRequest.GET("/").basicAuth("john", "secret");
    HttpResponse<String> response = httpClient.toBlocking().exchange(request, String.class);
    // then:
    assertEquals(HttpStatus.OK, response.status());
    // when:
    String html = response.body();
    // then:
    assertNotNull(html);
    assertTrue(html.contains("User: john email: john@email.com"));
    // and:
    assertTrue(html.contains("Developing Microservices"));
    // cleanup:
    httpClient.close();
    // and:
    embeddedServer.close();
}
Also used : SecurityViewModelProcessor(io.micronaut.views.model.security.SecurityViewModelProcessor) MockAuthenticationProvider(io.micronaut.views.model.security.MockAuthenticationProvider) HttpClient(io.micronaut.http.client.HttpClient) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) BooksController(io.micronaut.views.model.security.BooksController) Test(org.junit.jupiter.api.Test)

Aggregations

HttpClient (io.micronaut.http.client.HttpClient)5 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)4 Test (org.junit.jupiter.api.Test)4 BlockingHttpClient (io.micronaut.http.client.BlockingHttpClient)2 MicronautTest (io.micronaut.test.extensions.junit5.annotation.MicronautTest)2 UnmodifiableFruitsController (io.micronaut.views.model.UnmodifiableFruitsController)2 BooksController (io.micronaut.views.model.security.BooksController)2 MockAuthenticationProvider (io.micronaut.views.model.security.MockAuthenticationProvider)2 SecurityViewModelProcessor (io.micronaut.views.model.security.SecurityViewModelProcessor)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BeanContext (io.micronaut.context.BeanContext)1 BeanProvider (io.micronaut.context.BeanProvider)1 EachBean (io.micronaut.context.annotation.EachBean)1 Factory (io.micronaut.context.annotation.Factory)1 Parameter (io.micronaut.context.annotation.Parameter)1 Requires (io.micronaut.context.annotation.Requires)1 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)1 Internal (io.micronaut.core.annotation.Internal)1 Nullable (io.micronaut.core.annotation.Nullable)1 Argument (io.micronaut.core.type.Argument)1