Implemented mapping of LDAP username to email.

Change-Id: Ifb1d2969c6dfafe768b99524de8303277184a94d
diff --git a/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java b/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
index c135506..fefa17c 100644
--- a/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
+++ b/full/src/main/java/de/ids_mannheim/korap/authentication/KustvaktAuthenticationManager.java
@@ -438,9 +438,9 @@
 		User unknown = null;
 		// just to make sure that the plain password does not appear anywhere in
 		// the logs!
-
-		System.out.printf("Debug: authenticateIdM: entering for '%s'...\n", username);
-
+		if (DEBUG){
+            jlog.debug("Debug: authenticateIdM: entering for '%s'...\n", username);
+		}
 		/**
 		 * wozu Apache Validatoren für User/Passwort für IdM/LDAP? siehe
 		 * validation.properties. Abgeschaltet 21.04.17/FB try {
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 c4dd257..786398f 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
@@ -21,7 +21,11 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import com.unboundid.ldap.sdk.LDAPException;
+
+import de.ids_mannheim.korap.authentication.LdapAuth3;
 import de.ids_mannheim.korap.config.Attributes;
+import de.ids_mannheim.korap.constant.AuthenticationMethod;
 import de.ids_mannheim.korap.encryption.RandomCodeGenerator;
 import de.ids_mannheim.korap.exceptions.KustvaktException;
 import de.ids_mannheim.korap.exceptions.StatusCodes;
@@ -262,6 +266,18 @@
 
         Set<AccessScope> accessScopes =
                 scopeService.convertToAccessScope(scopes);
+        
+        if (config.getOAuth2passwordAuthentication()
+                .equals(AuthenticationMethod.LDAP)) {
+            try {
+                username = LdapAuth3.getEmail(username, config.getLdapConfig());
+            }
+            catch (LDAPException e) {
+                throw new KustvaktException(StatusCodes.LDAP_BASE_ERRCODE,
+                        e.getExceptionMessage());
+            }
+        }
+        
         return createsAccessTokenResponse(scopes, accessScopes, clientId,
                 username, authenticationTime,
                 false);