Skip to content

Commit

Permalink
fix: use a throwable in the Oauth2Response on technical error for int…
Browse files Browse the repository at this point in the history
  • Loading branch information
phiz71 committed Nov 23, 2023
1 parent 0e25f79 commit c13a525
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<gravitee-common.version>1.26.1</gravitee-common.version>
<gravitee-resource-api.version>1.1.0</gravitee-resource-api.version>
<gravitee-gateway-api.version>1.18.1</gravitee-gateway-api.version>
<gravitee-resource-oauth2-provider-api.version>1.3.0</gravitee-resource-oauth2-provider-api.version>
<gravitee-resource-oauth2-provider-api.version>1.4.0</gravitee-resource-oauth2-provider-api.version>
<gravitee-node.version>1.23.0</gravitee-node.version>
<maven-assembly-plugin.version>2.5.5</maven-assembly-plugin.version>
<!-- Property used by the publication job in CI-->
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/io/gravitee/resource/oauth2/am/OAuth2AMResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.gravitee.node.container.spring.SpringEnvironmentConfiguration;
import io.gravitee.resource.oauth2.am.configuration.OAuth2ResourceConfiguration;
import io.gravitee.resource.oauth2.api.OAuth2Resource;
import io.gravitee.resource.oauth2.api.OAuth2ResourceException;
import io.gravitee.resource.oauth2.api.OAuth2Response;
import io.gravitee.resource.oauth2.api.openid.UserInfoResponse;
import io.vertx.core.AsyncResult;
Expand Down Expand Up @@ -189,7 +190,7 @@ public void introspect(String accessToken, Handler<OAuth2Response> responseHandl
@Override
public void handle(Throwable event) {
logger.error("An error occurs while checking access token", event);
responseHandler.handle(new OAuth2Response(false, event.getMessage()));
responseHandler.handle(new OAuth2Response(event));
}
}
)
Expand All @@ -204,7 +205,7 @@ public void handle(HttpClientRequest request) {
public void handle(AsyncResult<HttpClientResponse> asyncResponse) {
if (asyncResponse.failed()) {
logger.error("An error occurs while checking access token", asyncResponse.cause());
responseHandler.handle(new OAuth2Response(false, asyncResponse.cause().getMessage()));
responseHandler.handle(new OAuth2Response(asyncResponse.cause()));
} else {
final HttpClientResponse response = asyncResponse.result();
logger.debug(
Expand All @@ -229,7 +230,16 @@ public void handle(AsyncResult<HttpClientResponse> asyncResponse) {
);
}
} else {
responseHandler.handle(new OAuth2Response(false, buffer.toString()));
logger.error(
"An error occurs while checking access token. Request ends with status {}: {}",
response.statusCode(),
buffer.toString()
);
responseHandler.handle(
new OAuth2Response(
new OAuth2ResourceException("An error occurs while checking access token")
)
);
}
});
}
Expand All @@ -241,7 +251,7 @@ public void handle(AsyncResult<HttpClientResponse> asyncResponse) {
@Override
public void handle(Throwable event) {
logger.error("An error occurs while checking access token", event);
responseHandler.handle(new OAuth2Response(false, event.getMessage()));
responseHandler.handle(new OAuth2Response(event));
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public void shouldNotValidateAccessToken() throws Exception {
accessToken,
oAuth2Response -> {
Assert.assertFalse(oAuth2Response.isSuccess());
Assert.assertEquals("An error occurs while checking access token", oAuth2Response.getPayload());
lock.countDown();
}
);
Expand Down Expand Up @@ -162,6 +163,31 @@ public void shouldNotValidateAccessToken_v2() throws Exception {
Assert.assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}

@Test
public void shouldNotValidateAccessToken_v2_not_200() throws Exception {
String accessToken = "xxxx-xxxx-xxxx-xxxx";
stubFor(post(urlEqualTo("/domain/oauth/introspect")).willReturn(aResponse().withStatus(401)));

final CountDownLatch lock = new CountDownLatch(1);

Mockito.when(configuration.getSecurityDomain()).thenReturn("domain");
Mockito.when(configuration.getVersion()).thenReturn(OAuth2ResourceConfiguration.Version.V2_X);
Mockito.when(configuration.getServerURL()).thenReturn("http://localhost:" + wireMockRule.port());

resource.doStart();

resource.introspect(
accessToken,
oAuth2Response -> {
Assert.assertFalse(oAuth2Response.isSuccess());
Assert.assertEquals("An error occurs while checking access token", oAuth2Response.getPayload());
lock.countDown();
}
);

Assert.assertEquals(true, lock.await(10000, TimeUnit.MILLISECONDS));
}

@Test
public void shouldGetUserInfo() throws Exception {
stubFor(
Expand Down

0 comments on commit c13a525

Please sign in to comment.