@Test public void testAuthenticate() throws ClientException { if (!USE_EXTERNAL_TEST_LDAP_SERVER) { return; } Session session = getLDAPDirectory("userDirectory").getSession(); try { assertTrue(session.authenticate("Administrator", "Administrator")); assertTrue(session.authenticate("user1", "user1")); assertFalse(session.authenticate("Administrator", "BAD password")); assertFalse(session.authenticate("user1", "*")); assertFalse(session.authenticate("NotExistingUser", "whatever")); assertFalse(session.authenticate("*", "*")); assertFalse(session.authenticate("Administrator", "")); assertFalse(session.authenticate("user1", "")); } finally { session.close(); } }
@Override public boolean authenticate(String username, String password) throws DirectoryException { if (password == null || "".equals(password.trim())) { return false; } SearchResult entry; try { entry = getLdapEntry(username); } catch (NamingException e) { throw new DirectoryException("failed to fetch the ldap entry for " + username, e); } if (entry == null) { return false; } String dn = entry.getNameInNamespace(); Properties env = (Properties) directory.getContextProperties().clone(); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, password); InitialLdapContext authenticationDirContext = null; try { log.debug(String.format("LDAP bind dn='%s'", dn)); authenticationDirContext = new InitialLdapContext(env, null); authenticationDirContext.reconnect(null); log.debug("Bind succeeded, authentication ok"); return true; } catch (NamingException e) { log.debug("Bind failed: " + e.getMessage()); return false; } finally { try { if (authenticationDirContext != null) { authenticationDirContext.close(); } } catch (NamingException e) { log.error( "Error closing authentication context when biding dn " + dn, e); return false; } } }
LDAPSession extends BaseSession implements EntrySource { @Override public boolean authenticate(String username, String password) throws DirectoryException { if (password == null || "".equals(password.trim())) { return false; } SearchResult entry; try { entry = getLdapEntry(username); } catch (NamingException e) { throw new DirectoryException("failed to fetch the ldap entry for " + username, e); } if (entry == null) { return false; } String dn = entry.getNameInNamespace(); Properties env = (Properties) directory.getContextProperties().clone(); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, password); InitialLdapContext authenticationDirContext = null; try { log.debug(String.format("LDAP bind dn='%s'", dn)); authenticationDirContext = new InitialLdapContext(env, null); authenticationDirContext.reconnect(null); log.debug("Bind succeeded, authentication ok"); return true; } catch (NamingException e) { log.debug("Bind failed: " + e.getMessage()); return false; } finally { try { if (authenticationDirContext != null) { authenticationDirContext.close(); } } catch (NamingException e) { log.error( "Error closing authentication context when biding dn " + dn, e); return false; } } } }
LDAPSession extends BaseSession implements EntrySource { @Override public boolean authenticate(String username, String password) throws DirectoryException { if (password == null || "".equals(password.trim())) { return false; } SearchResult entry; try { entry = getLdapEntry(username); } catch (NamingException e) { throw new DirectoryException("failed to fetch the ldap entry for " + username, e); } if (entry == null) { return false; } String dn = entry.getNameInNamespace(); Properties env = (Properties) directory.getContextProperties().clone(); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, password); InitialLdapContext authenticationDirContext = null; try { log.debug(String.format("LDAP bind dn='%s'", dn)); authenticationDirContext = new InitialLdapContext(env, null); authenticationDirContext.reconnect(null); log.debug("Bind succeeded, authentication ok"); return true; } catch (NamingException e) { log.debug("Bind failed: " + e.getMessage()); return false; } finally { try { if (authenticationDirContext != null) { authenticationDirContext.close(); } } catch (NamingException e) { log.error( "Error closing authentication context when biding dn " + dn, e); return false; } } } LDAPSession(LDAPDirectory directory, DirContext dirContext); }
LDAPSession extends BaseSession implements EntrySource { @Override public boolean authenticate(String username, String password) throws DirectoryException { if (password == null || "".equals(password.trim())) { return false; } SearchResult entry; try { entry = getLdapEntry(username); } catch (NamingException e) { throw new DirectoryException("failed to fetch the ldap entry for " + username, e); } if (entry == null) { return false; } String dn = entry.getNameInNamespace(); Properties env = (Properties) directory.getContextProperties().clone(); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, password); InitialLdapContext authenticationDirContext = null; try { log.debug(String.format("LDAP bind dn='%s'", dn)); authenticationDirContext = new InitialLdapContext(env, null); authenticationDirContext.reconnect(null); log.debug("Bind succeeded, authentication ok"); return true; } catch (NamingException e) { log.debug("Bind failed: " + e.getMessage()); return false; } finally { try { if (authenticationDirContext != null) { authenticationDirContext.close(); } } catch (NamingException e) { log.error( "Error closing authentication context when biding dn " + dn, e); return false; } } } LDAPSession(LDAPDirectory directory, DirContext dirContext); void setSubStringMatchType(String type); Directory getDirectory(); DirContext getContext(); @Override @SuppressWarnings("unchecked") DocumentModel createEntry(Map<String, Object> fieldMap); @Override DocumentModel getEntry(String id); @Override DocumentModel getEntry(String id, boolean fetchReferences); @Override DocumentModel getEntryFromSource(String id, boolean fetchReferences); @Override boolean hasEntry(String id); @Override DocumentModelList getEntries(); @Override @SuppressWarnings("unchecked") void updateEntry(DocumentModel docModel); @Override void deleteEntry(DocumentModel dm); @Override void deleteEntry(String id); @Override void deleteEntry(String id, Map<String, String> map); DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, boolean fetchReferences,
            Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy,
            boolean fetchReferences); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext); @Override void close(); @Override List<String> getProjection(Map<String, Serializable> filter,
            String columnName); @Override List<String> getProjection(Map<String, Serializable> filter,
            Set<String> fulltext, String columnName); @Override boolean authenticate(String username, String password); @Override String getIdField(); @Override String getPasswordField(); @Override boolean isAuthenticating(); @Override boolean isReadOnly(); boolean rdnMatchesIdField(); @Override // useful for the log function String toString(); @Override DocumentModel createEntry(DocumentModel entry); }
LDAPSession extends BaseSession implements EntrySource { @Override public boolean authenticate(String username, String password) throws DirectoryException { if (password == null || "".equals(password.trim())) { return false; } SearchResult entry; try { entry = getLdapEntry(username); } catch (NamingException e) { throw new DirectoryException("failed to fetch the ldap entry for " + username, e); } if (entry == null) { return false; } String dn = entry.getNameInNamespace(); Properties env = (Properties) directory.getContextProperties().clone(); env.put(Context.SECURITY_PRINCIPAL, dn); env.put(Context.SECURITY_CREDENTIALS, password); InitialLdapContext authenticationDirContext = null; try { log.debug(String.format("LDAP bind dn='%s'", dn)); authenticationDirContext = new InitialLdapContext(env, null); authenticationDirContext.reconnect(null); log.debug("Bind succeeded, authentication ok"); return true; } catch (NamingException e) { log.debug("Bind failed: " + e.getMessage()); return false; } finally { try { if (authenticationDirContext != null) { authenticationDirContext.close(); } } catch (NamingException e) { log.error( "Error closing authentication context when biding dn " + dn, e); return false; } } } LDAPSession(LDAPDirectory directory, DirContext dirContext); void setSubStringMatchType(String type); Directory getDirectory(); DirContext getContext(); @Override @SuppressWarnings("unchecked") DocumentModel createEntry(Map<String, Object> fieldMap); @Override DocumentModel getEntry(String id); @Override DocumentModel getEntry(String id, boolean fetchReferences); @Override DocumentModel getEntryFromSource(String id, boolean fetchReferences); @Override boolean hasEntry(String id); @Override DocumentModelList getEntries(); @Override @SuppressWarnings("unchecked") void updateEntry(DocumentModel docModel); @Override void deleteEntry(DocumentModel dm); @Override void deleteEntry(String id); @Override void deleteEntry(String id, Map<String, String> map); DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, boolean fetchReferences,
            Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext, Map<String, String> orderBy,
            boolean fetchReferences); @Override DocumentModelList query(Map<String, Serializable> filter,
            Set<String> fulltext); @Override void close(); @Override List<String> getProjection(Map<String, Serializable> filter,
            String columnName); @Override List<String> getProjection(Map<String, Serializable> filter,
            Set<String> fulltext, String columnName); @Override boolean authenticate(String username, String password); @Override String getIdField(); @Override String getPasswordField(); @Override boolean isAuthenticating(); @Override boolean isReadOnly(); boolean rdnMatchesIdField(); @Override // useful for the log function String toString(); @Override DocumentModel createEntry(DocumentModel entry);  }