Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SessionPage.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import org.apache.wicket.PageParameters;
  20. import org.apache.wicket.markup.html.WebPage;
  21. import org.apache.wicket.protocol.http.WebRequest;
  22. import org.apache.wicket.protocol.http.WebResponse;
  23. import com.gitblit.Constants;
  24. import com.gitblit.Constants.AuthenticationType;
  25. import com.gitblit.Keys;
  26. import com.gitblit.models.UserModel;
  27. import com.gitblit.utils.StringUtils;
  28. import com.gitblit.wicket.GitBlitWebApp;
  29. import com.gitblit.wicket.GitBlitWebSession;
  30. public abstract class SessionPage extends WebPage {
  31. public SessionPage() {
  32. super();
  33. login();
  34. }
  35. public SessionPage(final PageParameters params) {
  36. super(params);
  37. login();
  38. }
  39. protected String [] getEncodings() {
  40. return app().settings().getStrings(Keys.web.blobEncodings).toArray(new String[0]);
  41. }
  42. protected GitBlitWebApp app() {
  43. return GitBlitWebApp.get();
  44. }
  45. private void login() {
  46. GitBlitWebSession session = GitBlitWebSession.get();
  47. HttpServletRequest request = ((WebRequest) getRequest()).getHttpServletRequest();
  48. HttpServletResponse response = ((WebResponse) getResponse()).getHttpServletResponse();
  49. // If using container/external servlet authentication, use request attribute
  50. String authedUser = (String) request.getAttribute(Constants.ATTRIB_AUTHUSER);
  51. // Default to trusting session authentication if not set in request by external processing
  52. if (StringUtils.isEmpty(authedUser) && session.isLoggedIn()) {
  53. authedUser = session.getUsername();
  54. }
  55. if (!StringUtils.isEmpty(authedUser)) {
  56. // Avoid session fixation for non-session authentication
  57. // If the authenticated user is different from the session user, discard
  58. // the old session entirely, without trusting any session values
  59. if (!authedUser.equals(session.getUsername())) {
  60. session.replaceSession();
  61. }
  62. if (!session.isSessionInvalidated()) {
  63. // Refresh usermodel to pick up any changes to permissions or roles (issue-186)
  64. UserModel user = app().users().getUserModel(authedUser);
  65. if (user == null || user.disabled) {
  66. // user was deleted/disabled during session
  67. app().authentication().logout(request, response, user);
  68. session.setUser(null);
  69. session.invalidateNow();
  70. return;
  71. }
  72. // validate cookie during session (issue-361)
  73. if (app().settings().getBoolean(Keys.web.allowCookieAuthentication, true)) {
  74. String requestCookie = app().authentication().getCookie(request);
  75. if (!StringUtils.isEmpty(requestCookie) && !StringUtils.isEmpty(user.cookie)) {
  76. if (!requestCookie.equals(user.cookie)) {
  77. // cookie was changed during our session
  78. app().authentication().logout(request, response, user);
  79. session.setUser(null);
  80. session.invalidateNow();
  81. return;
  82. }
  83. }
  84. }
  85. session.setUser(user);
  86. session.continueRequest();
  87. return;
  88. }
  89. }
  90. // try to authenticate by servlet request
  91. UserModel user = app().authentication().authenticate(request);
  92. // Login the user
  93. if (user != null) {
  94. AuthenticationType authenticationType = (AuthenticationType) request.getAttribute(Constants.ATTRIB_AUTHTYPE);
  95. // issue 62: fix session fixation vulnerability
  96. // but only if authentication was done in the container.
  97. // It avoid double change of session, that some authentication method
  98. // don't like
  99. if (AuthenticationType.CONTAINER != authenticationType) {
  100. session.replaceSession();
  101. }
  102. session.setUser(user);
  103. // Set Cookie
  104. app().authentication().setCookie(request, response, user);
  105. session.continueRequest();
  106. }
  107. }
  108. }