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.

OAuth2ContextFactory.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 java.io.IOException;
  22. import java.util.Optional;
  23. import javax.servlet.http.HttpServletRequest;
  24. import javax.servlet.http.HttpServletResponse;
  25. import org.sonar.api.platform.Server;
  26. import org.sonar.api.server.ServerSide;
  27. import org.sonar.api.server.authentication.OAuth2IdentityProvider;
  28. import org.sonar.api.server.authentication.UserIdentity;
  29. import org.sonar.db.user.UserDto;
  30. import org.sonar.server.authentication.UserRegistration.ExistingEmailStrategy;
  31. import org.sonar.server.authentication.UserRegistration.UpdateLoginStrategy;
  32. import org.sonar.server.authentication.event.AuthenticationEvent;
  33. import org.sonar.server.user.ThreadLocalUserSession;
  34. import org.sonar.server.user.UserSessionFactory;
  35. import static java.lang.String.format;
  36. import static org.sonar.server.authentication.OAuth2CallbackFilter.CALLBACK_PATH;
  37. @ServerSide
  38. public class OAuth2ContextFactory {
  39. private final ThreadLocalUserSession threadLocalUserSession;
  40. private final UserRegistrar userRegistrar;
  41. private final Server server;
  42. private final OAuthCsrfVerifier csrfVerifier;
  43. private final JwtHttpHandler jwtHttpHandler;
  44. private final UserSessionFactory userSessionFactory;
  45. private final OAuth2AuthenticationParameters oAuthParameters;
  46. public OAuth2ContextFactory(ThreadLocalUserSession threadLocalUserSession, UserRegistrar userRegistrar, Server server,
  47. OAuthCsrfVerifier csrfVerifier, JwtHttpHandler jwtHttpHandler, UserSessionFactory userSessionFactory, OAuth2AuthenticationParameters oAuthParameters) {
  48. this.threadLocalUserSession = threadLocalUserSession;
  49. this.userRegistrar = userRegistrar;
  50. this.server = server;
  51. this.csrfVerifier = csrfVerifier;
  52. this.jwtHttpHandler = jwtHttpHandler;
  53. this.userSessionFactory = userSessionFactory;
  54. this.oAuthParameters = oAuthParameters;
  55. }
  56. public OAuth2IdentityProvider.InitContext newContext(HttpServletRequest request, HttpServletResponse response, OAuth2IdentityProvider identityProvider) {
  57. return new OAuthContextImpl(request, response, identityProvider);
  58. }
  59. public OAuth2IdentityProvider.CallbackContext newCallback(HttpServletRequest request, HttpServletResponse response, OAuth2IdentityProvider identityProvider) {
  60. return new OAuthContextImpl(request, response, identityProvider);
  61. }
  62. private class OAuthContextImpl implements OAuth2IdentityProvider.InitContext, OAuth2IdentityProvider.CallbackContext {
  63. private final HttpServletRequest request;
  64. private final HttpServletResponse response;
  65. private final OAuth2IdentityProvider identityProvider;
  66. public OAuthContextImpl(HttpServletRequest request, HttpServletResponse response, OAuth2IdentityProvider identityProvider) {
  67. this.request = request;
  68. this.response = response;
  69. this.identityProvider = identityProvider;
  70. }
  71. @Override
  72. public String getCallbackUrl() {
  73. return server.getPublicRootUrl() + CALLBACK_PATH + identityProvider.getKey();
  74. }
  75. @Override
  76. public String generateCsrfState() {
  77. return csrfVerifier.generateState(request, response);
  78. }
  79. @Override
  80. public HttpServletRequest getRequest() {
  81. return request;
  82. }
  83. @Override
  84. public HttpServletResponse getResponse() {
  85. return response;
  86. }
  87. @Override
  88. public void redirectTo(String url) {
  89. try {
  90. response.sendRedirect(url);
  91. } catch (IOException e) {
  92. throw new IllegalStateException(format("Fail to redirect to %s", url), e);
  93. }
  94. }
  95. @Override
  96. public void verifyCsrfState() {
  97. csrfVerifier.verifyState(request, response, identityProvider);
  98. }
  99. @Override
  100. public void verifyCsrfState(String parameterName) {
  101. csrfVerifier.verifyState(request, response, identityProvider, parameterName);
  102. }
  103. @Override
  104. public void redirectToRequestedPage() {
  105. try {
  106. Optional<String> redirectTo = oAuthParameters.getReturnTo(request);
  107. oAuthParameters.delete(request, response);
  108. getResponse().sendRedirect(redirectTo.orElse(server.getContextPath() + "/"));
  109. } catch (IOException e) {
  110. throw new IllegalStateException("Fail to redirect to requested page", e);
  111. }
  112. }
  113. @Override
  114. public void authenticate(UserIdentity userIdentity) {
  115. Boolean allowEmailShift = oAuthParameters.getAllowEmailShift(request).orElse(false);
  116. Boolean allowUpdateLogin = oAuthParameters.getAllowUpdateLogin(request).orElse(false);
  117. UserDto userDto = userRegistrar.register(
  118. UserRegistration.builder()
  119. .setUserIdentity(userIdentity)
  120. .setProvider(identityProvider)
  121. .setSource(AuthenticationEvent.Source.oauth2(identityProvider))
  122. .setExistingEmailStrategy(allowEmailShift ? ExistingEmailStrategy.ALLOW : ExistingEmailStrategy.WARN)
  123. .setUpdateLoginStrategy(allowUpdateLogin ? UpdateLoginStrategy.ALLOW : UpdateLoginStrategy.WARN)
  124. .build());
  125. jwtHttpHandler.generateToken(userDto, request, response);
  126. threadLocalUserSession.set(userSessionFactory.create(userDto));
  127. }
  128. }
  129. }