{
if (!_prefs.getRequired()) {
return true;
}
Realm realm = getRealm();
Session session = context.getSession();
if (realm == null) {
context.log().error("Couldn't get realm named '"+_prefs.getRealm()+"'");
try {
String contentType = MimeTypes.guessContentType(context.getRequest());
Templates.message(context, contentType, 500);
} catch(IOException e) {
context.log().error("Error while writing '500 Internal Server Error' response", e);
}
return false;
}
if (session == null) {
//cannot authorize if there's no session
return false;
}
String citizenName = session.getCitizen();
Citizen citizen = null;
if (citizenName != null) {
citizen = realm.getCitizen(citizenName);
}
if (citizen != null && citizen.getRealm().equals(realm)) {
return true;
} else {
HttpServletRequest request = context.getRequest();
String clientIp = request.getRemoteAddr();
context.log().info("client ip: "+clientIp);
Citizen[] searchResult = realm.searchCitizenByVariable("ctz.ip", clientIp);
if (searchResult != null && searchResult.length > 0) {
context.setCitizen(searchResult[0]);
context.log().info("web: ipauthentication ok");
if (context.getOriginalPathinfo().equals(loginPath)) {
throw new RedirectException(context.getSession().getId(), forwardPath);
}
return true;
}
String username = request.getParameter("webauth.username");
String password = request.getParameter("webauth.password");
context.getSession().removeAttribute("webauth.failedUser");
if (username != null && password != null && username.length() > 0) {
citizen = realm.getCitizen(username);
context.log().info("username: '"+username+"' citizen: "+citizen);
if (citizen != null && citizen.verifyCredentials(password)) {
context.setCitizen(citizen);
context.log().info("web: authentication ok");