HADOOP-12481. JWTRedirectAuthenticationHandler doesn't Retain Original Query 
String. Contributed by Larry McCay.


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/a121fa1d
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/a121fa1d
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/a121fa1d

Branch: refs/heads/HDFS-7966
Commit: a121fa1d39b2eb129bcc0e786d0d24c9ec0cdefc
Parents: fdd7406
Author: cnauroth <[email protected]>
Authored: Thu Oct 15 16:44:59 2015 -0700
Committer: cnauroth <[email protected]>
Committed: Thu Oct 15 16:44:59 2015 -0700

----------------------------------------------------------------------
 .../JWTRedirectAuthenticationHandler.java       |  7 +++-
 .../TestJWTRedirectAuthentictionHandler.java    | 42 +++++++++++++++++++-
 hadoop-common-project/hadoop-common/CHANGES.txt |  3 ++
 3 files changed, 50 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/a121fa1d/hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/server/JWTRedirectAuthenticationHandler.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/server/JWTRedirectAuthenticationHandler.java
 
b/hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/server/JWTRedirectAuthenticationHandler.java
index abbf379..cbe923b 100644
--- 
a/hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/server/JWTRedirectAuthenticationHandler.java
+++ 
b/hadoop-common-project/hadoop-auth/src/main/java/org/apache/hadoop/security/authentication/server/JWTRedirectAuthenticationHandler.java
@@ -233,10 +233,15 @@ public class JWTRedirectAuthenticationHandler extends
     }
     String loginURL = authenticationProviderUrl + delimiter
         + ORIGINAL_URL_QUERY_PARAM
-        + request.getRequestURL().toString();
+        + request.getRequestURL().toString() + getOriginalQueryString(request);
     return loginURL;
   }
 
+  private String getOriginalQueryString(HttpServletRequest request) {
+    String originalQueryString = request.getQueryString();
+    return (originalQueryString == null) ? "" : "?" + originalQueryString;
+  }
+
   /**
    * This method provides a single method for validating the JWT for use in
    * request processing. It provides for the override of specific aspects of

http://git-wip-us.apache.org/repos/asf/hadoop/blob/a121fa1d/hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestJWTRedirectAuthentictionHandler.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestJWTRedirectAuthentictionHandler.java
 
b/hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestJWTRedirectAuthentictionHandler.java
index 4ac9535..019ecb4 100644
--- 
a/hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestJWTRedirectAuthentictionHandler.java
+++ 
b/hadoop-common-project/hadoop-auth/src/test/java/org/apache/hadoop/security/authentication/server/TestJWTRedirectAuthentictionHandler.java
@@ -356,6 +356,40 @@ public class TestJWTRedirectAuthentictionHandler extends
     }
   }
 
+  @Test
+  public void testOrigURLWithQueryString() throws Exception {
+    handler.setPublicKey(publicKey);
+
+    Properties props = getProperties();
+    handler.init(props);
+
+    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+    Mockito.when(request.getRequestURL()).thenReturn(
+        new StringBuffer(SERVICE_URL));
+    Mockito.when(request.getQueryString()).thenReturn("name=value");
+
+    String loginURL = 
((TestJWTRedirectAuthenticationHandler)handler).testConstructLoginURL(request);
+    Assert.assertNotNull("loginURL should not be null.", loginURL);
+    Assert.assertEquals("https://localhost:8443/authserver?originalUrl="; + 
SERVICE_URL + "?name=value", loginURL);
+  }
+
+  @Test
+  public void testOrigURLNoQueryString() throws Exception {
+    handler.setPublicKey(publicKey);
+
+    Properties props = getProperties();
+    handler.init(props);
+
+    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+    Mockito.when(request.getRequestURL()).thenReturn(
+        new StringBuffer(SERVICE_URL));
+    Mockito.when(request.getQueryString()).thenReturn(null);
+
+    String loginURL = 
((TestJWTRedirectAuthenticationHandler)handler).testConstructLoginURL(request);
+    Assert.assertNotNull("LoginURL should not be null.", loginURL);
+    Assert.assertEquals("https://localhost:8443/authserver?originalUrl="; + 
SERVICE_URL, loginURL);
+  }
+
   @Before
   public void setup() throws Exception, NoSuchAlgorithmException {
     setupKerberosRequirements();
@@ -367,7 +401,7 @@ public class TestJWTRedirectAuthentictionHandler extends
     publicKey = (RSAPublicKey) kp.getPublic();
     privateKey = (RSAPrivateKey) kp.getPrivate();
 
-    handler = new JWTRedirectAuthenticationHandler();
+    handler = new TestJWTRedirectAuthenticationHandler();
   }
 
   protected void setupKerberosRequirements() throws Exception {
@@ -415,4 +449,10 @@ public class TestJWTRedirectAuthentictionHandler extends
 
     return signedJWT;
   }
+
+  class TestJWTRedirectAuthenticationHandler extends 
JWTRedirectAuthenticationHandler {
+    public String testConstructLoginURL(HttpServletRequest req) {
+      return constructLoginURL(req);
+    }
+  };
 }

http://git-wip-us.apache.org/repos/asf/hadoop/blob/a121fa1d/hadoop-common-project/hadoop-common/CHANGES.txt
----------------------------------------------------------------------
diff --git a/hadoop-common-project/hadoop-common/CHANGES.txt 
b/hadoop-common-project/hadoop-common/CHANGES.txt
index 4d0f8dc..2f2ce6b 100644
--- a/hadoop-common-project/hadoop-common/CHANGES.txt
+++ b/hadoop-common-project/hadoop-common/CHANGES.txt
@@ -891,6 +891,9 @@ Release 2.8.0 - UNRELEASED
     HADOOP-10775. Shell operations to fail with meaningful errors on windows if
     winutils.exe not found. (stevel)
 
+    HADOOP-12481. JWTRedirectAuthenticationHandler doesn't Retain Original 
Query
+    String (Larry McCay via cnauroth)
+
   OPTIMIZATIONS
 
     HADOOP-11785. Reduce the number of listStatus operation in distcp

Reply via email to