3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.authentication;
22 import java.util.Optional;
23 import javax.servlet.http.HttpServletRequest;
24 import org.sonar.db.DbClient;
25 import org.sonar.db.DbSession;
26 import org.sonar.db.user.UserDto;
27 import org.sonar.server.authentication.event.AuthenticationEvent;
28 import org.sonar.server.authentication.event.AuthenticationException;
30 import static org.sonar.server.authentication.event.AuthenticationEvent.Method;
31 import static org.sonar.server.authentication.event.AuthenticationEvent.Source;
34 * Authentication based on the tuple {login, password}. Validation can be
35 * delegated to an external system, e.g. LDAP.
37 public class CredentialsAuthentication {
39 private final DbClient dbClient;
40 private final AuthenticationEvent authenticationEvent;
41 private final CredentialsExternalAuthentication externalAuthentication;
42 private final CredentialsLocalAuthentication localAuthentication;
44 public CredentialsAuthentication(DbClient dbClient, AuthenticationEvent authenticationEvent,
45 CredentialsExternalAuthentication externalAuthentication, CredentialsLocalAuthentication localAuthentication) {
46 this.dbClient = dbClient;
47 this.authenticationEvent = authenticationEvent;
48 this.externalAuthentication = externalAuthentication;
49 this.localAuthentication = localAuthentication;
52 public UserDto authenticate(Credentials credentials, HttpServletRequest request, Method method) {
53 try (DbSession dbSession = dbClient.openSession(false)) {
54 return authenticate(dbSession, credentials, request, method);
58 private UserDto authenticate(DbSession dbSession, Credentials credentials, HttpServletRequest request, Method method) {
59 UserDto localUser = dbClient.userDao().selectActiveUserByLogin(dbSession, credentials.getLogin());
60 if (localUser != null && localUser.isLocal()) {
61 localAuthentication.authenticate(dbSession, localUser, credentials.getPassword().orElse(null), method);
63 authenticationEvent.loginSuccess(request, localUser.getLogin(), Source.local(method));
66 Optional<UserDto> externalUser = externalAuthentication.authenticate(credentials, request, method);
67 if (externalUser.isPresent()) {
68 return externalUser.get();
70 localAuthentication.generateHashToAvoidEnumerationAttack();
71 throw AuthenticationException.newBuilder()
72 .setSource(Source.local(method))
73 .setLogin(credentials.getLogin())
74 .setMessage(localUser != null && !localUser.isLocal() ? "User is not local" : "No active user for login")