Async LDAP authentication with play framework

531 views Asked by At

I am building a web application with the Play Framework. To authenticate the users of my application I use LDAP. I am new to the play framework and async programming. I wanted the LDAP authentication to be async and do not block my app. So I created a Promise and put the LDAP authentication code in it. My code is working but I am not sure if it is Async or not. How can I verify that it is really Async. Here is my code

public class ActiveDirectoryServices {

public static final String ldapURL =      Play.application().configuration().getString("ActiveDirectory.url");
public static final String domainName =   Play.application().configuration().getString("ActoveDirectory.DomainName");
public static final int timeout =         Play.application().configuration().getInt("ActoveDirectory.timeout");
private static final String account = "account";
private static final String pass = "password";

public static Promise<Boolean> authenticate(String username, String password) throws AuthenticationException, CommunicationException, NamingException{

    Hashtable<String, String> env = new Hashtable<String,String>();     
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put("com.sun.jndi.ldap.connect.timeout", ""+(timeout*1000));
    env.put(Context.PROVIDER_URL, ldapURL);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, username+domainName);
    env.put(Context.SECURITY_CREDENTIALS, password);

    DirContext authContext = null; 
    authContext = new InitialDirContext(env);        
    return Promise.pure(Boolean.TRUE);                         
}

}

Then in a controller I use the above code as following:

try {

    Promise<Boolean> promiseActiveDirectoryCheck = ActiveDirectoryServices.authenticate(userName, password);
        return promiseActiveDirectoryCheck.flatMap(response -> {

        if(response){                           
          return Promise.pure(ok("access granted"));
        }


    });

}catch (AuthenticationException exp) {
      return Promise.pure(ok("access denied"));

}catch (CommunicationException exp) {
      return Promise.pure(ok("The active directory server is not reachable"));

}catch (NamingException exp) {
      return Promise.pure(ok("active directory domain name does not exist"));

}
0

There are 0 answers