1 package org.jahia.services.ldap; 2 3 import org.jahia.exceptions.JahiaInitializationException; 4 import org.jahia.settings.SettingsBean; 5 6 import javax.naming.Context ; 7 import javax.naming.NamingEnumeration ; 8 import javax.naming.NamingException ; 9 import javax.naming.directory.*; 10 import java.io.File ; 11 import java.io.FileInputStream ; 12 import java.io.FileNotFoundException ; 13 import java.io.IOException ; 14 import java.util.Hashtable ; 15 import java.util.Properties ; 16 17 18 25 public class UserLDAPService extends LDAPService { 26 27 private static org.apache.log4j.Logger logger = 28 org.apache.log4j.Logger.getLogger (UserLDAPService.class); 29 30 private static UserLDAPService m_Instance; 31 32 private static String DEFAULT_CONFIGURATION_FILE = "users.ldap.properties"; 33 34 private static String CONTEXT_FACTORY_PROP = "users.ldap.context.factory"; 35 private static String LDAP_URL_PROP = "users.ldap.url"; 36 private static String AUTHENTIFICATION_MODE_PROP = "users.ldap.authentification.mode"; 37 private static String PUBLIC_BIND_DN_PROP = "users.ldap.public.bind.dn"; 38 private static String UID_SEARCH_ATTRIBUTE_PROP = "users.ldap.uid.search.attribute"; 39 private static String UID_SEARCH_NAME_PROP = "users.ldap.uid.search.name"; 40 private static String DN_IDENTIFIER_ATTRIBUTE_PROP = "users.ldap.dn.identifier.attribute"; 41 42 private Properties ldapProperties = null; 43 44 47 public static synchronized UserLDAPService getInstance () { 48 49 if (m_Instance == null) { 50 m_Instance = new UserLDAPService (); 51 } 52 53 return m_Instance; 54 } 55 56 public void init (SettingsBean jSettings) 57 throws JahiaInitializationException { 58 String configPath = jSettings.getJahiaLdapDiskPath (); 59 String configFileName; 60 61 File configFile = new File (configPath + File.separator + DEFAULT_CONFIGURATION_FILE); 62 if (configFile.exists ()) { 63 64 configFileName = configPath + File.separator + DEFAULT_CONFIGURATION_FILE; 65 66 try { 67 File ldapPropFile = new File (configFileName); 68 FileInputStream ldapPropInputStr = new FileInputStream (ldapPropFile); 69 ldapProperties = new Properties (); 70 ldapProperties.load (ldapPropInputStr); 71 ldapPropInputStr.close (); 72 } catch (FileNotFoundException fnfe) { 73 logger.error (fnfe); 74 } catch (IOException ioe) { 75 logger.error ("UserLDAPService.init", ioe); 76 } 77 78 } else { 79 logger.debug ( 80 "Config file not found in " + configPath + File.separator + DEFAULT_CONFIGURATION_FILE); 81 } 82 logger.debug ("Initialized"); 83 } 84 85 private UserLDAPService () { 86 } 87 88 89 92 public String login (String userID, String userPassword) { 93 String personName = null; 94 95 try { 96 DirContext publicCtx = connectToPublicDir (); 97 if (publicCtx != null) { 98 personName = findNamebyUID (publicCtx, userID); 99 } 100 disconnectDir (publicCtx); 101 102 DirContext privateCtx = null; 103 privateCtx = connectToPrivateDir (personName, userPassword); 104 if (privateCtx != null) { 105 } else { 106 personName = null; 107 } 108 disconnectDir (privateCtx); 109 } catch (NamingException e) { 110 personName = null; 111 } 112 return personName; 113 } 114 115 private DirContext connectToPublicDir () 116 throws NamingException { 117 Hashtable publicEnv = new Hashtable (11); 119 publicEnv.put (Context.INITIAL_CONTEXT_FACTORY, 120 ldapProperties.getProperty (CONTEXT_FACTORY_PROP)); 121 publicEnv.put (Context.PROVIDER_URL, 122 ldapProperties.getProperty (LDAP_URL_PROP)); 123 publicEnv.put (Context.SECURITY_AUTHENTICATION, 124 ldapProperties.getProperty (AUTHENTIFICATION_MODE_PROP)); 125 publicEnv.put (Context.SECURITY_PRINCIPAL, 126 ldapProperties.getProperty (PUBLIC_BIND_DN_PROP)); 127 128 DirContext ctx = null; 129 ctx = new InitialDirContext (publicEnv); 131 return ctx; 132 } 133 134 private DirContext connectToPrivateDir (String personName, String personPassword) 135 throws NamingException { 136 Hashtable privateEnv = new Hashtable (11); 138 privateEnv.put (Context.INITIAL_CONTEXT_FACTORY, 139 ldapProperties.getProperty (CONTEXT_FACTORY_PROP)); 140 privateEnv.put (Context.PROVIDER_URL, 141 ldapProperties.getProperty (LDAP_URL_PROP)); 142 privateEnv.put (Context.SECURITY_AUTHENTICATION, 143 ldapProperties.getProperty (AUTHENTIFICATION_MODE_PROP)); 144 privateEnv.put (Context.SECURITY_PRINCIPAL, 145 personName + "," + 146 ldapProperties.getProperty (UID_SEARCH_NAME_PROP)); 147 privateEnv.put (Context.SECURITY_CREDENTIALS, 148 personPassword); 149 150 DirContext ctx = new InitialDirContext (privateEnv); 152 return ctx; 153 } 154 155 private void disconnectDir (DirContext ctx) 156 throws NamingException { 157 ctx.close (); 158 } 159 160 private String findNamebyUID (DirContext ctx, String uid) 161 throws NamingException { 162 String personName = null; 163 164 SearchControls searchCtl = new SearchControls (); 166 searchCtl.setSearchScope (SearchControls.SUBTREE_SCOPE); 167 NamingEnumeration answer = ctx.search ( 168 ldapProperties.getProperty (UID_SEARCH_NAME_PROP), 169 ldapProperties.getProperty (UID_SEARCH_ATTRIBUTE_PROP) + "=" + uid, 170 searchCtl); 171 172 if (answer.hasMore ()) { 173 SearchResult sr = (SearchResult) answer.next (); 176 Attributes attrs = sr.getAttributes (); 177 personName = 178 (String ) attrs.get ( 179 ldapProperties.getProperty (DN_IDENTIFIER_ATTRIBUTE_PROP)) 180 .get (); 181 personName = sr.getName (); 182 } 183 return personName; 184 } 185 186 } 187 | Popular Tags |