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 java.io.IOException;
30  
31  import org.apache.hc.client5.http.auth.AuthScheme;
32  import org.apache.hc.client5.http.auth.AuthSchemeFactory;
33  import org.apache.hc.client5.http.classic.methods.HttpGet;
34  import org.apache.hc.client5.http.auth.StandardAuthScheme;
35  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
36  import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
37  import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
38  import org.apache.hc.client5.http.impl.win.WinHttpClients;
39  import org.apache.hc.client5.http.impl.win.WindowsNegotiateSchemeGetTokenFail;
40  import org.apache.hc.core5.http.ClassicHttpRequest;
41  import org.apache.hc.core5.http.ClassicHttpResponse;
42  import org.apache.hc.core5.http.HttpException;
43  import org.apache.hc.core5.http.HttpHeaders;
44  import org.apache.hc.core5.http.HttpHost;
45  import org.apache.hc.core5.http.HttpStatus;
46  import org.apache.hc.core5.http.config.Registry;
47  import org.apache.hc.core5.http.config.RegistryBuilder;
48  import org.apache.hc.core5.http.io.HttpRequestHandler;
49  import org.apache.hc.core5.http.io.entity.EntityUtils;
50  import org.apache.hc.core5.http.protocol.HttpContext;
51  import org.junit.Assume;
52  import org.junit.Test;
53  
54  /**
55   * Unit tests for Windows negotiate authentication.
56   */
57  public class TestWindowsNegotiateScheme extends LocalServerTestBase {
58  
59      @Test(timeout=30000) // this timeout (in ms) needs to be extended if you're actively debugging the code
60      public void testNoInfiniteLoopOnSPNOutsideDomain() throws Exception {
61          this.server.registerHandler("/", new HttpRequestHandler() {
62  
63              @Override
64              public void handle(
65                      final ClassicHttpRequest request,
66                      final ClassicHttpResponse response,
67                      final HttpContext context) throws HttpException, IOException {
68                  response.addHeader(HttpHeaders.WWW_AUTHENTICATE, StandardAuthScheme.SPNEGO);
69                  response.setCode(HttpStatus.SC_UNAUTHORIZED);
70              }
71  
72          });
73          Assume.assumeTrue("Test can only be run on Windows", WinHttpClients.isWinAuthAvailable());
74  
75          // HTTPCLIENT-1545
76          // If a service principal name (SPN) from outside your Windows domain tree (e.g., HTTP/example.com) is used,
77          // InitializeSecurityContext will return SEC_E_DOWNGRADE_DETECTED (decimal: -2146892976, hex: 0x80090350).
78          // Because WindowsNegotiateScheme wasn't setting the completed state correctly when authentication fails,
79          // HttpClient goes into an infinite loop, constantly retrying the negotiate authentication to kingdom
80          // come. This error message, "The system detected a possible attempt to compromise security. Please ensure that
81          // you can contact the server that authenticated you." is associated with SEC_E_DOWNGRADE_DETECTED.
82  
83          final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
84              .register(StandardAuthScheme.SPNEGO, new AuthSchemeFactory() {
85                  @Override
86                  public AuthScheme create(final HttpContext context) {
87                      return new WindowsNegotiateSchemeGetTokenFail(StandardAuthScheme.SPNEGO, "HTTP/example.com");
88                  }
89              }).build();
90          final CloseableHttpClient customClient = HttpClientBuilder.create()
91                  .setDefaultAuthSchemeRegistry(authSchemeRegistry).build();
92  
93          final HttpHost target = start();
94          final HttpGet httpGet = new HttpGet("/");
95          try (final CloseableHttpResponse response = customClient.execute(target, httpGet)) {
96              EntityUtils.consume(response.getEntity());
97          }
98      }
99  
100 }