case HTTP.STATUS_PROXY_AUTHENTICATION:
// Disallow proxy auth if no proxy or through a tunnel
if (!isHTTPProxy() || m_nTunnelStatus == TUNNEL_ESTABLISHED)
{
throw new LoginException("Proxy authentication disallowed to non-proxy");
}
m_currentAuthentication = m_proxyAuthentication;
break;
case HTTP.STATUS_FORBIDDEN:
if (s_logger.isDebugEnabled())
{
s_logger.debug("Authentication error " +
m_nResponseStatus + ": " + m_sResponseMessage);
}
m_bAuthDone = false;
if (m_currentAuthentication.getProvider() == null || m_currentAuthentication.getProvider().isAuthenticationDeterministic())
{
return true;
}
if (m_currentAuthentication.getSPNEGO() == SPNEGO_SILENT)
{
m_currentAuthentication.setSPNEGO(SPNEGO_CRED);
m_nBasicCount = 0;
m_sToken = null;
if (m_authenticator != null)
{
m_authenticator.dispose();
m_authenticator = null;
}
resetAuth();
return false;
}
break;
default:
completeAuthentication();
return true;
}
if (m_nBasicCount < 0 && m_currentAuthentication.getUserSaved() == null)
{
m_nBasicCount = 0;
}
if (m_bAuthDone)
{
if (s_logger.isDebugEnabled())
{
s_logger.debug("Authentication error " +
m_nResponseStatus + ": " + m_sResponseMessage);
}
}
int nProtocol = m_currentAuthentication.parseAuthProtocol();
while (nProtocol == AUTH_SPNEGO)
{
if (m_authenticator == null)
{
try
{
m_authenticator = AuthenticatorFactory.create();
}
catch (Exception e)
{
m_serverAuthentication.setSPNEGO(SPNEGO_NONE);
m_proxyAuthentication.setSPNEGO(SPNEGO_NONE);
nProtocol = m_currentAuthentication.parseAuthProtocol();
}
}
if (m_authenticator != null)
{
try
{
byte[] token;
if (m_sToken == null)
{
if (m_currentAuthentication.getSPNEGO() == SPNEGO_SILENT && !m_bAuthDone)
{
// Use mutual authentication due to bug #6733095 in Sun's JRE: http://bugs.sun.com/view_bug.do?bug_id=6733095
m_authenticator.init(Authenticator.PROTOCOL_SPNEGO, Authenticator.MODE_MUTUAL, m_currentAuthentication.getServiceName(), null, null, null);
}
else
{
m_currentAuthentication.setSPNEGO(SPNEGO_CRED);
m_bAuthDone = false;
if (m_nBasicCount < 0)
{
m_currentAuthentication.copyFromSaved();
}
else
{
m_currentAuthentication.clearUserPassword();
}
if (m_nBasicCount++ == MAX_BASIC_ATTEMPTS || !retrievePassword())
{
throw new IOException("Too many authentication attempts");
}
// Use mutual authentication due to bug #6733095 in Sun's JRE: http://bugs.sun.com/view_bug.do?bug_id=6733095
m_authenticator.init(Authenticator.PROTOCOL_SPNEGO, Authenticator.MODE_MUTUAL, m_currentAuthentication.getServiceName(), null, m_currentAuthentication.getUser(), m_currentAuthentication.getPassword());
}
if (s_logger.isDebugEnabled())
{
s_logger.debug("Starting authentication sequence with service \"" + m_currentAuthentication.getServiceName() +
((m_currentAuthentication.getUser() == null) ? "\"" : "\", user \"" + m_currentAuthentication.getUser() + "\""));
}
token = m_authenticator.nextToken(null);
}
else
{
token = Base64Util.decode(m_sToken);
if (s_logger.isDebugEnabled())
{
s_logger.debug("Authentication response token " + new Binary(token));
}
token = m_authenticator.nextToken(token);
}
resetAuth();
if (token != null)
{
if (s_logger.isDebugEnabled())
{
s_logger.debug("Authentication request token " + new Binary(token));
}
setAuthToken(AUTH_SPNEGO, token);
}
if (m_authenticator.isDone())
{
m_authenticator.dispose();
m_authenticator = null;
m_bAuthDone = true;
}
return false;
}
catch (CancellationException e)
{
throw e;
}
catch (Exception e)
{
s_logger.debug("Authentication error", e);
if (m_currentAuthentication.getSPNEGO() == SPNEGO_SILENT &&
e instanceof LoginException)
{
m_currentAuthentication.setSPNEGO(SPNEGO_CRED);
}
else
{
m_currentAuthentication.setSPNEGO(SPNEGO_NONE);
nProtocol = m_currentAuthentication.parseAuthProtocol();
m_authenticator.dispose();
m_authenticator = null;
}
m_nBasicCount = 0;
m_sToken = null;
}
}
}
if (nProtocol == AUTH_BASIC)
{
if (m_nBasicCount < 0)
{
m_currentAuthentication.copyFromSaved();
}
if (m_nBasicCount++ == MAX_BASIC_ATTEMPTS || !retrievePassword())
{
m_authHeader = null;
throw new LoginException();
}
StringBuffer buf = new StringBuffer(32);
if (s_logger.isDebugEnabled())
{
s_logger.debug("Attempting basic authentication for user \"" + m_currentAuthentication.getUser() + "\"");
}
if (m_currentAuthentication.getUser() != null)
{
buf.append(m_currentAuthentication.getUser());
}
buf.append(':');
if (m_currentAuthentication.getPassword() != null)
{
buf.append(m_currentAuthentication.getPassword());
}
byte[] token = buf.toString().getBytes("UTF-8");
for (int i = 0; i < buf.length(); ++i)
{
buf.setCharAt(i, ' ');
}
m_currentAuthentication.setUser(null);
resetAuth();
setAuthToken(AUTH_BASIC, token);
Arrays.fill(token, (byte)0);
return false;
}
throw new LoginException();
}