Added getting username from LDAP

Change-Id: Ib568da0537fdbe303c1ef2fe842124230467089f
diff --git a/full/Changes b/full/Changes
index fc593b0..a6b0378 100644
--- a/full/Changes
+++ b/full/Changes
@@ -3,7 +3,9 @@
 - Support token array in matchinfo (fixes #570; diewald)
 - Updated VC list API and deprecated owner VC list (addressed #580)
 - Added user info web-service (solved #566)
-- Implemented configurable resource filters for search and match info APIs
+- Implemented configurable resource filters for search and match 
+  info APIs (#539)
+- Added getting username from LDAP (#568)
 
 # version 0.69.3
 
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 603ec38..8d3633f 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
@@ -252,6 +252,29 @@
         }
         return null;
     }
+    
+    public static String getUsername(String sUserDN, String ldapConfigFilename) throws LDAPException {
+        String sUserPwd = "*";
+        LDAPConfig ldapConfig = new LDAPConfig(ldapConfigFilename);
+        final String idsC2Attribute = "idsC2Profile";
+        final String uidAttribute = "uid";
+        
+        SearchResult searchResult = search(sUserDN, sUserPwd, ldapConfig, false, false)
+                .getSearchResultValue();
+
+        if (searchResult == null) {
+            return null;
+        }
+        
+        String username = null;
+        for (SearchResultEntry entry : searchResult.getSearchEntries()) {
+            username = entry.getAttributeValue(idsC2Attribute);
+            if (username == null) {
+                username = entry.getAttributeValue(uidAttribute);
+            }
+        }
+        return username;
+    }
 
     public static void ldapTerminate(LDAPConnection lc) {
         if (DEBUGLOG) System.out.println("Terminating...");
diff --git a/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java b/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java
index 5b1fbc9..665b082 100644
--- a/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java
+++ b/full/src/main/java/de/ids_mannheim/korap/oauth2/oltu/service/OltuTokenService.java
@@ -284,7 +284,8 @@
         if (config.getOAuth2passwordAuthentication()
                 .equals(AuthenticationMethod.LDAP)) {
             try {
-                username = LdapAuth3.getEmail(username, config.getLdapConfig());
+                //username = LdapAuth3.getEmail(username, config.getLdapConfig());
+                username = LdapAuth3.getUsername(username, config.getLdapConfig());
             }
             catch (LDAPException e) {
                 throw new KustvaktException(StatusCodes.LDAP_BASE_ERRCODE,
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 65322d9..de8b9b1 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
@@ -154,6 +154,17 @@
         assertEquals("peter@example.org", LdapAuth3.getEmail("testuser2", TEST_LDAPS_CONF));
         assertEquals(null, LdapAuth3.getEmail("non-exsting", TEST_LDAPS_CONF));
     }
+    
+    @Test
+    public void gettingUsernameForEmail() throws LDAPException {
+        assertEquals("idsTestUser", LdapAuth3.getUsername("testuser@example.com", TEST_LDAP_CONF));
+        assertEquals("testuser2", LdapAuth3.getUsername("peter@example.org", TEST_LDAPS_CONF));
+        assertEquals(null, LdapAuth3.getUsername("non-exsting", TEST_LDAPS_CONF));
+
+        assertEquals("testuser2", LdapAuth3.getUsername("testUser2", TEST_LDAPS_CONF));
+        // login with uid, get idsC2Profile username
+        assertEquals("idsTestUser", LdapAuth3.getUsername("testUser", TEST_LDAPS_CONF));
+    }
 
     @Test
     public void gettingMailAttributeForNotRegisteredUserWorks() throws LDAPException {
diff --git a/full/src/test/resources/test-ldap-users.ldif b/full/src/test/resources/test-ldap-users.ldif
index b35a919..0b92701 100644
--- a/full/src/test/resources/test-ldap-users.ldif
+++ b/full/src/test/resources/test-ldap-users.ldif
@@ -19,6 +19,7 @@
 extraProfile: testuser123
 extraPassword: password
 uid: testuser
+idsC2Profile: idsTestUser
 
 dn: uid=test,ou=people,dc=example,dc=com
 cn: Peter Test