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.

EnforceAuthenticationFilter.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2013 Laurens Vrijnsen
  3. * Copyright 2013 gitblit.com.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */package com.gitblit.servlet;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import javax.inject.Inject;
  20. import javax.inject.Singleton;
  21. import javax.servlet.Filter;
  22. import javax.servlet.FilterChain;
  23. import javax.servlet.FilterConfig;
  24. import javax.servlet.ServletException;
  25. import javax.servlet.ServletRequest;
  26. import javax.servlet.ServletResponse;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import com.gitblit.IStoredSettings;
  32. import com.gitblit.Keys;
  33. import com.gitblit.Keys.web;
  34. import com.gitblit.manager.IRuntimeManager;
  35. import com.gitblit.manager.IAuthenticationManager;
  36. import com.gitblit.models.UserModel;
  37. /**
  38. * This filter enforces authentication via HTTP Basic Authentication, if the settings indicate so.
  39. * It looks at the settings "web.authenticateViewPages" and "web.enforceHttpBasicAuthentication"; if
  40. * both are true, any unauthorized access will be met with a HTTP Basic Authentication header.
  41. *
  42. * @author Laurens Vrijnsen
  43. *
  44. */
  45. @Singleton
  46. public class EnforceAuthenticationFilter implements Filter {
  47. protected transient Logger logger = LoggerFactory.getLogger(getClass());
  48. private final IStoredSettings settings;
  49. private final IAuthenticationManager authenticationManager;
  50. @Inject
  51. public EnforceAuthenticationFilter(
  52. IRuntimeManager runtimeManager,
  53. IAuthenticationManager authenticationManager) {
  54. super();
  55. this.settings = runtimeManager.getSettings();
  56. this.authenticationManager = authenticationManager;
  57. }
  58. /*
  59. * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
  60. */
  61. @Override
  62. public void init(FilterConfig filterConfig) throws ServletException {
  63. }
  64. /*
  65. * This does the actual filtering: is the user authenticated? If not, enforce HTTP authentication (401)
  66. *
  67. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
  68. */
  69. @Override
  70. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  71. Boolean mustForceAuth = settings.getBoolean(Keys.web.authenticateViewPages, false)
  72. && settings.getBoolean(Keys.web.enforceHttpBasicAuthentication, false);
  73. HttpServletRequest httpRequest = (HttpServletRequest) request;
  74. HttpServletResponse httpResponse = (HttpServletResponse) response;
  75. UserModel user = authenticationManager.authenticate(httpRequest);
  76. if (mustForceAuth && (user == null)) {
  77. // not authenticated, enforce now:
  78. logger.debug(MessageFormat.format("EnforceAuthFilter: user not authenticated for URL {0}!", request.toString()));
  79. String challenge = MessageFormat.format("Basic realm=\"{0}\"", settings.getString(Keys.web.siteName, ""));
  80. httpResponse.setHeader("WWW-Authenticate", challenge);
  81. httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
  82. return;
  83. } else {
  84. // user is authenticated, or don't care, continue handling
  85. chain.doFilter(request, response);
  86. }
  87. }
  88. /*
  89. * @see javax.servlet.Filter#destroy()
  90. */
  91. @Override
  92. public void destroy() {
  93. }
  94. }