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.

InitFilter.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.authentication;
  21. import javax.servlet.FilterChain;
  22. import javax.servlet.FilterConfig;
  23. import javax.servlet.ServletRequest;
  24. import javax.servlet.ServletResponse;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27. import org.sonar.api.server.authentication.BaseIdentityProvider;
  28. import org.sonar.api.server.authentication.IdentityProvider;
  29. import org.sonar.api.server.authentication.OAuth2IdentityProvider;
  30. import org.sonar.api.server.authentication.UnauthorizedException;
  31. import org.sonar.server.authentication.event.AuthenticationEvent;
  32. import org.sonar.server.authentication.event.AuthenticationException;
  33. import static java.lang.String.format;
  34. import static org.sonar.server.authentication.AuthenticationError.handleAuthenticationError;
  35. import static org.sonar.server.authentication.AuthenticationError.handleError;
  36. import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
  37. public class InitFilter extends AuthenticationFilter {
  38. private static final String INIT_CONTEXT = "/sessions/init/";
  39. private final BaseContextFactory baseContextFactory;
  40. private final OAuth2ContextFactory oAuth2ContextFactory;
  41. private final AuthenticationEvent authenticationEvent;
  42. private final OAuth2AuthenticationParameters oAuthOAuth2AuthenticationParameters;
  43. public InitFilter(IdentityProviderRepository identityProviderRepository, BaseContextFactory baseContextFactory,
  44. OAuth2ContextFactory oAuth2ContextFactory, AuthenticationEvent authenticationEvent, OAuth2AuthenticationParameters oAuthOAuth2AuthenticationParameters) {
  45. super(identityProviderRepository);
  46. this.baseContextFactory = baseContextFactory;
  47. this.oAuth2ContextFactory = oAuth2ContextFactory;
  48. this.authenticationEvent = authenticationEvent;
  49. this.oAuthOAuth2AuthenticationParameters = oAuthOAuth2AuthenticationParameters;
  50. }
  51. @Override
  52. public UrlPattern doGetPattern() {
  53. return UrlPattern.create(INIT_CONTEXT + "*");
  54. }
  55. @Override
  56. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
  57. HttpServletRequest httpRequest = (HttpServletRequest) request;
  58. HttpServletResponse httpResponse = (HttpServletResponse) response;
  59. IdentityProvider provider = resolveProviderOrHandleResponse(httpRequest, httpResponse, INIT_CONTEXT);
  60. if (provider != null) {
  61. handleProvider(httpRequest, httpResponse, provider);
  62. }
  63. }
  64. private void handleProvider(HttpServletRequest request, HttpServletResponse response, IdentityProvider provider) {
  65. try {
  66. if (provider instanceof BaseIdentityProvider) {
  67. handleBaseIdentityProvider(request, response, (BaseIdentityProvider) provider);
  68. } else if (provider instanceof OAuth2IdentityProvider) {
  69. oAuthOAuth2AuthenticationParameters.init(request, response);
  70. handleOAuth2IdentityProvider(request, response, (OAuth2IdentityProvider) provider);
  71. } else {
  72. handleError(request, response, format("Unsupported IdentityProvider class: %s", provider.getClass()));
  73. }
  74. } catch (AuthenticationException e) {
  75. oAuthOAuth2AuthenticationParameters.delete(request, response);
  76. authenticationEvent.loginFailure(request, e);
  77. handleAuthenticationError(e, request, response);
  78. } catch (Exception e) {
  79. oAuthOAuth2AuthenticationParameters.delete(request, response);
  80. handleError(e, request, response, format("Fail to initialize authentication with provider '%s'", provider.getKey()));
  81. }
  82. }
  83. private void handleBaseIdentityProvider(HttpServletRequest request, HttpServletResponse response, BaseIdentityProvider provider) {
  84. try {
  85. provider.init(baseContextFactory.newContext(request, response, provider));
  86. } catch (UnauthorizedException e) {
  87. throw AuthenticationException.newBuilder()
  88. .setSource(Source.external(provider))
  89. .setMessage(e.getMessage())
  90. .setPublicMessage(e.getMessage())
  91. .build();
  92. }
  93. }
  94. private void handleOAuth2IdentityProvider(HttpServletRequest request, HttpServletResponse response, OAuth2IdentityProvider provider) {
  95. try {
  96. provider.init(oAuth2ContextFactory.newContext(request, response, provider));
  97. } catch (UnauthorizedException e) {
  98. throw AuthenticationException.newBuilder()
  99. .setSource(Source.oauth2(provider))
  100. .setMessage(e.getMessage())
  101. .setPublicMessage(e.getMessage())
  102. .build();
  103. }
  104. }
  105. @Override
  106. public void init(FilterConfig filterConfig) {
  107. // Nothing to do
  108. }
  109. @Override
  110. public void destroy() {
  111. // Nothing to do
  112. }
  113. }