From eb16b04d14b3bea6cd110f21361e049e35a37822 Mon Sep 17 00:00:00 2001 From: Andrew Branson Date: Tue, 7 Oct 2014 09:17:37 +0200 Subject: Repackage to org.cristalise --- .../cristalise/lookup/ldap/LDAPAuthManager.java | 100 +++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/main/java/org/cristalise/lookup/ldap/LDAPAuthManager.java (limited to 'src/main/java/org/cristalise/lookup/ldap/LDAPAuthManager.java') diff --git a/src/main/java/org/cristalise/lookup/ldap/LDAPAuthManager.java b/src/main/java/org/cristalise/lookup/ldap/LDAPAuthManager.java new file mode 100644 index 0000000..0dfa010 --- /dev/null +++ b/src/main/java/org/cristalise/lookup/ldap/LDAPAuthManager.java @@ -0,0 +1,100 @@ +package org.cristalise.lookup.ldap; + +import org.cristalise.kernel.common.InvalidDataException; +import org.cristalise.kernel.common.ObjectNotFoundException; +import org.cristalise.kernel.process.Gateway; +import org.cristalise.kernel.process.auth.Authenticator; +import org.cristalise.kernel.utils.Logger; + +import com.novell.ldap.LDAPConnection; +import com.novell.ldap.LDAPException; + +public class LDAPAuthManager implements Authenticator { + + protected LDAPConnection mLDAPConn; + protected LDAPProperties ldapProps; + + + @Override + public boolean authenticate(String agentName, + String password, String resource) throws InvalidDataException, ObjectNotFoundException { + + ldapProps = new LDAPProperties(Gateway.getProperties()); + + if (ldapProps.mHost!=null && ldapProps.mPort!= null && ldapProps.mLocalPath!=null ) + { + try { // anonymously bind to LDAP and find the agent entry for the username + ldapProps.mUser = ""; + ldapProps.mPassword = ""; + mLDAPConn = LDAPLookupUtils.createConnection(ldapProps); + LDAPLookup anonLookup = new LDAPLookup(); + anonLookup.initPaths(ldapProps); + anonLookup.open(this); + String agentDN = anonLookup.getFullDN(anonLookup.getAgentPath(agentName)); + + //found agentDN, try to log in with it + ldapProps.mUser = agentDN; + ldapProps.mPassword = password; + mLDAPConn = LDAPLookupUtils.createConnection(ldapProps); + return true; + } catch (LDAPException e) { + return false; + } + } + else + { + throw new InvalidDataException("Cannot log in. Some connection properties are not set."); + } + + } + + @Override + public boolean authenticate(String resource) throws InvalidDataException, ObjectNotFoundException { + ldapProps = new LDAPProperties(Gateway.getProperties()); + + if (ldapProps.mUser == null || ldapProps.mUser.length()==0 || + ldapProps.mPassword == null || ldapProps.mPassword.length()==0) + throw new InvalidDataException("LDAP root user properties not found in config."); + try { + mLDAPConn = LDAPLookupUtils.createConnection(ldapProps); + return true; + } catch (LDAPException e) { + return false; + } + } + + /* (non-Javadoc) + * @see org.cristalise.kernel.process.auth.Authenticator#getAuthObject() + */ + @Override + public LDAPConnection getAuthObject() { + + if (mLDAPConn==null || !mLDAPConn.isConnected()) { + Logger.warning("LDAPAuthManager - lost connection to LDAP server. Attempting to reconnect."); + try { + mLDAPConn = LDAPLookupUtils.createConnection(ldapProps); + } catch (LDAPException ex) { } + } + return mLDAPConn; + } + + @Override + public void disconnect() { + Logger.msg(1, "LDAP Lookup: Shutting down LDAP connection."); + if (mLDAPConn != null) { + try { + mLDAPConn.disconnect(); + } catch (LDAPException e) { + Logger.error(e); + } + mLDAPConn = null; + } + + } + + public LDAPAuthManager() { + // TODO Auto-generated constructor stub + } + + +} -- cgit v1.2.3