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