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.http.client.protocol;
28  
29  import org.apache.http.HttpHost;
30  import org.apache.http.HttpRequest;
31  import org.apache.http.HttpRequestInterceptor;
32  import org.apache.http.auth.AuthProtocolState;
33  import org.apache.http.auth.AuthScope;
34  import org.apache.http.auth.AuthState;
35  import org.apache.http.auth.Credentials;
36  import org.apache.http.auth.UsernamePasswordCredentials;
37  import org.apache.http.client.AuthCache;
38  import org.apache.http.conn.routing.HttpRoute;
39  import org.apache.http.impl.auth.BasicScheme;
40  import org.apache.http.impl.auth.DigestScheme;
41  import org.apache.http.impl.client.BasicAuthCache;
42  import org.apache.http.impl.client.BasicCredentialsProvider;
43  import org.apache.http.message.BasicHttpRequest;
44  import org.apache.http.protocol.HttpCoreContext;
45  import org.junit.Assert;
46  import org.junit.Before;
47  import org.junit.Test;
48  
49  public class TestRequestAuthCache {
50  
51      private HttpHost target;
52      private HttpHost proxy;
53      private Credentials creds1;
54      private Credentials creds2;
55      private AuthScope authscope1;
56      private AuthScope authscope2;
57      private BasicScheme authscheme1;
58      private BasicScheme authscheme2;
59      private DigestScheme digestAuthscheme1;
60      private DigestScheme digestAuthscheme2;
61      private BasicCredentialsProvider credProvider;
62      private AuthState targetState;
63      private AuthState proxyState;
64  
65      @Before
66      public void setUp() {
67          this.target = new HttpHost("localhost", 80);
68          this.proxy = new HttpHost("localhost", 8080);
69  
70          this.credProvider = new BasicCredentialsProvider();
71          this.creds1 = new UsernamePasswordCredentials("user1", "secret1");
72          this.creds2 = new UsernamePasswordCredentials("user2", "secret2");
73          this.authscope1 = new AuthScope(this.target);
74          this.authscope2 = new AuthScope(this.proxy);
75          this.authscheme1 = new BasicScheme();
76          this.authscheme2 = new BasicScheme();
77          this.digestAuthscheme1 = new DigestScheme();
78          this.digestAuthscheme2 = new DigestScheme();
79  
80          this.credProvider.setCredentials(this.authscope1, this.creds1);
81          this.credProvider.setCredentials(this.authscope2, this.creds2);
82  
83          this.targetState = new AuthState();
84          this.proxyState = new AuthState();
85      }
86  
87      @Test(expected=IllegalArgumentException.class)
88      public void testRequestParameterCheck() throws Exception {
89          final HttpClientContext context = HttpClientContext.create();
90          final HttpRequestInterceptor interceptor = new RequestAuthCache();
91          interceptor.process(null, context);
92      }
93  
94      @Test(expected=IllegalArgumentException.class)
95      public void testContextParameterCheck() throws Exception {
96          final HttpRequest request = new BasicHttpRequest("GET", "/");
97          final HttpRequestInterceptor interceptor = new RequestAuthCache();
98          interceptor.process(request, null);
99      }
100 
101     @Test
102     public void testPreemptiveTargetAndProxyAuth() throws Exception {
103         final HttpRequest request = new BasicHttpRequest("GET", "/");
104 
105         final HttpClientContext context = HttpClientContext.create();
106         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
107         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
108         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
109         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
110         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
111 
112         final AuthCache authCache = new BasicAuthCache();
113         authCache.put(this.target, this.authscheme1);
114         authCache.put(this.proxy, this.authscheme2);
115 
116         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
117 
118         final HttpRequestInterceptor interceptor = new RequestAuthCache();
119         interceptor.process(request, context);
120         Assert.assertNotNull(this.targetState.getAuthScheme());
121         Assert.assertSame(this.creds1, this.targetState.getCredentials());
122         Assert.assertNotNull(this.proxyState.getAuthScheme());
123         Assert.assertSame(this.creds2, this.proxyState.getCredentials());
124     }
125 
126     @Test
127     public void testCredentialsProviderNotSet() throws Exception {
128         final HttpRequest request = new BasicHttpRequest("GET", "/");
129 
130         final HttpClientContext context = HttpClientContext.create();
131         context.setAttribute(HttpClientContext.CREDS_PROVIDER, null);
132         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
133         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
134         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
135         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
136 
137         final AuthCache authCache = new BasicAuthCache();
138         authCache.put(this.target, this.authscheme1);
139         authCache.put(this.proxy, this.authscheme2);
140 
141         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
142 
143         final HttpRequestInterceptor interceptor = new RequestAuthCache();
144         interceptor.process(request, context);
145         Assert.assertNull(this.targetState.getAuthScheme());
146         Assert.assertNull(this.targetState.getCredentials());
147         Assert.assertNull(this.proxyState.getAuthScheme());
148         Assert.assertNull(this.proxyState.getCredentials());
149     }
150 
151     @Test
152     public void testAuthCacheNotSet() throws Exception {
153         final HttpRequest request = new BasicHttpRequest("GET", "/");
154 
155         final HttpClientContext context = HttpClientContext.create();
156         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
157         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
158         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
159         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
160         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
161         context.setAttribute(HttpClientContext.AUTH_CACHE, null);
162 
163         final HttpRequestInterceptor interceptor = new RequestAuthCache();
164         interceptor.process(request, context);
165         Assert.assertNull(this.targetState.getAuthScheme());
166         Assert.assertNull(this.targetState.getCredentials());
167         Assert.assertNull(this.proxyState.getAuthScheme());
168         Assert.assertNull(this.proxyState.getCredentials());
169     }
170 
171     @Test
172     public void testAuthCacheEmpty() throws Exception {
173         final HttpRequest request = new BasicHttpRequest("GET", "/");
174 
175         final HttpClientContext context = HttpClientContext.create();
176         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
177         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
178         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
179         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
180         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
181 
182         final AuthCache authCache = new BasicAuthCache();
183         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
184 
185         final HttpRequestInterceptor interceptor = new RequestAuthCache();
186         interceptor.process(request, context);
187         Assert.assertNull(this.targetState.getAuthScheme());
188         Assert.assertNull(this.targetState.getCredentials());
189         Assert.assertNull(this.proxyState.getAuthScheme());
190         Assert.assertNull(this.proxyState.getCredentials());
191     }
192 
193     @Test
194     public void testNoMatchingCredentials() throws Exception {
195         final HttpRequest request = new BasicHttpRequest("GET", "/");
196 
197         this.credProvider.clear();
198 
199         final HttpClientContext context = HttpClientContext.create();
200         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
201         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
202         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
203         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
204         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
205 
206         final AuthCache authCache = new BasicAuthCache();
207         authCache.put(this.target, this.authscheme1);
208         authCache.put(this.proxy, this.authscheme2);
209 
210         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
211 
212         final HttpRequestInterceptor interceptor = new RequestAuthCache();
213         interceptor.process(request, context);
214         Assert.assertNull(this.targetState.getAuthScheme());
215         Assert.assertNull(this.targetState.getCredentials());
216         Assert.assertNull(this.proxyState.getAuthScheme());
217         Assert.assertNull(this.proxyState.getCredentials());
218     }
219 
220     @Test
221     public void testAuthSchemeAlreadySet() throws Exception {
222         final HttpRequest request = new BasicHttpRequest("GET", "/");
223 
224         final HttpClientContext context = HttpClientContext.create();
225         context.setAttribute(HttpClientContext.CREDS_PROVIDER, this.credProvider);
226         context.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, this.target);
227         context.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(this.target, null, this.proxy, false));
228         context.setAttribute(HttpClientContext.TARGET_AUTH_STATE, this.targetState);
229         context.setAttribute(HttpClientContext.PROXY_AUTH_STATE, this.proxyState);
230 
231         final AuthCache authCache = new BasicAuthCache();
232         authCache.put(this.target, this.authscheme1);
233         authCache.put(this.proxy, this.authscheme2);
234 
235         context.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
236 
237         this.targetState.setState(AuthProtocolState.CHALLENGED);
238         this.targetState.update(new BasicScheme(), new UsernamePasswordCredentials("user3", "secret3"));
239         this.proxyState.setState(AuthProtocolState.CHALLENGED);
240         this.proxyState.update(new BasicScheme(), new UsernamePasswordCredentials("user4", "secret4"));
241 
242         final HttpRequestInterceptor interceptor = new RequestAuthCache();
243         interceptor.process(request, context);
244         Assert.assertNotSame(this.authscheme1, this.targetState.getAuthScheme());
245         Assert.assertNotSame(this.creds1, this.targetState.getCredentials());
246         Assert.assertNotSame(this.authscheme2, this.proxyState.getAuthScheme());
247         Assert.assertNotSame(this.creds2, this.proxyState.getCredentials());
248     }
249 
250 }