You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by jo...@apache.org on 2022/05/09 17:58:55 UTC

[nifi] 13/16: NIFI-9984 Allow 200-series responses in OAuth2 Access Token Provider

This is an automated email from the ASF dual-hosted git repository.

joewitt pushed a commit to branch support/nifi-1.16
in repository https://gitbox.apache.org/repos/asf/nifi.git

commit 237b0abbf3d3e558cd8a0b889a0b206271258983
Author: exceptionfactory <ex...@apache.org>
AuthorDate: Wed May 4 11:13:57 2022 -0500

    NIFI-9984 Allow 200-series responses in OAuth2 Access Token Provider
    
    This closes #6016
    
    Signed-off-by: Mike Thomsen <mt...@apache.org>
---
 .../nifi/oauth2/StandardOauth2AccessTokenProvider.java  | 17 ++++++++---------
 .../oauth2/StandardOauth2AccessTokenProviderTest.java   | 15 +++++++++------
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java b/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java
index c09ecc9e96..af3a96dd68 100644
--- a/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java
+++ b/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/main/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProvider.java
@@ -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);
         }
     }
diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/test/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProviderTest.java b/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/test/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProviderTest.java
index cbc485aaf6..20054bcad3 100644
--- a/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/test/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProviderTest.java
+++ b/nifi-nar-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/test/java/org/apache/nifi/oauth2/StandardOauth2AccessTokenProviderTest.java
@@ -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\" }"
         );
     }