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.http.impl.auth;
28  
29  import java.net.Authenticator;
30  import java.net.Authenticator.RequestorType;
31  import java.net.InetAddress;
32  import java.net.PasswordAuthentication;
33  import java.net.URL;
34  import java.util.Locale;
35  
36  import org.apache.hc.client5.http.auth.AuthScope;
37  import org.apache.hc.client5.http.auth.Credentials;
38  import org.apache.hc.client5.http.auth.StandardAuthScheme;
39  import org.apache.hc.client5.http.classic.methods.HttpGet;
40  import org.apache.hc.core5.http.protocol.HttpCoreContext;
41  import org.junit.jupiter.api.Assertions;
42  import org.junit.jupiter.api.Test;
43  import org.mockito.ArgumentMatchers;
44  import org.mockito.Mockito;
45  
46  /**
47   * Simple tests for {@link SystemDefaultCredentialsProvider}.
48   */
49  public class TestSystemDefaultCredentialsProvider {
50  
51      private final static String PROXY_PROTOCOL1 = "http";
52      private final static String PROXY_HOST1 = "proxyhost1";
53      private final static int PROXY_PORT1 = 3128;
54      private final static String PROMPT1 = "HttpClient authentication test prompt";
55      private final static String TARGET_SCHEME1 = "https";
56      private final static String TARGET_HOST1 = "targethost1";
57      private final static int TARGET_PORT1 = 80;
58      private final static PasswordAuthentication AUTH1 =
59          new PasswordAuthentication("testUser", "testPassword".toCharArray());
60  
61      // It's not possible to mock static Authenticator methods. So we mock a delegate
62      private final class DelegatedAuthenticator extends Authenticator {
63          private final AuthenticatorDelegate authenticatorDelegate;
64  
65          private DelegatedAuthenticator(final AuthenticatorDelegate authenticatorDelegate) {
66              this.authenticatorDelegate = authenticatorDelegate;
67          }
68  
69          @Override
70          protected PasswordAuthentication getPasswordAuthentication() {
71              return authenticatorDelegate.getPasswordAuthentication(getRequestingHost(), getRequestingSite(),
72                                                                     getRequestingPort(), getRequestingProtocol(),
73                                                                     getRequestingPrompt(), getRequestingScheme(),
74                                                                     getRequestingURL(), getRequestorType());
75          }
76      }
77  
78      private interface AuthenticatorDelegate {
79          PasswordAuthentication getPasswordAuthentication(
80              String host,
81              InetAddress addr,
82              int port,
83              String protocol,
84              String prompt,
85              String scheme,
86              URL url,
87              RequestorType reqType);
88      }
89  
90      @Test
91      public void testSystemCredentialsProviderCredentials() throws Exception {
92  
93          final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);
94  
95          final URL httpRequestUrl = new URL(TARGET_SCHEME1, TARGET_HOST1, TARGET_PORT1, "/");
96          final AuthScope authScope = new AuthScope(PROXY_PROTOCOL1, PROXY_HOST1, PROXY_PORT1, PROMPT1, StandardAuthScheme.BASIC);
97          final HttpCoreContext coreContext = new HttpCoreContext();
98          coreContext.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpGet(httpRequestUrl.toURI()));
99  
100         final Credentials receivedCredentials =
101             new SystemDefaultCredentialsProvider().getCredentials(authScope, coreContext);
102 
103         Mockito.verify(authenticatorDelegate).getPasswordAuthentication(
104                 PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
105                 PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT),
106                 httpRequestUrl,
107                 RequestorType.SERVER);
108         Assertions.assertNotNull(receivedCredentials);
109         Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
110     }
111 
112     @Test
113     public void testSystemCredentialsProviderNoContext() throws Exception {
114 
115         final AuthenticatorDelegate authenticatorDelegate = installAuthenticator(AUTH1);
116 
117         final AuthScope authScope = new AuthScope(PROXY_PROTOCOL1, PROXY_HOST1, PROXY_PORT1, PROMPT1, StandardAuthScheme.BASIC);
118 
119         final Credentials receivedCredentials =
120             new SystemDefaultCredentialsProvider().getCredentials(authScope, null);
121 
122         Mockito.verify(authenticatorDelegate).getPasswordAuthentication(
123                 PROXY_HOST1, null, PROXY_PORT1, PROXY_PROTOCOL1,
124                 PROMPT1, StandardAuthScheme.BASIC.toUpperCase(Locale.ROOT), null,
125                 RequestorType.SERVER);
126         Assertions.assertNotNull(receivedCredentials);
127         Assertions.assertEquals(AUTH1.getUserName(), receivedCredentials.getUserPrincipal().getName());
128     }
129 
130     private AuthenticatorDelegate installAuthenticator(final PasswordAuthentication returedAuthentication) {
131         final AuthenticatorDelegate authenticatorDelegate = Mockito.mock(AuthenticatorDelegate.class);
132         Mockito.when(authenticatorDelegate.getPasswordAuthentication(ArgumentMatchers.anyString(),
133                                                                      ArgumentMatchers.any(), ArgumentMatchers.anyInt(),
134                                                                      ArgumentMatchers.anyString(), ArgumentMatchers.anyString(),
135                                                                      ArgumentMatchers.anyString(), ArgumentMatchers.any(),
136                                                                      ArgumentMatchers.any())).thenReturn(returedAuthentication);
137         Authenticator.setDefault(new DelegatedAuthenticator(authenticatorDelegate));
138         return authenticatorDelegate;
139     }
140 }