1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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
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 }