I am building a web application using the Play Framework. To authenticate users of my application, I use LDAP. I am new to the gaming platform and asynchronous programming. I wanted LDAP authentication to be asynchronous and not block my application. So I created Promise and injected an LDAP authentication code into it. My code works, but I'm not sure if it is Async or not. How can I verify that this 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 the controller, I use the above code as follows:
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")); }
source share