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