You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthenticationError.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import org.sonar.api.utils.log.Logger;
  24. import org.sonar.api.utils.log.Loggers;
  25. import org.sonar.server.authentication.event.AuthenticationException;
  26. import static org.sonar.server.authentication.AuthenticationRedirection.encodeMessage;
  27. import static org.sonar.server.authentication.AuthenticationRedirection.redirectTo;
  28. import static org.sonar.server.authentication.Cookies.newCookieBuilder;
  29. public final class AuthenticationError {
  30. private static final String UNAUTHORIZED_PATH = "/sessions/unauthorized";
  31. private static final Logger LOGGER = Loggers.get(AuthenticationError.class);
  32. private static final String AUTHENTICATION_ERROR_COOKIE = "AUTHENTICATION-ERROR";
  33. private static final int FIVE_MINUTES_IN_SECONDS = 5 * 60;
  34. private AuthenticationError() {
  35. // Utility class
  36. }
  37. static void handleError(Exception e, HttpServletRequest request, HttpServletResponse response, String message) {
  38. LOGGER.warn(message, e);
  39. redirectToUnauthorized(request, response);
  40. }
  41. static void handleError(HttpServletRequest request, HttpServletResponse response, String message) {
  42. LOGGER.warn(message);
  43. redirectToUnauthorized(request, response);
  44. }
  45. static void handleAuthenticationError(AuthenticationException e, HttpServletRequest request, HttpServletResponse response) {
  46. String publicMessage = e.getPublicMessage();
  47. if (publicMessage != null && !publicMessage.isEmpty()) {
  48. addErrorCookie(request, response, publicMessage);
  49. }
  50. redirectToUnauthorized(request, response);
  51. }
  52. public static void addErrorCookie(HttpServletRequest request, HttpServletResponse response, String value) {
  53. response.addCookie(newCookieBuilder(request)
  54. .setName(AUTHENTICATION_ERROR_COOKIE)
  55. .setValue(encodeMessage(value))
  56. .setHttpOnly(false)
  57. .setExpiry(FIVE_MINUTES_IN_SECONDS)
  58. .build());
  59. }
  60. private static void redirectToUnauthorized(HttpServletRequest request, HttpServletResponse response) {
  61. redirectTo(response, request.getContextPath() + UNAUTHORIZED_PATH);
  62. }
  63. }