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.

LdapAuthenticatorTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.junit.ClassRule;
  22. import org.junit.Test;
  23. import org.sonar.auth.ldap.server.LdapServer;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. public class LdapAuthenticatorTest {
  26. /**
  27. * A reference to the original ldif file
  28. */
  29. public static final String USERS_EXAMPLE_ORG_LDIF = "/users.example.org.ldif";
  30. /**
  31. * A reference to an aditional ldif file.
  32. */
  33. public static final String USERS_INFOSUPPORT_COM_LDIF = "/users.infosupport.com.ldif";
  34. @ClassRule
  35. public static LdapServer exampleServer = new LdapServer(USERS_EXAMPLE_ORG_LDIF);
  36. @ClassRule
  37. public static LdapServer infosupportServer = new LdapServer(USERS_INFOSUPPORT_COM_LDIF, "infosupport.com", "dc=infosupport,dc=com");
  38. @Test
  39. public void testNoConnection() {
  40. exampleServer.disableAnonymousAccess();
  41. try {
  42. LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_SIMPLE).asConfig(),
  43. new LdapAutodiscovery());
  44. LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());
  45. authenticator.authenticate("godin", "secret1");
  46. } finally {
  47. exampleServer.enableAnonymousAccess();
  48. }
  49. }
  50. @Test
  51. public void testSimple() {
  52. LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_SIMPLE).asConfig(),
  53. new LdapAutodiscovery());
  54. LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());
  55. assertThat(authenticator.authenticate("godin", "secret1")).isTrue();
  56. assertThat(authenticator.authenticate("godin", "wrong")).isFalse();
  57. assertThat(authenticator.authenticate("tester", "secret2")).isTrue();
  58. assertThat(authenticator.authenticate("tester", "wrong")).isFalse();
  59. assertThat(authenticator.authenticate("notfound", "wrong")).isFalse();
  60. // SONARPLUGINS-2493
  61. assertThat(authenticator.authenticate("godin", "")).isFalse();
  62. assertThat(authenticator.authenticate("godin", null)).isFalse();
  63. }
  64. @Test
  65. public void testSimpleMultiLdap() {
  66. LdapSettingsManager settingsManager = new LdapSettingsManager(
  67. LdapSettingsFactory.generateAuthenticationSettings(exampleServer, infosupportServer, LdapContextFactory.AUTH_METHOD_SIMPLE).asConfig(), new LdapAutodiscovery());
  68. LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());
  69. assertThat(authenticator.authenticate("godin", "secret1")).isTrue();
  70. assertThat(authenticator.authenticate("godin", "wrong")).isFalse();
  71. assertThat(authenticator.authenticate("tester", "secret2")).isTrue();
  72. assertThat(authenticator.authenticate("tester", "wrong")).isFalse();
  73. assertThat(authenticator.authenticate("notfound", "wrong")).isFalse();
  74. // SONARPLUGINS-2493
  75. assertThat(authenticator.authenticate("godin", "")).isFalse();
  76. assertThat(authenticator.authenticate("godin", null)).isFalse();
  77. // SONARPLUGINS-2793
  78. assertThat(authenticator.authenticate("robby", "secret1")).isTrue();
  79. assertThat(authenticator.authenticate("robby", "wrong")).isFalse();
  80. }
  81. @Test
  82. public void testSasl() {
  83. LdapSettingsManager settingsManager = new LdapSettingsManager(LdapSettingsFactory.generateAuthenticationSettings(exampleServer, null, LdapContextFactory.AUTH_METHOD_CRAM_MD5).asConfig(),
  84. new LdapAutodiscovery());
  85. LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());
  86. assertThat(authenticator.authenticate("godin", "secret1")).isTrue();
  87. assertThat(authenticator.authenticate("godin", "wrong")).isFalse();
  88. assertThat(authenticator.authenticate("tester", "secret2")).isTrue();
  89. assertThat(authenticator.authenticate("tester", "wrong")).isFalse();
  90. assertThat(authenticator.authenticate("notfound", "wrong")).isFalse();
  91. }
  92. @Test
  93. public void testSaslMultipleLdap() {
  94. LdapSettingsManager settingsManager = new LdapSettingsManager(
  95. LdapSettingsFactory.generateAuthenticationSettings(exampleServer, infosupportServer, LdapContextFactory.AUTH_METHOD_CRAM_MD5).asConfig(), new LdapAutodiscovery());
  96. LdapAuthenticator authenticator = new LdapAuthenticator(settingsManager.getContextFactories(), settingsManager.getUserMappings());
  97. assertThat(authenticator.authenticate("godin", "secret1")).isTrue();
  98. assertThat(authenticator.authenticate("godin", "wrong")).isFalse();
  99. assertThat(authenticator.authenticate("tester", "secret2")).isTrue();
  100. assertThat(authenticator.authenticate("tester", "wrong")).isFalse();
  101. assertThat(authenticator.authenticate("notfound", "wrong")).isFalse();
  102. assertThat(authenticator.authenticate("robby", "secret1")).isTrue();
  103. assertThat(authenticator.authenticate("robby", "wrong")).isFalse();
  104. }
  105. }