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 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.client5.http.protocol.HttpClientContext;
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
48
49 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
62 private final static 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 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 HttpClientContext context = HttpClientContext.create();
98 context.setRequest(new HttpGet(httpRequestUrl.toURI()));
99
100 final Credentials receivedCredentials =
101 new SystemDefaultCredentialsProvider().getCredentials(authScope, context);
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 void testSystemCredentialsProviderNoContext() {
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 }