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.

CredentialsAuthentication.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 java.util.Optional;
  22. import javax.servlet.http.HttpServletRequest;
  23. import org.sonar.db.DbClient;
  24. import org.sonar.db.DbSession;
  25. import org.sonar.db.user.UserDto;
  26. import org.sonar.server.authentication.event.AuthenticationEvent;
  27. import org.sonar.server.authentication.event.AuthenticationException;
  28. import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
  29. import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
  30. /**
  31. * Authentication based on the tuple {login, password}. Validation can be
  32. * delegated to an external system, e.g. LDAP.
  33. */
  34. public class CredentialsAuthentication {
  35. private final DbClient dbClient;
  36. private final AuthenticationEvent authenticationEvent;
  37. private final CredentialsExternalAuthentication externalAuthentication;
  38. private final CredentialsLocalAuthentication localAuthentication;
  39. public CredentialsAuthentication(DbClient dbClient, AuthenticationEvent authenticationEvent,
  40. CredentialsExternalAuthentication externalAuthentication, CredentialsLocalAuthentication localAuthentication) {
  41. this.dbClient = dbClient;
  42. this.authenticationEvent = authenticationEvent;
  43. this.externalAuthentication = externalAuthentication;
  44. this.localAuthentication = localAuthentication;
  45. }
  46. public UserDto authenticate(Credentials credentials, HttpServletRequest request, Method method) {
  47. try (DbSession dbSession = dbClient.openSession(false)) {
  48. return authenticate(dbSession, credentials, request, method);
  49. }
  50. }
  51. private UserDto authenticate(DbSession dbSession, Credentials credentials, HttpServletRequest request, Method method) {
  52. UserDto localUser = dbClient.userDao().selectActiveUserByLogin(dbSession, credentials.getLogin());
  53. if (localUser != null && localUser.isLocal()) {
  54. localAuthentication.authenticate(dbSession, localUser, credentials.getPassword().orElse(null), method);
  55. dbSession.commit();
  56. authenticationEvent.loginSuccess(request, localUser.getLogin(), Source.local(method));
  57. return localUser;
  58. }
  59. Optional<UserDto> externalUser = externalAuthentication.authenticate(credentials, request, method);
  60. if (externalUser.isPresent()) {
  61. return externalUser.get();
  62. }
  63. throw AuthenticationException.newBuilder()
  64. .setSource(Source.local(method))
  65. .setLogin(credentials.getLogin())
  66. .setMessage(localUser != null && !localUser.isLocal() ? "User is not local" : "No active user for login")
  67. .build();
  68. }
  69. }