use of io.micronaut.http.client.exceptions.HttpClientResponseException 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;
}
use of io.micronaut.http.client.exceptions.HttpClientResponseException in project micronaut-security by micronaut-projects.
the class LoggersTest method loggersEndpointIsSecured.
@Test
void loggersEndpointIsSecured() {
BlockingHttpClient client = httpClient.toBlocking();
Executable e = () -> client.exchange(HttpRequest.GET("/loggers"));
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, e);
assertEquals(HttpStatus.UNAUTHORIZED, thrown.getStatus());
}
use of io.micronaut.http.client.exceptions.HttpClientResponseException in project micronaut-security by micronaut-projects.
the class LoggersTest method loggersEndpointIsNotAccessibleForUsersWithoutRoleROLE_SYSTEM.
@Test
void loggersEndpointIsNotAccessibleForUsersWithoutRoleROLE_SYSTEM() {
BlockingHttpClient client = httpClient.toBlocking();
Executable e = () -> client.exchange(HttpRequest.GET("/loggers").basicAuth("user", "password"));
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, e);
assertEquals(HttpStatus.FORBIDDEN, thrown.getStatus());
}
use of io.micronaut.http.client.exceptions.HttpClientResponseException in project micronaut-security by micronaut-projects.
the class SensitiveEndpointRuleReplacementTest method testAccessingASensitiveEndpointWithAuthenticationAndASensitiveEndpointRuleReplacementWorks.
@Test
void testAccessingASensitiveEndpointWithAuthenticationAndASensitiveEndpointRuleReplacementWorks() {
BlockingHttpClient client = httpClient.toBlocking();
Executable e = () -> client.exchange(HttpRequest.GET("/beans"));
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, e);
assertEquals(HttpStatus.UNAUTHORIZED, thrown.getStatus());
e = () -> client.exchange(HttpRequest.GET("/beans").basicAuth("user", "password"));
assertDoesNotThrow(e);
}
use of io.micronaut.http.client.exceptions.HttpClientResponseException in project micronaut-views by micronaut-projects.
the class ModelAndViewTest method returningANullModelCausesA404.
@Test
void returningANullModelCausesA404() {
// given:
BlockingHttpClient client = httpClient.toBlocking();
// expect:
assertTrue(beanContext.containsBean(FruitsController.class));
// when:
Executable e = () -> client.exchange(HttpRequest.GET("/null"), String.class);
// then:
HttpClientResponseException thrown = assertThrows(HttpClientResponseException.class, e);
}
Aggregations