Bring back function to get email attribute for/from user ID
Resolves https://github.com/KorAP/Kustvakt/issues/12
Change-Id: I74eb3d1e93b7b406fc86847025c516898f1b07bc
diff --git a/full/src/main/java/de/ids_mannheim/korap/authentication/LdapAuth3.java b/full/src/main/java/de/ids_mannheim/korap/authentication/LdapAuth3.java
index 129843f..bc091ad 100644
--- a/full/src/main/java/de/ids_mannheim/korap/authentication/LdapAuth3.java
+++ b/full/src/main/java/de/ids_mannheim/korap/authentication/LdapAuth3.java
@@ -77,12 +77,22 @@
sUserDN = Filter.encodeValue(sUserDN);
sUserPwd = Filter.encodeValue(sUserPwd);
+ SearchResult srchRes = search(sUserDN, sUserPwd, ldapConfigFilename);
+
+ if (srchRes == null || srchRes.getEntryCount() == 0) {
+ if (DEBUGLOG) System.out.printf("Finding '%s': no entry found!\n", sUserDN);
+ return LDAP_AUTH_RNAUTH;
+ }
+ return LDAP_AUTH_ROK;
+ }
+
+ public static SearchResult search(String sUserDN, String sUserPwd, String ldapConfigFilename) throws LDAPException {
Map<String, String> ldapConfig;
try {
ldapConfig = loadProp(ldapConfigFilename);
} catch (IOException e) {
System.out.println("Error: LDAPAuth.login: cannot load Property file!");
- return LDAP_AUTH_RINTERR;
+ return null;
}
assert ldapConfig != null;
@@ -137,7 +147,8 @@
lc = new LDAPConnection(socketFactory);
} catch (GeneralSecurityException e) {
System.err.printf("Error: login: Connecting to LDAPS Server: failed: '%s'!\n", e);
- return ldapTerminate(null, LDAP_AUTH_RCONNECT);
+ ldapTerminate(null);
+ return null;
}
} else {
lc = new LDAPConnection();
@@ -149,7 +160,8 @@
} catch (LDAPException e) {
String fullStackTrace = org.apache.commons.lang.exception.ExceptionUtils.getFullStackTrace(e);
System.err.printf("Error: login: Connecting to LDAP Server: failed: '%s'!\n", fullStackTrace);
- return ldapTerminate(lc, LDAP_AUTH_RCONNECT);
+ ldapTerminate(lc);
+ return null;
}
@@ -162,7 +174,8 @@
if (DEBUGLOG) System.out.print("Binding: OK.\n");
} catch (LDAPException e) {
System.err.printf("Error: login: Binding failed: '%s'!\n", e);
- return ldapTerminate(lc, LDAP_AUTH_RINTERR);
+ ldapTerminate(lc);
+ return null;
}
if (DEBUGLOG) System.out.printf("Debug: isConnected=%d\n", lc.isConnected() ? 1 : 0);
@@ -179,25 +192,52 @@
if (DEBUGLOG) System.out.printf("Finding '%s': %d entries.\n", sUserDN, srchRes.getEntryCount());
} catch (LDAPSearchException e) {
System.err.printf("Error: login: Search for User failed: '%s'!\n", e);
- return ldapTerminate(lc, LDAP_AUTH_RNAUTH);
+ ldapTerminate(lc);
+ return null;
}
if (srchRes.getEntryCount() == 0) {
if (DEBUGLOG) System.out.printf("Finding '%s': no entry found!\n", sUserDN);
- return ldapTerminate(lc, LDAP_AUTH_RNAUTH);
+ return null;
}
- return ldapTerminate(lc, LDAP_AUTH_ROK); // OK.
+ ldapTerminate(lc);
+ return srchRes;
}
- public static int ldapTerminate(LDAPConnection lc, int ret) {
+ public static String getEmail(String sUserDN, String ldapConfigFilename) throws LDAPException {
+ String sUserPwd = "*";
+ Map<String, String> ldapConfig;
+ try {
+ ldapConfig = loadProp(ldapConfigFilename);
+ } catch (IOException e) {
+ System.out.println("Error: LDAPAuth.login: cannot load Property file!");
+ return null;
+ }
+ final String emailAttribute = ldapConfig.getOrDefault("emailAttribute", "mail");
+
+ SearchResult searchResult = search(sUserDN, sUserPwd, ldapConfigFilename);
+
+ if (searchResult == null) {
+ return null;
+ }
+
+ for (SearchResultEntry entry : searchResult.getSearchEntries()) {
+ String mail = entry.getAttributeValue(emailAttribute);
+ if (mail != null) {
+ return mail;
+ }
+ }
+ return null;
+ }
+
+ public static void ldapTerminate(LDAPConnection lc) {
if (DEBUGLOG) System.out.println("Terminating...");
if (lc != null) {
lc.close(null);
}
if (DEBUGLOG) System.out.println("closing connection: done.\n");
- return ret;
}
private static void addSSLCipherSuites(String ciphersCsv) {
diff --git a/full/src/test/java/de/ids_mannheim/korap/authentication/LdapAuth3Test.java b/full/src/test/java/de/ids_mannheim/korap/authentication/LdapAuth3Test.java
index 9df6359..30d585b 100644
--- a/full/src/test/java/de/ids_mannheim/korap/authentication/LdapAuth3Test.java
+++ b/full/src/test/java/de/ids_mannheim/korap/authentication/LdapAuth3Test.java
@@ -132,4 +132,10 @@
assertEquals(LDAP_AUTH_RNAUTH, LdapAuth3.login("testuser", "*", TEST_LDAPS_TS_CONF));
}
+ @Test
+ public void testGettingMailForUid() throws LDAPException {
+ assertEquals("testuser@example.com", LdapAuth3.getEmail("testuser", TEST_LDAP_CONF));
+ assertEquals("peter@example.org", LdapAuth3.getEmail("testuser2", TEST_LDAPS_CONF));
+ assertEquals(null, LdapAuth3.getEmail("non-exsting", TEST_LDAPS_CONF));
+ }
}
diff --git a/full/src/test/java/de/ids_mannheim/korap/server/EmbeddedLdapServerTest.java b/full/src/test/java/de/ids_mannheim/korap/server/EmbeddedLdapServerTest.java
index 8532839..e5e5cb7 100644
--- a/full/src/test/java/de/ids_mannheim/korap/server/EmbeddedLdapServerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/server/EmbeddedLdapServerTest.java
@@ -42,4 +42,14 @@
public void unauthorizedUsersAreNotAllowed() throws LDAPException {
assertEquals(LDAP_AUTH_RNAUTH, LdapAuth3.login("yuser", "password", EMBEDDED_LDAP_DEFAULT_CONF));
}
+
+ @Test
+ public void gettingMailForUser() throws LDAPException {
+ assertEquals("user2@example.com", LdapAuth3.getEmail("user2", EMBEDDED_LDAP_DEFAULT_CONF));
+ }
+
+ @Test
+ public void gettingMailForUnknownUserIsNull() throws LDAPException {
+ assertEquals(null, LdapAuth3.getEmail("user1000", EMBEDDED_LDAP_DEFAULT_CONF));
+ }
}