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.5KB

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