You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/09/04 08:20:04 UTC

[2/3] httpcomponents-core git commit: Auth server filter to pass URI authority and request URI to #authenticate and #generateChallenge methods

Auth server filter to pass URI authority and request URI to #authenticate and #generateChallenge methods


Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/59bff65e
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/59bff65e
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/59bff65e

Branch: refs/heads/master
Commit: 59bff65effd5d004cde9ff465ea53d97c8b6a5d2
Parents: 7f7f499
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Mon Sep 4 10:01:02 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Mon Sep 4 10:17:21 2017 +0200

----------------------------------------------------------------------
 .../testing/classic/ClassicAuthenticationTest.java   | 12 ++++++++++--
 .../core5/testing/nio/Http1AuthenticationTest.java   | 12 ++++++++++--
 .../http/examples/AsyncServerFilterExample.java      | 11 +++++++++--
 .../http/examples/ClassicServerFilterExample.java    | 11 +++++++++--
 .../io/support/AbstractHttpServerAuthFilter.java     | 15 ++++++++++-----
 .../nio/support/AbstractAsyncServerAuthFilter.java   | 15 ++++++++++-----
 6 files changed, 58 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
index 93e4fe5..2f9d9b3 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicAuthenticationTest.java
@@ -56,6 +56,7 @@ import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.http.protocol.HttpCoreContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.util.Timeout;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -111,12 +112,19 @@ public class ClassicAuthenticationTest {
 
                         @Override
                         protected boolean authenticate(
-                                final String challengeResponse, final HttpContext context) {
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return challengeResponse != null && challengeResponse.equals("let me pass");
                         }
 
                         @Override
-                        protected String generateChallenge(final String challengeResponse, final HttpContext context) {
+                        protected String generateChallenge(
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return "who goes there?";
                         }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
----------------------------------------------------------------------
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
index aa5a696..90e6b96 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1AuthenticationTest.java
@@ -59,6 +59,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
 import org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.reactor.ListenerEndpoint;
 import org.apache.hc.core5.testing.classic.LoggingConnPoolListener;
@@ -125,12 +126,19 @@ public class Http1AuthenticationTest {
 
                         @Override
                         protected boolean authenticate(
-                                final String challengeResponse, final HttpContext context) {
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return challengeResponse != null && challengeResponse.equals("let me pass");
                         }
 
                         @Override
-                        protected String generateChallenge(final String challengeResponse, final HttpContext context) {
+                        protected String generateChallenge(
+                                final String challengeResponse,
+                                final URIAuthority authority,
+                                final String requestUri,
+                                final HttpContext context) {
                             return "who goes there?";
                         }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
index cc07a66..1ee1218 100644
--- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
+++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java
@@ -56,6 +56,7 @@ import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
 import org.apache.hc.core5.http.nio.support.AbstractAsyncServerAuthFilter;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.reactor.IOReactorConfig;
 import org.apache.hc.core5.reactor.ListenerEndpoint;
 import org.apache.hc.core5.util.TimeValue;
@@ -91,13 +92,19 @@ public class AsyncServerFilterExample {
 
                     @Override
                     protected boolean authenticate(
-                            final String challengeResponse, final HttpContext context) {
+                            final String challengeResponse,
+                            final URIAuthority authority,
+                            final String requestUri,
+                            final HttpContext context) {
                         return "let me pass".equals(challengeResponse);
                     }
 
                     @Override
                     protected String generateChallenge(
-                            final String challengeResponse, final HttpContext context) {
+                            final String challengeResponse,
+                            final URIAuthority authority,
+                            final String requestUri,
+                            final HttpContext context) {
                         return "who goes there?";
                     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
index a147716..c94447a 100644
--- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
+++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicServerFilterExample.java
@@ -47,6 +47,7 @@ import org.apache.hc.core5.http.io.support.AbstractHttpServerAuthFilter;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
 import org.apache.hc.core5.http.protocol.HttpContext;
 import org.apache.hc.core5.io.ShutdownType;
+import org.apache.hc.core5.net.URIAuthority;
 import org.apache.hc.core5.util.TimeValue;
 
 /**
@@ -80,13 +81,19 @@ public class ClassicServerFilterExample {
 
                     @Override
                     protected boolean authenticate(
-                            final String challengeResponse, final HttpContext context) {
+                            final String challengeResponse,
+                            final URIAuthority authority,
+                            final String requestUri,
+                            final HttpContext context) {
                         return "let me pass".equals(challengeResponse);
                     }
 
                     @Override
                     protected String generateChallenge(
-                            final String challengeResponse, final HttpContext context) {
+                            final String challengeResponse,
+                            final URIAuthority authority,
+                            final String requestUri,
+                            final HttpContext context) {
                         return "who goes there?";
                     }
 

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
index 1024f94..a000c27 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/io/support/AbstractHttpServerAuthFilter.java
@@ -44,6 +44,7 @@ import org.apache.hc.core5.http.io.entity.EntityUtils;
 import org.apache.hc.core5.http.io.entity.StringEntity;
 import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.net.URIAuthority;
 
 /**
  * @since 5.0
@@ -57,11 +58,11 @@ public abstract class AbstractHttpServerAuthFilter<T> implements HttpFilterHandl
         this.respondImmediately = respondImmediately;
     }
 
-    protected abstract T parseChallengeResponse(String challenge, HttpContext context) throws HttpException;
+    protected abstract T parseChallengeResponse(String authorizationValue, HttpContext context) throws HttpException;
 
-    protected abstract boolean authenticate(T challengeResponse, HttpContext context);
+    protected abstract boolean authenticate(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
-    protected abstract String generateChallenge(T challengeResponse, HttpContext context);
+    protected abstract String generateChallenge(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
     protected HttpEntity generateResponseContent(final HttpResponse unauthorized) {
         return new StringEntity("Unauthorized");
@@ -75,7 +76,11 @@ public abstract class AbstractHttpServerAuthFilter<T> implements HttpFilterHandl
             final HttpFilterChain chain) throws HttpException, IOException {
         final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
         final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
-        final boolean authenticated = authenticate(challengeResponse, context);
+
+        final URIAuthority authority = request.getAuthority();
+        final String requestUri = request.getRequestUri();
+
+        final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
         final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
         final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
 
@@ -86,7 +91,7 @@ public abstract class AbstractHttpServerAuthFilter<T> implements HttpFilterHandl
             chain.proceed(request, responseTrigger, context);
         } else {
             final ClassicHttpResponse unauthorized = new BasicClassicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
-            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, context));
+            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
             final HttpEntity responseContent = generateResponseContent(unauthorized);
             unauthorized.setEntity(responseContent);
             if (respondImmediately || expectContinue || request.getEntity() == null) {

http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/59bff65e/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
----------------------------------------------------------------------
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
index cb2fb26..443af98 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/support/AbstractAsyncServerAuthFilter.java
@@ -48,6 +48,7 @@ import org.apache.hc.core5.http.nio.AsyncFilterHandler;
 import org.apache.hc.core5.http.nio.CapacityChannel;
 import org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer;
 import org.apache.hc.core5.http.protocol.HttpContext;
+import org.apache.hc.core5.net.URIAuthority;
 
 /**
  * @since 5.0
@@ -61,11 +62,11 @@ public abstract class AbstractAsyncServerAuthFilter<T> implements AsyncFilterHan
         this.respondImmediately = respondImmediately;
     }
 
-    protected abstract T parseChallengeResponse(String challenge, HttpContext context) throws HttpException;
+    protected abstract T parseChallengeResponse(String authorizationValue, HttpContext context) throws HttpException;
 
-    protected abstract boolean authenticate(T challengeResponse, HttpContext context);
+    protected abstract boolean authenticate(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
-    protected abstract String generateChallenge(T challengeResponse, HttpContext context);
+    protected abstract String generateChallenge(T challengeResponse, URIAuthority authority, String requestUri, HttpContext context);
 
     protected AsyncEntityProducer generateResponseContent(final HttpResponse unauthorized) {
         return new BasicAsyncEntityProducer("Unauthorized");
@@ -80,7 +81,11 @@ public abstract class AbstractAsyncServerAuthFilter<T> implements AsyncFilterHan
             final AsyncFilterChain chain) throws HttpException, IOException {
         final Header h = request.getFirstHeader(HttpHeaders.AUTHORIZATION);
         final T challengeResponse = h != null ? parseChallengeResponse(h.getValue(), context) : null;
-        final boolean authenticated = authenticate(challengeResponse, context);
+
+        final URIAuthority authority = request.getAuthority();
+        final String requestUri = request.getRequestUri();
+
+        final boolean authenticated = authenticate(challengeResponse, authority, requestUri, context);
         final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
         final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
 
@@ -91,7 +96,7 @@ public abstract class AbstractAsyncServerAuthFilter<T> implements AsyncFilterHan
             return chain.proceed(request, entityDetails, context, responseTrigger);
         } else {
             final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
-            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, context));
+            unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, generateChallenge(challengeResponse, authority, requestUri, context));
             final AsyncEntityProducer responseContentProducer = generateResponseContent(unauthorized);
             if (respondImmediately || expectContinue || entityDetails == null) {
                 responseTrigger.submitResponse(unauthorized, responseContentProducer);