protected GitBlitWebApp app() {
return GitBlitWebApp.get();
}
private void login() {
GitBlitWebSession session = GitBlitWebSession.get();
if (session.isLoggedIn() && !session.isSessionInvalidated()) {
// already have a session, refresh usermodel to pick up
// any changes to permissions or roles (issue-186)
UserModel user = app().users().getUserModel(session.getUser().username);
if (user == null || user.disabled) {
// user was deleted/disabled during session
HttpServletResponse response = ((WebResponse) getRequestCycle().getResponse())
.getHttpServletResponse();
app().authentication().logout(response, user);
session.setUser(null);
session.invalidateNow();
return;
}
// validate cookie during session (issue-361)
if (user != null && app().settings().getBoolean(Keys.web.allowCookieAuthentication, true)) {
HttpServletRequest request = ((WebRequest) getRequestCycle().getRequest())
.getHttpServletRequest();
String requestCookie = app().authentication().getCookie(request);
if (!StringUtils.isEmpty(requestCookie) && !StringUtils.isEmpty(user.cookie)) {
if (!requestCookie.equals(user.cookie)) {
// cookie was changed during our session
HttpServletResponse response = ((WebResponse) getRequestCycle().getResponse())
.getHttpServletResponse();
app().authentication().logout(response, user);
session.setUser(null);
session.invalidateNow();
return;
}
}
}
session.setUser(user);
return;
}
// try to authenticate by servlet request
HttpServletRequest httpRequest = ((WebRequest) getRequestCycle().getRequest())
.getHttpServletRequest();
UserModel user = app().authentication().authenticate(httpRequest);
// Login the user
if (user != null) {
// issue 62: fix session fixation vulnerability
session.replaceSession();
session.setUser(user);
// Set Cookie
WebResponse response = (WebResponse) getRequestCycle().getResponse();
app().authentication().setCookie(response.getHttpServletResponse(), user);
session.continueRequest();
}
}