blob: 98b35cfb5fb86b270b47cc943f05eb2b4d84118e [file] [log] [blame]
Marc Kupietz1e388b42022-04-30 18:37:03 +02001package de.ids_mannheim.korap.server;
2
3import com.unboundid.ldap.sdk.LDAPException;
4import com.unboundid.util.Base64;
Marc Kupietz1e388b42022-04-30 18:37:03 +02005import de.ids_mannheim.korap.authentication.LdapAuth3;
Marc Kupietzd43a98d2023-09-22 17:11:46 +02006import org.junit.jupiter.api.AfterAll;
7import org.junit.jupiter.api.Test;
Marc Kupietz1e388b42022-04-30 18:37:03 +02008
Marc Kupietz30925d82022-05-06 15:33:52 +02009import javax.crypto.SecretKeyFactory;
10import javax.crypto.spec.PBEKeySpec;
Marc Kupietz9a1188e2022-05-05 23:26:14 +020011import java.net.UnknownHostException;
12import java.security.GeneralSecurityException;
Marc Kupietz30925d82022-05-06 15:33:52 +020013import java.security.NoSuchAlgorithmException;
14import java.security.spec.InvalidKeySpecException;
15import java.security.spec.KeySpec;
Marc Kupietz9a1188e2022-05-05 23:26:14 +020016
Marc Kupietz1e388b42022-04-30 18:37:03 +020017import static de.ids_mannheim.korap.authentication.LdapAuth3.LDAP_AUTH_ROK;
Marc Kupietz7cb32132022-05-09 06:25:47 +020018import static de.ids_mannheim.korap.authentication.LdapAuth3.LDAP_AUTH_RUNKNOWN;
Marc Kupietzd43a98d2023-09-22 17:11:46 +020019import static org.junit.jupiter.api.Assertions.assertEquals;
20
Marc Kupietz1e388b42022-04-30 18:37:03 +020021public class EmbeddedLdapServerTest {
22
Marc Kupietz30925d82022-05-06 15:33:52 +020023 public static final String TEST_EMBEDDED_LDAP_CONF = "src/test/resources/test-embedded-ldap.conf";
Marc Kupietz392f4782022-05-02 13:23:18 +020024
Marc Kupietzd43a98d2023-09-22 17:11:46 +020025 @AfterAll
margaretha35e1ca22023-11-16 22:00:01 +010026 static void shutdownEmbeddedLdapServer () {
Marc Kupietz1e388b42022-04-30 18:37:03 +020027 EmbeddedLdapServer.stop();
28 }
29
30 @Test
margaretha35e1ca22023-11-16 22:00:01 +010031 public void embeddedServerStartsAutomaticallyAndUsersCanLogin ()
32 throws LDAPException {
33 assertEquals(LDAP_AUTH_ROK,
34 LdapAuth3.login("user", "password", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz1e388b42022-04-30 18:37:03 +020035 }
36
37 @Test
margaretha35e1ca22023-11-16 22:00:01 +010038 public void usersWithClearPasswordCanLogin () throws LDAPException {
39 assertEquals(LDAP_AUTH_ROK,
40 LdapAuth3.login("user1", "password1", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz30925d82022-05-06 15:33:52 +020041 }
42
43 @Test
margaretha35e1ca22023-11-16 22:00:01 +010044 public void usersWithSHA1PasswordCanLogin ()
45 throws LDAPException, NoSuchAlgorithmException {
46 assertEquals(LDAP_AUTH_ROK,
47 LdapAuth3.login("user3", "password3", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz30925d82022-05-06 15:33:52 +020048 }
49
50 @Test
margaretha35e1ca22023-11-16 22:00:01 +010051 public void usersWithSHA256PasswordCanLogin () throws LDAPException,
52 NoSuchAlgorithmException, InvalidKeySpecException {
53 assertEquals(LDAP_AUTH_ROK,
54 LdapAuth3.login("user4", "password4", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz1e388b42022-04-30 18:37:03 +020055 }
56
57 @Test
margaretha35e1ca22023-11-16 22:00:01 +010058 public void asteriskPasswordsFail () throws LDAPException {
59 assertEquals(LDAP_AUTH_RUNKNOWN,
60 LdapAuth3.login("user1", "*", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz30925d82022-05-06 15:33:52 +020061 }
62
63 @Test
margaretha35e1ca22023-11-16 22:00:01 +010064 public void loginWithPreencodedPBKDF2Password () throws LDAPException,
65 NoSuchAlgorithmException, InvalidKeySpecException {
Marc Kupietz30925d82022-05-06 15:33:52 +020066 byte[] salt = new byte[32];
margaretha35e1ca22023-11-16 22:00:01 +010067 KeySpec spec = new PBEKeySpec("password5".toCharArray(), salt, 65536,
68 256);
69 SecretKeyFactory f = SecretKeyFactory
70 .getInstance("PBKDF2withHmacSHA256");
Marc Kupietz30925d82022-05-06 15:33:52 +020071 byte[] hash = f.generateSecret(spec).getEncoded();
margaretha35e1ca22023-11-16 22:00:01 +010072 final String pbkdf2sha256Password = "{PBKDF2-SHA256}"
73 + Base64.encode(hash);
Marc Kupietzd43a98d2023-09-22 17:11:46 +020074 // System.out.println(pbkdf2sha256Password);
margaretha35e1ca22023-11-16 22:00:01 +010075 assertEquals(LDAP_AUTH_ROK, LdapAuth3.login("user5",
76 pbkdf2sha256Password, TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz30925d82022-05-06 15:33:52 +020077 }
78
79 @Test
margaretha35e1ca22023-11-16 22:00:01 +010080 public void loginWithUnencodedPBKDF2PasswordFails () throws LDAPException,
81 NoSuchAlgorithmException, InvalidKeySpecException {
82 assertEquals(LDAP_AUTH_RUNKNOWN,
83 LdapAuth3.login("user5", "password5", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz1e388b42022-04-30 18:37:03 +020084 }
85
86 @Test
margaretha35e1ca22023-11-16 22:00:01 +010087 public void unauthorizedUsersAreNotAllowed () throws LDAPException {
88 assertEquals(LDAP_AUTH_RUNKNOWN,
89 LdapAuth3.login("yuser", "password", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz1e388b42022-04-30 18:37:03 +020090 }
Marc Kupietz75e78282022-05-02 20:39:20 +020091
92 @Test
margaretha35e1ca22023-11-16 22:00:01 +010093 public void gettingMailForUser () throws LDAPException,
94 UnknownHostException, GeneralSecurityException {
Marc Kupietz30925d82022-05-06 15:33:52 +020095 EmbeddedLdapServer.startIfNotRunning(TEST_EMBEDDED_LDAP_CONF);
margaretha35e1ca22023-11-16 22:00:01 +010096 assertEquals(LdapAuth3.getEmail("user2", TEST_EMBEDDED_LDAP_CONF),
97 "user2@example.com");
Marc Kupietz75e78282022-05-02 20:39:20 +020098 }
99
100 @Test
margaretha35e1ca22023-11-16 22:00:01 +0100101 public void gettingMailForNAUTHUserIsNull () throws LDAPException,
102 UnknownHostException, GeneralSecurityException {
Marc Kupietz30925d82022-05-06 15:33:52 +0200103 EmbeddedLdapServer.startIfNotRunning(TEST_EMBEDDED_LDAP_CONF);
margaretha35e1ca22023-11-16 22:00:01 +0100104 assertEquals(null,
105 LdapAuth3.getEmail("user1000", TEST_EMBEDDED_LDAP_CONF));
Marc Kupietz75e78282022-05-02 20:39:20 +0200106 }
Marc Kupietz392f4782022-05-02 13:23:18 +0200107}