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.impl.client.integration;
28  
29  import java.io.IOException;
30  
31  import org.apache.http.Consts;
32  import org.apache.http.HttpEntity;
33  import org.apache.http.HttpException;
34  import org.apache.http.HttpHost;
35  import org.apache.http.HttpRequest;
36  import org.apache.http.HttpResponse;
37  import org.apache.http.HttpResponseInterceptor;
38  import org.apache.http.HttpStatus;
39  import org.apache.http.auth.AUTH;
40  import org.apache.http.auth.AuthScope;
41  import org.apache.http.auth.Credentials;
42  import org.apache.http.auth.UsernamePasswordCredentials;
43  import org.apache.http.client.CredentialsProvider;
44  import org.apache.http.client.methods.HttpGet;
45  import org.apache.http.client.protocol.HttpClientContext;
46  import org.apache.http.entity.StringEntity;
47  import org.apache.http.localserver.LocalServerTestBase;
48  import org.apache.http.localserver.RequestBasicAuth;
49  import org.apache.http.protocol.HttpContext;
50  import org.apache.http.protocol.HttpProcessor;
51  import org.apache.http.protocol.HttpProcessorBuilder;
52  import org.apache.http.protocol.HttpRequestHandler;
53  import org.apache.http.protocol.ResponseConnControl;
54  import org.apache.http.protocol.ResponseContent;
55  import org.apache.http.protocol.ResponseDate;
56  import org.apache.http.protocol.ResponseServer;
57  import org.apache.http.util.EntityUtils;
58  import org.junit.Assert;
59  import org.junit.Before;
60  import org.junit.Test;
61  
62  public class TestClientAuthenticationFallBack extends LocalServerTestBase {
63  
64      public class ResponseBasicUnauthorized implements HttpResponseInterceptor {
65  
66          @Override
67          public void process(
68                  final HttpResponse response,
69                  final HttpContext context) throws HttpException, IOException {
70              if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
71                  response.addHeader(AUTH.WWW_AUTH, "Digest realm=\"test realm\" invalid");
72                  response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"test realm\"");
73              }
74          }
75  
76      }
77  
78      @Before @Override
79      public void setUp() throws Exception {
80          super.setUp();
81          final HttpProcessor httpproc = HttpProcessorBuilder.create()
82              .add(new ResponseDate())
83              .add(new ResponseServer(LocalServerTestBase.ORIGIN))
84              .add(new ResponseContent())
85              .add(new ResponseConnControl())
86              .add(new RequestBasicAuth())
87              .add(new ResponseBasicUnauthorized()).build();
88          this.serverBootstrap.setHttpProcessor(httpproc);
89      }
90  
91      static class AuthHandler implements HttpRequestHandler {
92  
93          @Override
94          public void handle(
95                  final HttpRequest request,
96                  final HttpResponse response,
97                  final HttpContext context) throws HttpException, IOException {
98              final String creds = (String) context.getAttribute("creds");
99              if (creds == null || !creds.equals("test:test")) {
100                 response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
101             } else {
102                 response.setStatusCode(HttpStatus.SC_OK);
103                 final StringEntity entity = new StringEntity("success", Consts.ASCII);
104                 response.setEntity(entity);
105             }
106         }
107 
108     }
109 
110     static class TestCredentialsProvider implements CredentialsProvider {
111 
112         private final Credentials creds;
113         private AuthScope authscope;
114 
115         TestCredentialsProvider(final Credentials creds) {
116             super();
117             this.creds = creds;
118         }
119 
120         @Override
121         public void clear() {
122         }
123 
124         @Override
125         public Credentials getCredentials(final AuthScope authscope) {
126             this.authscope = authscope;
127             return this.creds;
128         }
129 
130         @Override
131         public void setCredentials(final AuthScope authscope, final Credentials credentials) {
132         }
133 
134         public AuthScope getAuthScope() {
135             return this.authscope;
136         }
137 
138     }
139 
140     @Test
141     public void testBasicAuthenticationSuccess() throws Exception {
142         this.serverBootstrap.registerHandler("*", new AuthHandler());
143 
144         final HttpHost target = start();
145 
146         final HttpClientContext context = HttpClientContext.create();
147         final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
148                 new UsernamePasswordCredentials("test", "test"));
149         context.setCredentialsProvider(credsProvider);
150         final HttpGet httpget = new HttpGet("/");
151 
152         final HttpResponse response = this.httpclient.execute(target, httpget, context);
153         final HttpEntity entity = response.getEntity();
154         Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
155         Assert.assertNotNull(entity);
156         EntityUtils.consume(entity);
157         final AuthScope authscope = credsProvider.getAuthScope();
158         Assert.assertNotNull(authscope);
159         Assert.assertEquals("test realm", authscope.getRealm());
160     }
161 
162 }