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.nio.client.integration;
28  
29  import java.io.IOException;
30  import java.util.Arrays;
31  import java.util.concurrent.Future;
32  import java.util.concurrent.atomic.AtomicLong;
33  
34  import org.apache.http.Consts;
35  import org.apache.http.localserver.HttpAsyncTestBase;
36  import org.apache.http.HttpEntity;
37  import org.apache.http.HttpException;
38  import org.apache.http.HttpHost;
39  import org.apache.http.HttpRequest;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.HttpResponseInterceptor;
42  import org.apache.http.HttpStatus;
43  import org.apache.http.auth.AUTH;
44  import org.apache.http.auth.AuthScheme;
45  import org.apache.http.auth.AuthSchemeProvider;
46  import org.apache.http.auth.AuthScope;
47  import org.apache.http.auth.Credentials;
48  import org.apache.http.auth.UsernamePasswordCredentials;
49  import org.apache.http.client.CredentialsProvider;
50  import org.apache.http.client.config.RequestConfig;
51  import org.apache.http.client.methods.HttpGet;
52  import org.apache.http.client.protocol.HttpClientContext;
53  import org.apache.http.config.Registry;
54  import org.apache.http.config.RegistryBuilder;
55  import org.apache.http.entity.StringEntity;
56  import org.apache.http.impl.auth.BasicScheme;
57  import org.apache.http.impl.auth.BasicSchemeFactory;
58  import org.apache.http.impl.client.TargetAuthenticationStrategy;
59  import org.apache.http.localserver.RequestBasicAuth;
60  import org.apache.http.nio.protocol.BasicAsyncRequestHandler;
61  import org.apache.http.protocol.HttpContext;
62  import org.apache.http.protocol.HttpRequestHandler;
63  import org.apache.http.util.EntityUtils;
64  import org.junit.Assert;
65  import org.junit.Before;
66  import org.junit.Test;
67  
68  public class TestClientReauthentication extends HttpAsyncTestBase {
69  
70      @Before @Override
71      public void setUp() throws Exception {
72          super.setUp();
73          this.serverBootstrap.addInterceptorFirst(new RequestBasicAuth());
74          this.serverBootstrap.addInterceptorLast(new ResponseBasicUnauthorized());
75      }
76  
77      public class ResponseBasicUnauthorized implements HttpResponseInterceptor {
78  
79          @Override
80          public void process(
81                  final HttpResponse response,
82                  final HttpContext context) throws HttpException, IOException {
83              if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
84                  response.addHeader(AUTH.WWW_AUTH, "MyBasic realm=\"test realm\"");
85              }
86          }
87  
88      }
89  
90      static class AuthHandler implements HttpRequestHandler {
91  
92          private final AtomicLong count = new AtomicLong(0);
93  
94          @Override
95          public void handle(
96                  final HttpRequest request,
97                  final HttpResponse response,
98                  final HttpContext context) throws HttpException, IOException {
99              final String creds = (String) context.getAttribute("creds");
100             if (creds == null || !creds.equals("test:test")) {
101                 response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
102             } else {
103                 // Make client re-authenticate on each fourth request
104                 if (this.count.incrementAndGet() % 4 == 0) {
105                     response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
106                 } else {
107                     response.setStatusCode(HttpStatus.SC_OK);
108                     final StringEntity entity = new StringEntity("success", Consts.ASCII);
109                     response.setEntity(entity);
110                 }
111             }
112         }
113 
114     }
115 
116     static class TestCredentialsProvider implements CredentialsProvider {
117 
118         private final Credentials creds;
119         private AuthScope authscope;
120 
121         TestCredentialsProvider(final Credentials creds) {
122             super();
123             this.creds = creds;
124         }
125 
126         @Override
127         public void clear() {
128         }
129 
130         @Override
131         public Credentials getCredentials(final AuthScope authscope) {
132             this.authscope = authscope;
133             return this.creds;
134         }
135 
136         @Override
137         public void setCredentials(final AuthScope authscope, final Credentials credentials) {
138         }
139 
140         public AuthScope getAuthScope() {
141             return this.authscope;
142         }
143 
144     }
145 
146     @Test
147     public void testBasicAuthenticationSuccess() throws Exception {
148         this.serverBootstrap.registerHandler("*", new BasicAsyncRequestHandler(new AuthHandler()));
149 
150         final BasicSchemeFactory myBasicAuthSchemeFactory = new BasicSchemeFactory() {
151 
152             @Override
153             public AuthScheme create(final HttpContext context) {
154                 return new BasicScheme() {
155                     private static final long serialVersionUID = 1L;
156 
157                     @Override
158                     public String getSchemeName() {
159                         return "MyBasic";
160                     }
161 
162                 };
163             }
164 
165         };
166 
167         final TargetAuthenticationStrategy myAuthStrategy = new TargetAuthenticationStrategy() {
168 
169             @Override
170             protected boolean isCachable(final AuthScheme authScheme) {
171                 return "MyBasic".equalsIgnoreCase(authScheme.getSchemeName());
172             }
173 
174         };
175 
176         final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
177                 new UsernamePasswordCredentials("test", "test"));
178 
179         final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
180             .register("MyBasic", myBasicAuthSchemeFactory)
181             .build();
182         this.clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
183         this.clientBuilder.setTargetAuthenticationStrategy(myAuthStrategy);
184         final HttpHost target = start();
185 
186         final RequestConfig config = RequestConfig.custom()
187                 .setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
188                 .build();
189         final HttpClientContext context = HttpClientContext.create();
190         context.setCredentialsProvider(credsProvider);
191 
192         for (int i = 0; i < 10; i++) {
193             final HttpGet httpget = new HttpGet("/");
194             httpget.setConfig(config);
195             final Future<HttpResponse> future = this.httpclient.execute(target, httpget, context, null);
196             final HttpResponse response = future.get();
197             Assert.assertNotNull(response);
198             final HttpEntity entity = response.getEntity();
199             Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
200             Assert.assertNotNull(entity);
201             EntityUtils.consume(entity);
202         }
203     }
204 
205 }