ByteArrayInputStream authTokenIS = new ByteArrayInputStream(authToken);
MessageTrace.logRequestBase64(authTokenBase64);
MessageTrace.logRequestHex(authToken);
Session session = request.getSessionInternal();
NegotiationContext negotiationContext = (NegotiationContext) session.getNote(NEGOTIATION_CONTEXT);
if (negotiationContext == null)
{
log.debug("Creating new NegotiationContext");
{
negotiationContext = new NegotiationContext();
session.setNote(NEGOTIATION_CONTEXT, negotiationContext);
}
}
String username = session.getId();
String authenticationMethod = "";
try
{
// Set the ThreadLocal association.
negotiationContext.associate();
MessageFactory mf = MessageFactory.newInstance();
if (mf.accepts(authTokenIS) == false)
{
throw new IOException("Unsupported negotiation mechanism.");
}
NegotiationMessage requestMessage = mf.createMessage(authTokenIS);
negotiationContext.setRequestMessage(requestMessage);
Realm realm = context.getRealm();
principal = realm.authenticate(username, (String) null);
authenticationMethod = negotiationContext.getAuthenticationMethod();
if (log.isDebugEnabled() && principal != null)
log.debug("authenticated principal = " + principal);
NegotiationMessage responseMessage = negotiationContext.getResponseMessage();
if (responseMessage != null)
{
ByteArrayOutputStream responseMessageOS = new ByteArrayOutputStream();
responseMessage.writeTo(responseMessageOS, true);
String responseHeader = responseMessageOS.toString();
MessageTrace.logResponseBase64(responseHeader);
response.setHeader("WWW-Authenticate", negotiateScheme + " " + responseHeader);
}
}
catch (NegotiationException e)
{
IOException ioe = new IOException("Error processing " + negotiateScheme + " header.");
ioe.initCause(e);
throw ioe;
}
finally
{
// Clear the headers and remove the ThreadLocal association.
negotiationContext.clear();
}
if (principal == null)
{
response.sendError(Response.SC_UNAUTHORIZED);