PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugin/src/main/java/com/atlassian/plugin/remotable/plugin/module/oauth/OAuth2LOFilter.java

https://bitbucket.org/rodogu/remotable-plugins
Java | 172 lines | 137 code | 22 blank | 13 comment | 15 complexity | d3cdd04fe98f0e46226b6e0c77e8e0b5 MD5 | raw file
  1. package com.atlassian.plugin.remotable.plugin.module.oauth;
  2. import com.atlassian.plugin.remotable.plugin.product.WebSudoElevator;
  3. import com.atlassian.oauth.util.Check;
  4. import com.atlassian.sal.api.ApplicationProperties;
  5. import com.atlassian.sal.api.auth.AuthenticationController;
  6. import com.atlassian.sal.api.auth.AuthenticationListener;
  7. import com.atlassian.sal.api.auth.Authenticator;
  8. import com.google.common.collect.ImmutableSet;
  9. import net.oauth.OAuth;
  10. import net.oauth.server.HttpRequestMessage;
  11. import org.springframework.osgi.service.importer.ServiceProxyDestroyedException;
  12. import javax.servlet.*;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import java.io.IOException;
  16. import java.net.URI;
  17. import java.util.Set;
  18. import static net.oauth.OAuth.*;
  19. public class OAuth2LOFilter implements Filter
  20. {
  21. public static final String USER_ID = "user_id";
  22. private static final Set<String> OAUTH_DATA_REQUEST_PARAMS = ImmutableSet.of(OAUTH_CONSUMER_KEY,
  23. OAUTH_SIGNATURE_METHOD,
  24. OAUTH_SIGNATURE,
  25. OAUTH_TIMESTAMP,
  26. OAUTH_NONCE);
  27. private final Authenticator authenticator;
  28. private final AuthenticationListener authenticationListener;
  29. private final AuthenticationController authenticationController;
  30. private final WebSudoElevator webSudoElevator;
  31. private final ApplicationProperties applicationProperties;
  32. public OAuth2LOFilter(Authenticator authenticator,
  33. AuthenticationListener authenticationListener,
  34. AuthenticationController authenticationController,
  35. WebSudoElevator webSudoElevator,
  36. ApplicationProperties applicationProperties)
  37. {
  38. this.webSudoElevator = Check.notNull(webSudoElevator, "webSudoElevator");
  39. this.authenticator = Check.notNull(authenticator, "authenticator");
  40. this.authenticationListener = Check.notNull(authenticationListener, "authenticationListener");
  41. this.authenticationController = Check.notNull(authenticationController, "authenticationController");
  42. this.applicationProperties = Check.notNull(applicationProperties, "applicationProperties");
  43. }
  44. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
  45. {
  46. final HttpServletRequest request = (HttpServletRequest) req;
  47. final HttpServletResponse response = (HttpServletResponse) res;
  48. try
  49. {
  50. if (!mayProceed(request, response))
  51. {
  52. return;
  53. }
  54. }
  55. catch (ServiceProxyDestroyedException ex)
  56. {
  57. // ignore this exception as it only happens if the plugin has been shutdown while
  58. // the request has been in this filter
  59. }
  60. try
  61. {
  62. chain.doFilter(request, response);
  63. }
  64. finally
  65. {
  66. if (isOAuth2LOAccessAttempt(request) && request.getSession(false) != null)
  67. {
  68. request.getSession().invalidate();
  69. }
  70. }
  71. }
  72. boolean mayProceed(HttpServletRequest request, HttpServletResponse response)
  73. {
  74. // is it a protected resource? if not, we don't care
  75. if (!authenticationController.shouldAttemptAuthentication(request))
  76. {
  77. authenticationListener.authenticationNotAttempted(request, response);
  78. return true;
  79. }
  80. // are the oauth parameters present?
  81. if (!isOAuth2LOAccessAttempt(request))
  82. {
  83. // if the oauth parameters aren't present, we allow the filter chain to continue being processed,
  84. // but we want to add the WWW-Authenticate header
  85. authenticationListener.authenticationNotAttempted(request, response);
  86. return true;
  87. }
  88. final Authenticator.Result result = authenticator.authenticate(request, response);
  89. if (result.getStatus() == Authenticator.Result.Status.FAILED)
  90. {
  91. authenticationListener.authenticationFailure(result, request, response);
  92. return false;
  93. }
  94. if (result.getStatus() == Authenticator.Result.Status.ERROR)
  95. {
  96. authenticationListener.authenticationError(result, request, response);
  97. return false;
  98. }
  99. // can only mark the request as successfully authenticated if the user is a real one
  100. if (result.getStatus() == Authenticator.Result.Status.SUCCESS)
  101. {
  102. authenticationListener.authenticationSuccess(result, request, response);
  103. webSudoElevator.startWebSudoSession(request, response);
  104. }
  105. //markAsOAuthRequest(request);
  106. return true;
  107. }
  108. /**
  109. * We're trying to access an OAuth protected resource if all the OAuth parameters are set and we aren't trying to
  110. * turn a request token into an access token (which is the only other time all the OAuth parameters are in the
  111. * request).
  112. */
  113. private boolean isOAuth2LOAccessAttempt(HttpServletRequest request)
  114. {
  115. final Set<String> names = parameterNames(request);
  116. return names.containsAll(OAUTH_DATA_REQUEST_PARAMS) &&
  117. !names.contains(OAuth.OAUTH_TOKEN) &&
  118. !isTokenRequest(request) &&
  119. !isDownloadableResourceRequest(request);
  120. }
  121. private boolean isDownloadableResourceRequest(HttpServletRequest request)
  122. {
  123. return request.getRequestURI().startsWith(getContextPath(request) + "/download/resources/");
  124. }
  125. private boolean isTokenRequest(HttpServletRequest request)
  126. {
  127. return request.getRequestURL().toString().endsWith("/plugins/servlet/oauth/request-token");
  128. }
  129. private String getContextPath(HttpServletRequest request)
  130. {
  131. final String baseUrl = applicationProperties.getBaseUrl();
  132. if (baseUrl == null)
  133. {
  134. return request.getContextPath();
  135. }
  136. return URI.create(baseUrl).getPath();
  137. }
  138. private Set<String> parameterNames(HttpServletRequest request)
  139. {
  140. final ImmutableSet.Builder<String> names = ImmutableSet.builder();
  141. for (OAuth.Parameter parameter : HttpRequestMessage.getParameters(request))
  142. {
  143. names.add(parameter.getKey());
  144. }
  145. return names.build();
  146. }
  147. public void init(FilterConfig filterConfig) throws ServletException
  148. {}
  149. public void destroy() {}
  150. }