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.

LdapAuthenticator.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.auth.ldap;
  21. import java.util.Map;
  22. import javax.naming.NamingException;
  23. import javax.naming.directory.InitialDirContext;
  24. import javax.naming.directory.SearchResult;
  25. import javax.security.auth.login.Configuration;
  26. import javax.security.auth.login.LoginContext;
  27. import javax.security.auth.login.LoginException;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.sonar.api.security.Authenticator;
  30. import org.sonar.api.utils.log.Logger;
  31. import org.sonar.api.utils.log.Loggers;
  32. /**
  33. * @author Evgeny Mandrikov
  34. */
  35. public class LdapAuthenticator extends Authenticator {
  36. private static final Logger LOG = Loggers.get(LdapAuthenticator.class);
  37. private final Map<String, LdapContextFactory> contextFactories;
  38. private final Map<String, LdapUserMapping> userMappings;
  39. public LdapAuthenticator(Map<String, LdapContextFactory> contextFactories, Map<String, LdapUserMapping> userMappings) {
  40. this.contextFactories = contextFactories;
  41. this.userMappings = userMappings;
  42. }
  43. @Override
  44. public boolean doAuthenticate(Context context) {
  45. return authenticate(context.getUsername(), context.getPassword());
  46. }
  47. /**
  48. * Authenticate the user against LDAP servers until first success.
  49. * @param login The login to use.
  50. * @param password The password to use.
  51. * @return false if specified user cannot be authenticated with specified password on any LDAP server
  52. */
  53. public boolean authenticate(String login, String password) {
  54. for (String ldapKey : userMappings.keySet()) {
  55. final String principal;
  56. if (contextFactories.get(ldapKey).isSasl()) {
  57. principal = login;
  58. } else {
  59. final SearchResult result;
  60. try {
  61. result = userMappings.get(ldapKey).createSearch(contextFactories.get(ldapKey), login).findUnique();
  62. } catch (NamingException e) {
  63. LOG.debug("User {} not found in server {}: {}", login, ldapKey, e.getMessage());
  64. continue;
  65. }
  66. if (result == null) {
  67. LOG.debug("User {} not found in {}", login, ldapKey);
  68. continue;
  69. }
  70. principal = result.getNameInNamespace();
  71. }
  72. boolean passwordValid;
  73. if (contextFactories.get(ldapKey).isGssapi()) {
  74. passwordValid = checkPasswordUsingGssapi(principal, password, ldapKey);
  75. } else {
  76. passwordValid = checkPasswordUsingBind(principal, password, ldapKey);
  77. }
  78. if (passwordValid) {
  79. return true;
  80. }
  81. }
  82. LOG.debug("User {} not found", login);
  83. return false;
  84. }
  85. private boolean checkPasswordUsingBind(String principal, String password, String ldapKey) {
  86. if (StringUtils.isEmpty(password)) {
  87. LOG.debug("Password is blank.");
  88. return false;
  89. }
  90. InitialDirContext context = null;
  91. try {
  92. context = contextFactories.get(ldapKey).createUserContext(principal, password);
  93. return true;
  94. } catch (NamingException e) {
  95. LOG.debug("Password not valid for user {} in server {}: {}", principal, ldapKey, e.getMessage());
  96. return false;
  97. } finally {
  98. ContextHelper.closeQuietly(context);
  99. }
  100. }
  101. private boolean checkPasswordUsingGssapi(String principal, String password, String ldapKey) {
  102. // Use our custom configuration to avoid reliance on external config
  103. Configuration.setConfiguration(new Krb5LoginConfiguration());
  104. LoginContext lc;
  105. try {
  106. lc = new LoginContext(getClass().getName(), new CallbackHandlerImpl(principal, password));
  107. lc.login();
  108. } catch (LoginException e) {
  109. // Bad username: Client not found in Kerberos database
  110. // Bad password: Integrity check on decrypted field failed
  111. LOG.debug("Password not valid for {} in server {}: {}", principal, ldapKey, e.getMessage());
  112. return false;
  113. }
  114. try {
  115. lc.logout();
  116. } catch (LoginException e) {
  117. LOG.warn("Logout fails", e);
  118. }
  119. return true;
  120. }
  121. }