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.

BaseContextFactory.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import org.sonar.api.platform.Server;
  24. import org.sonar.api.server.authentication.BaseIdentityProvider;
  25. import org.sonar.api.server.authentication.UserIdentity;
  26. import org.sonar.db.user.UserDto;
  27. import org.sonar.server.authentication.event.AuthenticationEvent.Source;
  28. import org.sonar.server.user.ThreadLocalUserSession;
  29. import org.sonar.server.user.UserSessionFactory;
  30. public class BaseContextFactory {
  31. private final ThreadLocalUserSession threadLocalUserSession;
  32. private final UserRegistrar userRegistrar;
  33. private final Server server;
  34. private final JwtHttpHandler jwtHttpHandler;
  35. private final UserSessionFactory userSessionFactory;
  36. public BaseContextFactory(UserRegistrar userRegistrar, Server server, JwtHttpHandler jwtHttpHandler,
  37. ThreadLocalUserSession threadLocalUserSession, UserSessionFactory userSessionFactory) {
  38. this.userSessionFactory = userSessionFactory;
  39. this.userRegistrar = userRegistrar;
  40. this.server = server;
  41. this.jwtHttpHandler = jwtHttpHandler;
  42. this.threadLocalUserSession = threadLocalUserSession;
  43. }
  44. public BaseIdentityProvider.Context newContext(HttpServletRequest request, HttpServletResponse response, BaseIdentityProvider identityProvider) {
  45. return new ContextImpl(request, response, identityProvider);
  46. }
  47. private class ContextImpl implements BaseIdentityProvider.Context {
  48. private final HttpServletRequest request;
  49. private final HttpServletResponse response;
  50. private final BaseIdentityProvider identityProvider;
  51. public ContextImpl(HttpServletRequest request, HttpServletResponse response, BaseIdentityProvider identityProvider) {
  52. this.request = request;
  53. this.response = response;
  54. this.identityProvider = identityProvider;
  55. }
  56. @Override
  57. public HttpServletRequest getRequest() {
  58. return request;
  59. }
  60. @Override
  61. public HttpServletResponse getResponse() {
  62. return response;
  63. }
  64. @Override
  65. public String getServerBaseURL() {
  66. return server.getPublicRootUrl();
  67. }
  68. @Override
  69. public void authenticate(UserIdentity userIdentity) {
  70. UserDto userDto = userRegistrar.register(
  71. UserRegistration.builder()
  72. .setUserIdentity(userIdentity)
  73. .setProvider(identityProvider)
  74. .setSource(Source.external(identityProvider))
  75. .build());
  76. jwtHttpHandler.generateToken(userDto, request, response);
  77. threadLocalUserSession.set(userSessionFactory.create(userDto));
  78. }
  79. }
  80. }