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 4.9KB

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