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.

SyndicationFilter.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2011 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;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import javax.servlet.FilterChain;
  20. import javax.servlet.ServletException;
  21. import javax.servlet.ServletRequest;
  22. import javax.servlet.ServletResponse;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import com.gitblit.Constants.AccessRestrictionType;
  26. import com.gitblit.manager.IProjectManager;
  27. import com.gitblit.manager.IRepositoryManager;
  28. import com.gitblit.manager.IRuntimeManager;
  29. import com.gitblit.models.ProjectModel;
  30. import com.gitblit.models.RepositoryModel;
  31. import com.gitblit.models.UserModel;
  32. /**
  33. * The SyndicationFilter is an AuthenticationFilter which ensures that feed
  34. * requests for projects or view-restricted repositories have proper authentication
  35. * credentials and are authorized for the requested feed.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public class SyndicationFilter extends AuthenticationFilter {
  41. /**
  42. * Extract the repository name from the url.
  43. *
  44. * @param url
  45. * @return repository name
  46. */
  47. protected String extractRequestedName(String url) {
  48. if (url.indexOf('?') > -1) {
  49. return url.substring(0, url.indexOf('?'));
  50. }
  51. return url;
  52. }
  53. /**
  54. * doFilter does the actual work of preprocessing the request to ensure that
  55. * the user may proceed.
  56. *
  57. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
  58. * javax.servlet.ServletResponse, javax.servlet.FilterChain)
  59. */
  60. @Override
  61. public void doFilter(final ServletRequest request, final ServletResponse response,
  62. final FilterChain chain) throws IOException, ServletException {
  63. HttpServletRequest httpRequest = (HttpServletRequest) request;
  64. HttpServletResponse httpResponse = (HttpServletResponse) response;
  65. String fullUrl = getFullUrl(httpRequest);
  66. String name = extractRequestedName(fullUrl);
  67. IRuntimeManager runtimeManager = GitBlit.getManager(IRuntimeManager.class);
  68. IRepositoryManager repositoryManager = GitBlit.getManager(IRepositoryManager.class);
  69. IProjectManager projectManager = GitBlit.getManager(IProjectManager.class);
  70. ProjectModel project = projectManager.getProjectModel(name);
  71. RepositoryModel model = null;
  72. if (project == null) {
  73. // try loading a repository model
  74. model = repositoryManager.getRepositoryModel(name);
  75. if (model == null) {
  76. // repository not found. send 404.
  77. logger.info(MessageFormat.format("ARF: {0} ({1})", fullUrl,
  78. HttpServletResponse.SC_NOT_FOUND));
  79. httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
  80. return;
  81. }
  82. }
  83. // Wrap the HttpServletRequest with the AccessRestrictionRequest which
  84. // overrides the servlet container user principal methods.
  85. // JGit requires either:
  86. //
  87. // 1. servlet container authenticated user
  88. // 2. http.receivepack = true in each repository's config
  89. //
  90. // Gitblit must conditionally authenticate users per-repository so just
  91. // enabling http.receivepack is insufficient.
  92. AuthenticatedRequest authenticatedRequest = new AuthenticatedRequest(httpRequest);
  93. UserModel user = getUser(httpRequest);
  94. if (user != null) {
  95. authenticatedRequest.setUser(user);
  96. }
  97. // BASIC authentication challenge and response processing
  98. if (model != null) {
  99. if (model.accessRestriction.atLeast(AccessRestrictionType.VIEW)) {
  100. if (user == null) {
  101. // challenge client to provide credentials. send 401.
  102. if (runtimeManager.isDebugMode()) {
  103. logger.info(MessageFormat.format("ARF: CHALLENGE {0}", fullUrl));
  104. }
  105. httpResponse.setHeader("WWW-Authenticate", CHALLENGE);
  106. httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
  107. return;
  108. } else {
  109. // check user access for request
  110. if (user.canView(model)) {
  111. // authenticated request permitted.
  112. // pass processing to the restricted servlet.
  113. newSession(authenticatedRequest, httpResponse);
  114. logger.info(MessageFormat.format("ARF: {0} ({1}) authenticated", fullUrl,
  115. HttpServletResponse.SC_CONTINUE));
  116. chain.doFilter(authenticatedRequest, httpResponse);
  117. return;
  118. }
  119. // valid user, but not for requested access. send 403.
  120. if (runtimeManager.isDebugMode()) {
  121. logger.info(MessageFormat.format("ARF: {0} forbidden to access {1}",
  122. user.username, fullUrl));
  123. }
  124. httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
  125. return;
  126. }
  127. }
  128. }
  129. if (runtimeManager.isDebugMode()) {
  130. logger.info(MessageFormat.format("ARF: {0} ({1}) unauthenticated", fullUrl,
  131. HttpServletResponse.SC_CONTINUE));
  132. }
  133. // unauthenticated request permitted.
  134. // pass processing to the restricted servlet.
  135. chain.doFilter(authenticatedRequest, httpResponse);
  136. }
  137. }