View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.testing.sync;
28  
29  import static java.util.concurrent.TimeUnit.MILLISECONDS;
30  
31  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
32  import org.apache.hc.client5.http.auth.StandardAuthScheme;
33  import org.apache.hc.client5.http.classic.methods.HttpGet;
34  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
35  import org.apache.hc.client5.http.impl.win.WinHttpClients;
36  import org.apache.hc.client5.http.impl.win.WindowsNegotiateSchemeGetTokenFail;
37  import org.apache.hc.client5.testing.sync.extension.TestClientResources;
38  import org.apache.hc.core5.http.HttpHeaders;
39  import org.apache.hc.core5.http.HttpHost;
40  import org.apache.hc.core5.http.HttpStatus;
41  import org.apache.hc.core5.http.URIScheme;
42  import org.apache.hc.core5.http.config.Registry;
43  import org.apache.hc.core5.http.config.RegistryBuilder;
44  import org.apache.hc.core5.http.io.entity.EntityUtils;
45  import org.apache.hc.core5.testing.classic.ClassicTestServer;
46  import org.apache.hc.core5.util.Timeout;
47  import org.junit.jupiter.api.Assumptions;
48  import org.junit.jupiter.api.Test;
49  import org.junit.jupiter.api.extension.RegisterExtension;
50  
51  /**
52   * Unit tests for Windows negotiate authentication.
53   */
54  public class TestWindowsNegotiateScheme {
55  
56      public static final Timeout TIMEOUT = Timeout.ofMinutes(1);
57  
58      @RegisterExtension
59      private TestClientResources testResources = new TestClientResources(URIScheme.HTTP, TIMEOUT);
60  
61      @Test // this timeout (in ms) needs to be extended if you're actively debugging the code
62      @org.junit.jupiter.api.Timeout(value = 30000, unit = MILLISECONDS)
63      public void testNoInfiniteLoopOnSPNOutsideDomain() throws Exception {
64          final ClassicTestServer server = testResources.startServer(null, null, null);
65          server.registerHandler("/", (request, response, context) -> {
66              response.addHeader(HttpHeaders.WWW_AUTHENTICATE, StandardAuthScheme.SPNEGO);
67              response.setCode(HttpStatus.SC_UNAUTHORIZED);
68          });
69          final HttpHost target = testResources.targetHost();
70          Assumptions.assumeTrue(WinHttpClients.isWinAuthAvailable(), "Test can only be run on Windows");
71  
72          // HTTPCLIENT-1545
73          // If a service principal name (SPN) from outside your Windows domain tree (e.g., HTTP/example.com) is used,
74          // InitializeSecurityContext will return SEC_E_DOWNGRADE_DETECTED (decimal: -2146892976, hex: 0x80090350).
75          // Because WindowsNegotiateScheme wasn't setting the completed state correctly when authentication fails,
76          // HttpClient goes into an infinite loop, constantly retrying the negotiate authentication to kingdom
77          // come. This error message, "The system detected a possible attempt to compromise security. Please ensure that
78          // you can contact the server that authenticated you." is associated with SEC_E_DOWNGRADE_DETECTED.
79  
80          final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
81              .register(StandardAuthScheme.SPNEGO, context -> new WindowsNegotiateSchemeGetTokenFail(StandardAuthScheme.SPNEGO, "HTTP/example.com")).build();
82  
83          final CloseableHttpClient client = testResources.startClient(builder -> builder
84                  .setDefaultAuthSchemeRegistry(authSchemeRegistry)
85          );
86  
87          final HttpGet httpGet = new HttpGet("/");
88          client.execute(target, httpGet, response -> {
89              EntityUtils.consume(response.getEntity());
90              return null;
91          });
92      }
93  
94  }