if (isAuthMandatory) {
response.addHeader(WWW_AUTHENTICATE, "Negotiate");
try {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
} catch (IOException e) {
throw new ServerAuthException(e);
}
return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null, false);
}
return new AuthResult(TomcatAuthStatus.SUCCESS, unauthenticatedIdentity, false);
}
// Validate any credentials already included with this request
String username = null;
String password = null;
authorization.toBytes();
ByteChunk authorizationBC = authorization.getByteChunk();
if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { // Basic authorization
authorizationBC.setOffset(authorizationBC.getOffset() + 6);
// FIXME: Add trimming
// authorizationBC.trim();
CharChunk authorizationCC = authorization.getCharChunk();
Base64.decode(authorizationBC, authorizationCC);
// Get username and password
int colon = authorizationCC.indexOf(':');
if (colon < 0) {
username = authorizationCC.toString();
} else {
char[] buf = authorizationCC.getBuffer();
username = new String(buf, 0, colon);
password = new String(buf, colon + 1, authorizationCC.getEnd() - colon - 1);
}
authorizationBC.setOffset(authorizationBC.getOffset() - 6);
} else if (authorizationBC.startsWithIgnoreCase("negotiate ", 0)) { // Spnego authorization
authorizationBC.setOffset(authorizationBC.getOffset() + 10);
username = authorizationBC.toString();
authorizationBC.setOffset(authorizationBC.getOffset() - 10);
}
UserIdentity userIdentity = loginService.login(username, password);
if (userIdentity != null) {
return new AuthResult(TomcatAuthStatus.SUCCESS, userIdentity, false);
}
// Send an "unauthorized" response and an appropriate challenge (BASIC)
if (isAuthMandatory) {
try {
StringBuilder authenticateCC = new StringBuilder();
authenticateCC.append("Basic realm=\"");
if (realmName == null) {
authenticateCC.append(request.getServerName());
authenticateCC.append(':');
authenticateCC.append(Integer.toString(request.getServerPort()));
} else {
authenticateCC.append(realmName);
}
authenticateCC.append('\"');
response.addHeader(WWW_AUTHENTICATE, authenticateCC.toString());
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null, false);
} catch (IOException e) {
throw new ServerAuthException(e);
}
}
return new AuthResult(TomcatAuthStatus.SUCCESS, unauthenticatedIdentity, false);
}