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.

LdapUserMapping.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 org.apache.commons.lang.StringUtils;
  22. import org.sonar.api.config.Configuration;
  23. import org.sonar.api.utils.log.Logger;
  24. import org.sonar.api.utils.log.Loggers;
  25. /**
  26. * @author Evgeny Mandrikov
  27. */
  28. public class LdapUserMapping {
  29. private static final Logger LOG = Loggers.get(LdapUserMapping.class);
  30. private static final String DEFAULT_NAME_ATTRIBUTE = "cn";
  31. private static final String DEFAULT_EMAIL_ATTRIBUTE = "mail";
  32. private static final String DEFAULT_REQUEST = "(&(objectClass=inetOrgPerson)(uid={login}))";
  33. private final String baseDn;
  34. private final String request;
  35. private final String realNameAttribute;
  36. private final String emailAttribute;
  37. /**
  38. * Constructs mapping from Sonar settings.
  39. */
  40. public LdapUserMapping(Configuration config, String settingsPrefix) {
  41. String usesrBaseDnSettingKey = settingsPrefix + ".user.baseDn";
  42. String usersBaseDn = config.get(usesrBaseDnSettingKey).orElse(null);
  43. if (usersBaseDn == null) {
  44. String realm = config.get(settingsPrefix + ".realm").orElse(null);
  45. if (realm != null) {
  46. LOG.warn("Auto-discovery feature is deprecated, please use '{}' to specify user search dn", usesrBaseDnSettingKey);
  47. usersBaseDn = LdapAutodiscovery.getDnsDomainDn(realm);
  48. }
  49. }
  50. this.baseDn = usersBaseDn;
  51. this.realNameAttribute = StringUtils.defaultString(config.get(settingsPrefix + ".user.realNameAttribute").orElse(null), DEFAULT_NAME_ATTRIBUTE);
  52. this.emailAttribute = StringUtils.defaultString(config.get(settingsPrefix + ".user.emailAttribute").orElse(null), DEFAULT_EMAIL_ATTRIBUTE);
  53. String req = StringUtils.defaultString(config.get(settingsPrefix + ".user.request").orElse(null), DEFAULT_REQUEST);
  54. req = StringUtils.replace(req, "{login}", "{0}");
  55. this.request = req;
  56. }
  57. /**
  58. * Search for this mapping.
  59. */
  60. public LdapSearch createSearch(LdapContextFactory contextFactory, String username) {
  61. return new LdapSearch(contextFactory)
  62. .setBaseDn(getBaseDn())
  63. .setRequest(getRequest())
  64. .setParameters(username);
  65. }
  66. /**
  67. * Base DN. For example "ou=users,o=mycompany" or "cn=users" (Active Directory Server).
  68. */
  69. public String getBaseDn() {
  70. return baseDn;
  71. }
  72. /**
  73. * Request. For example:
  74. * <pre>
  75. * (&(objectClass=inetOrgPerson)(uid={0}))
  76. * (&(objectClass=user)(sAMAccountName={0}))
  77. * </pre>
  78. */
  79. public String getRequest() {
  80. return request;
  81. }
  82. /**
  83. * Real Name Attribute. For example "cn".
  84. */
  85. public String getRealNameAttribute() {
  86. return realNameAttribute;
  87. }
  88. /**
  89. * EMail Attribute. For example "mail".
  90. */
  91. public String getEmailAttribute() {
  92. return emailAttribute;
  93. }
  94. @Override
  95. public String toString() {
  96. return getClass().getSimpleName() + "{" +
  97. "baseDn=" + getBaseDn() +
  98. ", request=" + getRequest() +
  99. ", realNameAttribute=" + getRealNameAttribute() +
  100. ", emailAttribute=" + getEmailAttribute() +
  101. "}";
  102. }
  103. }