Updated AuthState API

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1182317 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2011-10-12 11:45:54 +00:00
parent 9edd1acd4f
commit 4d1b157272
10 changed files with 127 additions and 120 deletions

View File

@ -30,7 +30,6 @@ import java.util.Queue;
import org.apache.http.annotation.NotThreadSafe;
/**
* This class provides detailed information about the state of the authentication process.
*
@ -51,6 +50,7 @@ public class AuthState {
/** Credentials selected for authentication */
private Credentials credentials;
/** Available auth options */
private Queue<AuthOption> authOptions;
public AuthState() {
@ -59,7 +59,9 @@ public class AuthState {
}
/**
* Resets authentication state.
* Resets the auth state.
*
* @since 4.2
*/
public void reset() {
this.state = AuthProtocolState.UNCHALLENGED;
@ -69,6 +71,89 @@ public class AuthState {
this.credentials = null;
}
/**
* @since 4.2
*/
public AuthProtocolState getState() {
return this.state;
}
/**
* @since 4.2
*/
public void setState(final AuthProtocolState state) {
this.state = state != null ? state : AuthProtocolState.UNCHALLENGED;
}
/**
* Returns actual {@link AuthScheme}. May be null.
*/
public AuthScheme getAuthScheme() {
return this.authScheme;
}
/**
* Returns actual {@link Credentials}. May be null.
*/
public Credentials getCredentials() {
return this.credentials;
}
/**
* Updates the auth state with {@link AuthScheme} and {@link Credentials}.
*
* @param authScheme auth scheme. May not be null.
* @param credentials user crednetials. May not be null.
*
* @since 4.2
*/
public void update(final AuthScheme authScheme, final Credentials credentials) {
if (authScheme == null) {
throw new IllegalArgumentException("Auth scheme may not be null or empty");
}
if (credentials == null) {
throw new IllegalArgumentException("Credentials may not be null or empty");
}
this.authScheme = authScheme;
this.credentials = credentials;
this.authOptions = null;
}
/**
* Returns available {@link AuthOption}s. May be null.
*
* @since 4.2
*/
public Queue<AuthOption> getAuthOptions() {
return this.authOptions;
}
/**
* Returns <code>true</code> if {@link AuthOption}s are available, <code>false</code>
* otherwise.
*
* @since 4.2
*/
public boolean hasAuthOptions() {
return this.authOptions != null && !this.authOptions.isEmpty();
}
/**
* Updates the auth state with a queue of {@link AuthOption}s.
*
* @param authOptions a queue of auth options. May not be null or empty.
*
* @since 4.2
*/
public void update(final Queue<AuthOption> authOptions) {
if (authOptions == null || authOptions.isEmpty()) {
throw new IllegalArgumentException("Queue of auth options may not be null or empty");
}
this.authOptions = authOptions;
this.authScheme = null;
this.credentials = null;
}
/**
* Invalidates the authentication state by resetting its parameters.
*
@ -88,7 +173,10 @@ public class AuthState {
* Assigns the given {@link AuthScheme authentication scheme}.
*
* @param authScheme the {@link AuthScheme authentication scheme}
*
* @deprecated use {@link #update(AuthScheme, Credentials)}
*/
@Deprecated
public void setAuthScheme(final AuthScheme authScheme) {
if (authScheme == null) {
reset();
@ -97,43 +185,14 @@ public class AuthState {
this.authScheme = authScheme;
}
/**
* Returns the {@link AuthScheme authentication scheme}.
*
* @return {@link AuthScheme authentication scheme}
*/
public AuthScheme getAuthScheme() {
return this.authScheme;
}
/**
* @since 4.2
*/
public AuthProtocolState getState() {
return this.state;
}
/**
* @since 4.2
*/
public void setState(final AuthProtocolState state) {
this.state = state != null ? state : AuthProtocolState.UNCHALLENGED;
}
/**
* Returns user {@link Credentials} selected for authentication if available
*
* @return user credentials if available, <code>null</code otherwise
*/
public Credentials getCredentials() {
return this.credentials;
}
/**
* Sets user {@link Credentials} to be used for authentication
*
* @param credentials User credentials
*
* @deprecated use {@link #update(AuthScheme, Credentials)}
*/
@Deprecated
public void setCredentials(final Credentials credentials) {
this.credentials = credentials;
}
@ -162,41 +221,6 @@ public class AuthState {
this.authScope = authScope;
}
/**
* Returns available authentication options.
*
* @return authentication options, if available, <code>null</null> otherwise.
*
* @since 4.2
*/
public Queue<AuthOption> getAuthOptions() {
return this.authOptions;
}
/**
* Returns <code>true</code> if authentication options are available, <code>false</code>
* otherwise.
*
* @return <code>true</code> if authentication options are available, <code>false</code>
* otherwise.
*
* @since 4.2
*/
public boolean hasAuthOptions() {
return this.authOptions != null && !this.authOptions.isEmpty();
}
/**
* Sets authentication options to select from when authenticating.
*
* @param authOptions authentication options
*
* @since 4.2
*/
public void setAuthOptions(final Queue<AuthOption> authOptions) {
this.authOptions = authOptions != null && !authOptions.isEmpty() ? authOptions : null;
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();

View File

@ -127,8 +127,7 @@ public class RequestAuthCache implements HttpRequestInterceptor {
Credentials creds = credsProvider.getCredentials(authScope);
if (creds != null) {
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
authState.update(authScheme, creds);
} else {
this.log.debug("No credentials for preemptive authentication");
}

View File

@ -74,8 +74,7 @@ abstract class RequestAuthenticationBase implements HttpRequestInterceptor {
AuthOption authOption = authOptions.remove();
authScheme = authOption.getAuthScheme();
creds = authOption.getCredentials();
authState.setAuthScheme(authScheme);
authState.setCredentials(creds);
authState.update(authScheme, creds);
if (this.log.isDebugEnabled()) {
this.log.debug("Generating response to an authentication challenge using "
+ authScheme.getSchemeName() + " scheme");
@ -83,7 +82,6 @@ abstract class RequestAuthenticationBase implements HttpRequestInterceptor {
try {
Header header = authenticate(authScheme, creds, request, context);
request.addHeader(header);
authState.setAuthOptions(null);
break;
} catch (AuthenticationException ex) {
if (this.log.isWarnEnabled()) {

View File

@ -134,7 +134,7 @@ public class HttpAuthenticator {
Queue<AuthOption> authOptions = authStrategy.select(challenges, host, response, context);
if (authOptions != null && !authOptions.isEmpty()) {
authState.setState(AuthProtocolState.CHALLENGED);
authState.setAuthOptions(authOptions);
authState.update(authOptions);
return true;
} else {
return false;

View File

@ -232,11 +232,9 @@ public class TestRequestAuthCache {
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
this.targetState.setState(AuthProtocolState.CHALLENGED);
this.targetState.setAuthScheme(new BasicScheme());
this.targetState.setCredentials(new UsernamePasswordCredentials("user3", "secret3"));
this.proxyState.setAuthScheme(new BasicScheme());
this.proxyState.setCredentials(new UsernamePasswordCredentials("user4", "secret4"));
this.targetState.update(new BasicScheme(), new UsernamePasswordCredentials("user3", "secret3"));
this.proxyState.setState(AuthProtocolState.CHALLENGED);
this.proxyState.update(new BasicScheme(), new UsernamePasswordCredentials("user4", "secret4"));
HttpRequestInterceptor interceptor = new RequestAuthCache();
interceptor.process(request, context);

View File

@ -82,7 +82,7 @@ public class TestRequestAuthenticationBase {
public void testAuthFailureState() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.FAILURE);
this.authState.setAuthScheme(this.authScheme);
this.authState.update(this.authScheme, this.credentials);
this.interceptor.process(request, this.context);
@ -98,8 +98,7 @@ public class TestRequestAuthenticationBase {
public void testAuthChallengeStateNoOption() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.setAuthScheme(this.authScheme);
this.authState.setCredentials(this.credentials);
this.authState.update(this.authScheme, this.credentials);
Mockito.when(this.authScheme.authenticate(
Mockito.any(Credentials.class),
@ -119,7 +118,7 @@ public class TestRequestAuthenticationBase {
this.authState.setState(AuthProtocolState.CHALLENGED);
LinkedList<AuthOption> authOptions = new LinkedList<AuthOption>();
authOptions.add(new AuthOption(this.authScheme, this.credentials));
this.authState.setAuthOptions(authOptions);
this.authState.update(authOptions);
Mockito.when(this.authScheme.authenticate(
Mockito.any(Credentials.class),
@ -155,7 +154,7 @@ public class TestRequestAuthenticationBase {
Mockito.any(HttpContext.class))).thenReturn(new BasicHeader(AUTH.WWW_AUTH_RESP, "stuff"));
authOptions.add(new AuthOption(authScheme1, this.credentials));
authOptions.add(new AuthOption(authScheme2, this.credentials));
this.authState.setAuthOptions(authOptions);
this.authState.update(authOptions);
this.interceptor.process(request, this.context);
@ -173,9 +172,7 @@ public class TestRequestAuthenticationBase {
public void testAuthSuccess() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.SUCCESS);
this.authState.setAuthScheme(this.authScheme);
this.authState.setCredentials(this.credentials);
this.authState.update(this.authScheme, this.credentials);
Mockito.when(this.authScheme.isConnectionBased()).thenReturn(Boolean.FALSE);
Mockito.when(this.authScheme.authenticate(
@ -198,9 +195,7 @@ public class TestRequestAuthenticationBase {
public void testAuthSuccessConnectionBased() throws Exception {
HttpRequest request = new BasicHttpRequest("GET", "/");
this.authState.setState(AuthProtocolState.SUCCESS);
this.authState.setAuthScheme(this.authScheme);
this.authState.setCredentials(this.credentials);
this.authState.update(this.authScheme, this.credentials);
Mockito.when(this.authScheme.isConnectionBased()).thenReturn(Boolean.TRUE);
Mockito.when(this.authScheme.authenticate(

View File

@ -85,8 +85,7 @@ public class TestRequestProxyAuthentication {
authscheme.processChallenge(challenge);
AuthState authstate = new AuthState();
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.update(authscheme, creds);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
@ -118,8 +117,7 @@ public class TestRequestProxyAuthentication {
authscheme.processChallenge(challenge);
AuthState authstate = new AuthState();
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.update(authscheme, creds);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
@ -151,8 +149,7 @@ public class TestRequestProxyAuthentication {
authscheme.processChallenge(challenge);
AuthState authstate = new AuthState();
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.update(authscheme, creds);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);
@ -239,9 +236,8 @@ public class TestRequestProxyAuthentication {
Credentials creds = new UsernamePasswordCredentials("user", "secret");
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.setState(AuthProtocolState.SUCCESS);
authstate.update(authscheme, creds);
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ClientContext.PROXY_AUTH_STATE, authstate);

View File

@ -70,8 +70,7 @@ public class TestRequestTargetAuthentication {
authscheme.processChallenge(challenge);
AuthState authstate = new AuthState();
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.update(authscheme, creds);
context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);
@ -93,8 +92,7 @@ public class TestRequestTargetAuthentication {
authscheme.processChallenge(challenge);
AuthState authstate = new AuthState();
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.update(authscheme, creds);
context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);
@ -117,8 +115,7 @@ public class TestRequestTargetAuthentication {
authscheme.processChallenge(challenge);
AuthState authstate = new AuthState();
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.update(authscheme, creds);
context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);
@ -178,9 +175,8 @@ public class TestRequestTargetAuthentication {
Credentials creds = new UsernamePasswordCredentials("user", "secret");
authstate.setAuthScheme(authscheme);
authstate.setCredentials(creds);
authstate.setState(AuthProtocolState.SUCCESS);
authstate.update(authscheme, creds);
context.setAttribute(ClientContext.TARGET_AUTH_STATE, authstate);

View File

@ -35,6 +35,8 @@ import org.apache.http.HttpVersion;
import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthProtocolState;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
@ -52,6 +54,7 @@ public class TestResponseAuthCache {
private HttpHost proxy;
private BasicScheme authscheme1;
private BasicScheme authscheme2;
private Credentials credentials;
private AuthState targetState;
private AuthState proxyState;
@ -62,6 +65,7 @@ public class TestResponseAuthCache {
this.authscheme1 = new BasicScheme();
this.authscheme2 = new BasicScheme();
this.credentials = new UsernamePasswordCredentials("user", "pwd");
this.targetState = new AuthState();
this.proxyState = new AuthState();
@ -91,10 +95,10 @@ public class TestResponseAuthCache {
new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm"));
this.targetState.setState(AuthProtocolState.CHALLENGED);
this.targetState.setAuthScheme(this.authscheme1);
this.targetState.update(this.authscheme1, this.credentials);
this.proxyState.setState(AuthProtocolState.CHALLENGED);
this.proxyState.setAuthScheme(this.authscheme2);
this.proxyState.update(this.authscheme2, this.credentials);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
@ -148,10 +152,10 @@ public class TestResponseAuthCache {
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
this.targetState.setState(AuthProtocolState.CHALLENGED);
this.targetState.setAuthScheme(this.authscheme1);
this.targetState.update(this.authscheme1, this.credentials);
this.proxyState.setState(AuthProtocolState.CHALLENGED);
this.proxyState.setAuthScheme(this.authscheme2);
this.proxyState.update(this.authscheme2, this.credentials);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
@ -176,10 +180,10 @@ public class TestResponseAuthCache {
new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm"));
this.targetState.setState(AuthProtocolState.UNCHALLENGED);
this.targetState.setAuthScheme(this.authscheme1);
this.targetState.update(this.authscheme1, this.credentials);
this.proxyState.setState(AuthProtocolState.UNCHALLENGED);
this.proxyState.setAuthScheme(this.authscheme2);
this.proxyState.update(this.authscheme2, this.credentials);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);
@ -206,10 +210,10 @@ public class TestResponseAuthCache {
new BasicHeader(AUTH.PROXY_AUTH, "BASIC realm=auth-realm"));
this.targetState.setState(AuthProtocolState.FAILURE);
this.targetState.setAuthScheme(this.authscheme1);
this.targetState.update(this.authscheme1, this.credentials);
this.proxyState.setState(AuthProtocolState.FAILURE);
this.proxyState.setAuthScheme(this.authscheme2);
this.proxyState.update(this.authscheme2, this.credentials);
HttpContext context = new BasicHttpContext();
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, this.target);

View File

@ -248,8 +248,7 @@ public class TestHttpAuthenticator {
TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.setAuthScheme(new BasicScheme());
this.authState.setCredentials(this.credentials);
this.authState.update(new BasicScheme(), this.credentials);
Assert.assertFalse(this.httpAuthenticator.authenticate(host,
response, authStrategy, this.authState, this.context));
@ -268,8 +267,7 @@ public class TestHttpAuthenticator {
TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.setAuthScheme(new DigestScheme());
this.authState.setCredentials(this.credentials);
this.authState.update(new DigestScheme(), this.credentials);
Assert.assertTrue(this.httpAuthenticator.authenticate(host,
response, authStrategy, this.authState, this.context));
@ -286,8 +284,7 @@ public class TestHttpAuthenticator {
TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
this.authState.setState(AuthProtocolState.CHALLENGED);
this.authState.setAuthScheme(new BasicScheme());
this.authState.setCredentials(this.credentials);
this.authState.update(new BasicScheme(), this.credentials);
Assert.assertTrue(this.httpAuthenticator.authenticate(host,
response, authStrategy, this.authState, this.context));