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 2022/12/06 20:07:54 UTC
[httpcomponents-client] 05/06: New Authenticator interface method to return an auth result with additional challenge parameters
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch bearer_auth_support
in repository https://gitbox.apache.org/repos/asf/httpcomponents-client.git
commit 46a4779a055c4d519eb7fea82b451d733237aa27
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Dec 6 21:05:23 2022 +0100
New Authenticator interface method to return an auth result with additional challenge parameters
---
.../async/AuthenticatingAsyncDecorator.java | 18 +++--
.../apache/hc/client5/testing/auth/AuthResult.java | 92 ++++++++++++++++++++++
.../hc/client5/testing/auth/Authenticator.java | 7 ++
.../testing/classic/AuthenticatingDecorator.java | 19 +++--
4 files changed, 126 insertions(+), 10 deletions(-)
diff --git a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AuthenticatingAsyncDecorator.java b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AuthenticatingAsyncDecorator.java
index f0bff871d..550a2a82a 100644
--- a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AuthenticatingAsyncDecorator.java
+++ b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/async/AuthenticatingAsyncDecorator.java
@@ -28,10 +28,11 @@ package org.apache.hc.client5.testing.async;
import java.io.IOException;
import java.nio.ByteBuffer;
-import java.util.Collections;
+import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
+import org.apache.hc.client5.testing.auth.AuthResult;
import org.apache.hc.client5.testing.auth.AuthenticationHandler;
import org.apache.hc.client5.testing.auth.Authenticator;
import org.apache.hc.client5.testing.auth.BasicAuthenticationHandler;
@@ -43,6 +44,7 @@ import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpRequest;
import org.apache.hc.core5.http.HttpResponse;
import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.message.BasicHttpResponse;
import org.apache.hc.core5.http.message.BasicNameValuePair;
@@ -95,20 +97,26 @@ public class AuthenticatingAsyncDecorator implements AsyncServerExchangeHandler
final URIAuthority authority = request.getAuthority();
final String requestUri = request.getRequestUri();
- final boolean authenticated = authenticator.authenticate(authority, requestUri, challengeResponse);
+ final AuthResult authResult = authenticator.auth(authority, requestUri, challengeResponse);
final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
- if (authenticated) {
+ if (authResult.isSuccess()) {
if (expectContinue) {
responseChannel.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE), context);
}
exchangeHandler.handleRequest(request, entityDetails, responseChannel, context);
} else {
final HttpResponse unauthorized = new BasicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
+ final List<NameValuePair> challengeParams = new ArrayList<>();
final String realm = authenticator.getRealm(authority, requestUri);
- final String challenge = authenticationHandler.challenge(
- realm != null ? Collections.singletonList(new BasicNameValuePair("realm", realm)) : null);
+ if (realm != null) {
+ challengeParams.add(new BasicNameValuePair("realm", realm));
+ }
+ if (authResult.hasParams()) {
+ challengeParams.addAll(authResult.getParams());
+ }
+ final String challenge = authenticationHandler.challenge(challengeParams);
unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, challenge);
customizeUnauthorizedResponse(unauthorized);
diff --git a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/AuthResult.java b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/AuthResult.java
new file mode 100644
index 000000000..5ee1deb36
--- /dev/null
+++ b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/AuthResult.java
@@ -0,0 +1,92 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.client5.testing.auth;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.util.LangUtils;
+
+public final class AuthResult {
+
+ private final boolean success;
+ private final List<NameValuePair> params;
+
+ public AuthResult(final boolean success, final List<NameValuePair> params) {
+ this.success = success;
+ this.params = params != null ? Collections.unmodifiableList(params) : Collections.emptyList();
+ }
+
+ public AuthResult(final boolean success, final NameValuePair... params) {
+ this(success, Arrays.asList(params));
+ }
+
+ public boolean isSuccess() {
+ return success;
+ }
+
+ public boolean hasParams() {
+ return !params.isEmpty();
+ }
+
+ public List<NameValuePair> getParams() {
+ return params;
+ }
+
+ @Override
+ public int hashCode() {
+ int hash = LangUtils.HASH_SEED;
+ hash = LangUtils.hashCode(hash, this.success);
+ return hash;
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o instanceof AuthResult) {
+ final AuthResult that = (AuthResult) o;
+ return this.success == that.success;
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder buf = new StringBuilder();
+ buf.append(success);
+ if (!params.isEmpty()) {
+ buf.append(" ").append(params);
+ }
+ return buf.toString();
+ }
+
+}
diff --git a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/Authenticator.java b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/Authenticator.java
index 911b3c455..562482c07 100644
--- a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/Authenticator.java
+++ b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/auth/Authenticator.java
@@ -31,6 +31,13 @@ import org.apache.hc.core5.net.URIAuthority;
public interface Authenticator {
+ /**
+ * @since 5.3
+ */
+ default AuthResult auth(URIAuthority authority, String requestUri, String credentials) {
+ return new AuthResult(authenticate(authority, requestUri, credentials));
+ }
+
boolean authenticate(URIAuthority authority, String requestUri, String credentials);
String getRealm(URIAuthority authority, String requestUri);
diff --git a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/AuthenticatingDecorator.java b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/AuthenticatingDecorator.java
index e0598043e..c7ee521c6 100644
--- a/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/AuthenticatingDecorator.java
+++ b/httpclient5-testing/src/main/java/org/apache/hc/client5/testing/classic/AuthenticatingDecorator.java
@@ -28,8 +28,10 @@
package org.apache.hc.client5.testing.classic;
import java.io.IOException;
-import java.util.Collections;
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.hc.client5.testing.auth.AuthResult;
import org.apache.hc.client5.testing.auth.AuthenticationHandler;
import org.apache.hc.client5.testing.auth.Authenticator;
import org.apache.hc.client5.testing.auth.BasicAuthenticationHandler;
@@ -39,6 +41,7 @@ import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.HttpServerRequestHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
@@ -84,20 +87,26 @@ public class AuthenticatingDecorator implements HttpServerRequestHandler {
final URIAuthority authority = request.getAuthority();
final String requestUri = request.getRequestUri();
- final boolean authenticated = authenticator.authenticate(authority, requestUri, challengeResponse);
+ final AuthResult authResult = authenticator.auth(authority, requestUri, challengeResponse);
final Header expect = request.getFirstHeader(HttpHeaders.EXPECT);
final boolean expectContinue = expect != null && "100-continue".equalsIgnoreCase(expect.getValue());
- if (authenticated) {
+ if (authResult.isSuccess()) {
if (expectContinue) {
responseTrigger.sendInformation(new BasicClassicHttpResponse(HttpStatus.SC_CONTINUE));
}
requestHandler.handle(request, responseTrigger, context);
} else {
final ClassicHttpResponse unauthorized = new BasicClassicHttpResponse(HttpStatus.SC_UNAUTHORIZED);
+ final List<NameValuePair> challengeParams = new ArrayList<>();
final String realm = authenticator.getRealm(authority, requestUri);
- final String challenge = authenticationHandler.challenge(
- realm != null ? Collections.singletonList(new BasicNameValuePair("realm", realm)) : null);
+ if (realm != null) {
+ challengeParams.add(new BasicNameValuePair("realm", realm));
+ }
+ if (authResult.hasParams()) {
+ challengeParams.addAll(authResult.getParams());
+ }
+ final String challenge = authenticationHandler.challenge(challengeParams);
unauthorized.addHeader(HttpHeaders.WWW_AUTHENTICATE, challenge);
customizeUnauthorizedResponse(unauthorized);
if (unauthorized.getEntity() == null) {