diff options
| author | Andrew Branson <andrew.branson@cern.ch> | 2014-06-05 15:02:07 +0200 |
|---|---|---|
| committer | Andrew Branson <andrew.branson@cern.ch> | 2014-06-05 15:02:07 +0200 |
| commit | d4fa3bd9dd48f4d5e26850a23f5ba48a9c10ad64 (patch) | |
| tree | 5ad7bfbce8ba9df9aad53ef33a8b908ca0680fc4 /src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java | |
| parent | 8bb86312d4f07dcb343ca2d212f4020906dbdb52 (diff) | |
LDAP refactored behind interfaces. All functions of LDAP now hidden
behind interfaces: Authenticator, Lookup and NextKeyManager (LDAP
property storage was already a ClusterStorage). Gateway holds additional
objects, and
Fixes #26 #191. Refs #27 (needs additional work for read perms and auth
tokens)
Diffstat (limited to 'src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java')
| -rw-r--r-- | src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java b/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java new file mode 100644 index 0000000..4c26de6 --- /dev/null +++ b/src/main/java/com/c2kernel/lookup/ldap/LDAPAuthManager.java @@ -0,0 +1,95 @@ +package com.c2kernel.lookup.ldap;
+
+import com.c2kernel.common.InvalidDataException;
+import com.c2kernel.common.ObjectNotFoundException;
+import com.c2kernel.process.Gateway;
+import com.c2kernel.process.auth.Authenticator;
+import com.c2kernel.utils.Logger;
+import com.novell.ldap.LDAPConnection;
+import com.novell.ldap.LDAPException;
+
+public class LDAPAuthManager implements Authenticator {
+
+ private LDAPConnection mLDAPConn;
+ private 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(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;
+ }
+ }
+
+ @Override
+ public LDAPConnection getAuthObject() {
+
+ if (!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
+ }
+
+
+}
|
