NIFI-9984 Allow 200-series responses in OAuth2 Access Token Provider

This closes #6016

Signed-off-by: Mike Thomsen <mthomsen@apache.org>
This commit is contained in:
exceptionfactory 2022-05-04 11:13:57 -05:00 committed by Mike Thomsen
parent 272325cb4e
commit 64f9b66141
No known key found for this signature in database
GPG Key ID: 88511C3D4CAD246F
2 changed files with 17 additions and 15 deletions

View File

@ -315,19 +315,18 @@ public class StandardOauth2AccessTokenProvider extends AbstractControllerService
this.accessDetails = getAccessDetails(refreshRequest);
}
private AccessToken getAccessDetails(Request newRequest) {
private AccessToken getAccessDetails(final Request newRequest) {
try {
Response response = httpClient.newCall(newRequest).execute();
String responseBody = response.body().string();
if (response.code() != 200) {
final Response response = httpClient.newCall(newRequest).execute();
final String responseBody = response.body().string();
if (response.isSuccessful()) {
getLogger().debug("OAuth2 Access Token retrieved [HTTP {}]", response.code());
return ACCESS_DETAILS_MAPPER.readValue(responseBody, AccessToken.class);
} else {
getLogger().error(String.format("OAuth2 access token request failed [HTTP %d], response:%n%s", response.code(), responseBody));
throw new ProcessException(String.format("OAuth2 access token request failed [HTTP %d]", response.code()));
}
AccessToken accessDetails = ACCESS_DETAILS_MAPPER.readValue(responseBody, AccessToken.class);
return accessDetails;
} catch (IOException e) {
} catch (final IOException e) {
throw new UncheckedIOException("OAuth2 access token request failed", e);
}
}

View File

@ -64,6 +64,9 @@ public class StandardOauth2AccessTokenProviderTest {
private static final String CLIENT_SECRET = "clientSecret";
private static final long FIVE_MINUTES = 300;
private static final int HTTP_OK = 200;
private static final int HTTP_ACCEPTED = 201;
private StandardOauth2AccessTokenProvider testSubject;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@ -146,7 +149,7 @@ public class StandardOauth2AccessTokenProviderTest {
// GIVEN
Response response = buildResponse(
200,
HTTP_OK,
"{ \"access_token\":\"" + accessTokenValue + "\" }"
);
@ -166,12 +169,12 @@ public class StandardOauth2AccessTokenProviderTest {
String expectedToken = "second_token";
Response response1 = buildResponse(
200,
HTTP_OK,
"{ \"access_token\":\"" + firstToken + "\", \"expires_in\":\"0\", \"refresh_token\":\"not_checking_in_this_test\" }"
);
Response response2 = buildResponse(
200,
HTTP_OK,
"{ \"access_token\":\"" + expectedToken + "\" }"
);
@ -230,7 +233,7 @@ public class StandardOauth2AccessTokenProviderTest {
String expectedToken = "expected_token";
Response successfulAcquireResponse = buildResponse(
200,
HTTP_ACCEPTED,
"{ \"access_token\":\"" + expectedToken + "\", \"expires_in\":\"0\", \"refresh_token\":\"not_checking_in_this_test\" }"
);
@ -319,7 +322,7 @@ public class StandardOauth2AccessTokenProviderTest {
Response errorRefreshResponse = buildResponse(500, expectedRefreshErrorResponse);
Response successfulAcquireResponse = buildResponse(
200,
HTTP_OK,
"{ \"access_token\":\"" + expectedToken + "\", \"expires_in\":\"0\", \"refresh_token\":\"not_checking_in_this_test\" }"
);
@ -358,7 +361,7 @@ public class StandardOauth2AccessTokenProviderTest {
private Response buildSuccessfulInitResponse() {
return buildResponse(
200,
HTTP_OK,
"{ \"access_token\":\"exists_but_value_irrelevant\", \"expires_in\":\"0\", \"refresh_token\":\"not_checking_in_this_test\" }"
);
}