| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.server; |
| 2 | |
| 3 | import com.unboundid.ldap.sdk.LDAPException; |
| 4 | import com.unboundid.util.Base64; |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.authentication.LdapAuth3; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 6 | import org.junit.jupiter.api.AfterAll; |
| 7 | import org.junit.jupiter.api.Test; |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 8 | |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 9 | import javax.crypto.SecretKeyFactory; |
| 10 | import javax.crypto.spec.PBEKeySpec; |
| Marc Kupietz | 9a1188e | 2022-05-05 23:26:14 +0200 | [diff] [blame] | 11 | import java.net.UnknownHostException; |
| 12 | import java.security.GeneralSecurityException; |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 13 | import java.security.NoSuchAlgorithmException; |
| 14 | import java.security.spec.InvalidKeySpecException; |
| 15 | import java.security.spec.KeySpec; |
| Marc Kupietz | 9a1188e | 2022-05-05 23:26:14 +0200 | [diff] [blame] | 16 | |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 17 | import static de.ids_mannheim.korap.authentication.LdapAuth3.LDAP_AUTH_ROK; |
| Marc Kupietz | 7cb3213 | 2022-05-09 06:25:47 +0200 | [diff] [blame] | 18 | import static de.ids_mannheim.korap.authentication.LdapAuth3.LDAP_AUTH_RUNKNOWN; |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 19 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 21 | public class EmbeddedLdapServerTest { |
| 22 | |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 23 | public static final String TEST_EMBEDDED_LDAP_CONF = "src/test/resources/test-embedded-ldap.conf"; |
| Marc Kupietz | 392f478 | 2022-05-02 13:23:18 +0200 | [diff] [blame] | 24 | |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 25 | @AfterAll |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 26 | static void shutdownEmbeddedLdapServer () { |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 27 | EmbeddedLdapServer.stop(); |
| 28 | } |
| 29 | |
| 30 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 31 | public void embeddedServerStartsAutomaticallyAndUsersCanLogin () |
| 32 | throws LDAPException { |
| 33 | assertEquals(LDAP_AUTH_ROK, |
| 34 | LdapAuth3.login("user", "password", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 38 | public void usersWithClearPasswordCanLogin () throws LDAPException { |
| 39 | assertEquals(LDAP_AUTH_ROK, |
| 40 | LdapAuth3.login("user1", "password1", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 44 | public void usersWithSHA1PasswordCanLogin () |
| 45 | throws LDAPException, NoSuchAlgorithmException { |
| 46 | assertEquals(LDAP_AUTH_ROK, |
| 47 | LdapAuth3.login("user3", "password3", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 51 | public void usersWithSHA256PasswordCanLogin () throws LDAPException, |
| 52 | NoSuchAlgorithmException, InvalidKeySpecException { |
| 53 | assertEquals(LDAP_AUTH_ROK, |
| 54 | LdapAuth3.login("user4", "password4", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 58 | public void asteriskPasswordsFail () throws LDAPException { |
| 59 | assertEquals(LDAP_AUTH_RUNKNOWN, |
| 60 | LdapAuth3.login("user1", "*", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 64 | public void loginWithPreencodedPBKDF2Password () throws LDAPException, |
| 65 | NoSuchAlgorithmException, InvalidKeySpecException { |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 66 | byte[] salt = new byte[32]; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 67 | KeySpec spec = new PBEKeySpec("password5".toCharArray(), salt, 65536, |
| 68 | 256); |
| 69 | SecretKeyFactory f = SecretKeyFactory |
| 70 | .getInstance("PBKDF2withHmacSHA256"); |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 71 | byte[] hash = f.generateSecret(spec).getEncoded(); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 72 | final String pbkdf2sha256Password = "{PBKDF2-SHA256}" |
| 73 | + Base64.encode(hash); |
| Marc Kupietz | d43a98d | 2023-09-22 17:11:46 +0200 | [diff] [blame] | 74 | // System.out.println(pbkdf2sha256Password); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 75 | assertEquals(LDAP_AUTH_ROK, LdapAuth3.login("user5", |
| 76 | pbkdf2sha256Password, TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 80 | public void loginWithUnencodedPBKDF2PasswordFails () throws LDAPException, |
| 81 | NoSuchAlgorithmException, InvalidKeySpecException { |
| 82 | assertEquals(LDAP_AUTH_RUNKNOWN, |
| 83 | LdapAuth3.login("user5", "password5", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 87 | public void unauthorizedUsersAreNotAllowed () throws LDAPException { |
| 88 | assertEquals(LDAP_AUTH_RUNKNOWN, |
| 89 | LdapAuth3.login("yuser", "password", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 1e388b4 | 2022-04-30 18:37:03 +0200 | [diff] [blame] | 90 | } |
| Marc Kupietz | 75e7828 | 2022-05-02 20:39:20 +0200 | [diff] [blame] | 91 | |
| 92 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 93 | public void gettingMailForUser () throws LDAPException, |
| 94 | UnknownHostException, GeneralSecurityException { |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 95 | EmbeddedLdapServer.startIfNotRunning(TEST_EMBEDDED_LDAP_CONF); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 96 | assertEquals(LdapAuth3.getEmail("user2", TEST_EMBEDDED_LDAP_CONF), |
| 97 | "user2@example.com"); |
| Marc Kupietz | 75e7828 | 2022-05-02 20:39:20 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Test |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 101 | public void gettingMailForNAUTHUserIsNull () throws LDAPException, |
| 102 | UnknownHostException, GeneralSecurityException { |
| Marc Kupietz | 30925d8 | 2022-05-06 15:33:52 +0200 | [diff] [blame] | 103 | EmbeddedLdapServer.startIfNotRunning(TEST_EMBEDDED_LDAP_CONF); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 104 | assertEquals(null, |
| 105 | LdapAuth3.getEmail("user1000", TEST_EMBEDDED_LDAP_CONF)); |
| Marc Kupietz | 75e7828 | 2022-05-02 20:39:20 +0200 | [diff] [blame] | 106 | } |
| Marc Kupietz | 392f478 | 2022-05-02 13:23:18 +0200 | [diff] [blame] | 107 | } |