KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > ldap > UserLDAPService


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 JavaDoc;
7 import javax.naming.NamingEnumeration JavaDoc;
8 import javax.naming.NamingException JavaDoc;
9 import javax.naming.directory.*;
10 import java.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.FileNotFoundException JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Properties JavaDoc;
16
17
18 /**
19  * This class manages LDAP persistance for users. For the moment it's main use
20  * is to authentificate the user on an LDAP repository.
21  *
22  * @author Serge Huber
23  * @version 1.0
24  */

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 JavaDoc DEFAULT_CONFIGURATION_FILE = "users.ldap.properties";
33
34     private static String JavaDoc CONTEXT_FACTORY_PROP = "users.ldap.context.factory";
35     private static String JavaDoc LDAP_URL_PROP = "users.ldap.url";
36     private static String JavaDoc AUTHENTIFICATION_MODE_PROP = "users.ldap.authentification.mode";
37     private static String JavaDoc PUBLIC_BIND_DN_PROP = "users.ldap.public.bind.dn";
38     private static String JavaDoc UID_SEARCH_ATTRIBUTE_PROP = "users.ldap.uid.search.attribute";
39     private static String JavaDoc UID_SEARCH_NAME_PROP = "users.ldap.uid.search.name";
40     private static String JavaDoc DN_IDENTIFIER_ATTRIBUTE_PROP = "users.ldap.dn.identifier.attribute";
41
42     private Properties JavaDoc ldapProperties = null;
43
44     /**
45      * return the singleton instance
46      */

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 JavaDoc configPath = jSettings.getJahiaLdapDiskPath ();
59         String JavaDoc configFileName;
60
61         File JavaDoc configFile = new File JavaDoc (configPath + File.separator + DEFAULT_CONFIGURATION_FILE);
62         if (configFile.exists ()) {
63
64             configFileName = configPath + File.separator + DEFAULT_CONFIGURATION_FILE;
65
66             try {
67                 File JavaDoc ldapPropFile = new File JavaDoc (configFileName);
68                 FileInputStream JavaDoc ldapPropInputStr = new FileInputStream JavaDoc (ldapPropFile);
69                 ldapProperties = new Properties JavaDoc ();
70                 ldapProperties.load (ldapPropInputStr);
71                 ldapPropInputStr.close ();
72             } catch (FileNotFoundException JavaDoc fnfe) {
73                 logger.error (fnfe);
74             } catch (IOException JavaDoc 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     /**
90      *
91      */

92     public String JavaDoc login (String JavaDoc userID, String JavaDoc userPassword) {
93         String JavaDoc 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 JavaDoc e) {
110             personName = null;
111         }
112         return personName;
113     }
114
115     private DirContext connectToPublicDir ()
116             throws NamingException JavaDoc {
117         // Identify service provider to use
118
Hashtable JavaDoc publicEnv = new Hashtable JavaDoc (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         // Create the initial directory context
130
ctx = new InitialDirContext (publicEnv);
131         return ctx;
132     }
133
134     private DirContext connectToPrivateDir (String JavaDoc personName, String JavaDoc personPassword)
135             throws NamingException JavaDoc {
136         // Identify service provider to use
137
Hashtable JavaDoc privateEnv = new Hashtable JavaDoc (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         // Create the initial directory context
151
DirContext ctx = new InitialDirContext (privateEnv);
152         return ctx;
153     }
154
155     private void disconnectDir (DirContext ctx)
156             throws NamingException JavaDoc {
157         ctx.close ();
158     }
159
160     private String JavaDoc findNamebyUID (DirContext ctx, String JavaDoc uid)
161             throws NamingException JavaDoc {
162         String JavaDoc personName = null;
163
164         // Search for objects that have those matching attributes
165
SearchControls searchCtl = new SearchControls ();
166         searchCtl.setSearchScope (SearchControls.SUBTREE_SCOPE);
167         NamingEnumeration JavaDoc 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             // we only take the first value if there are multiple answers, which
174
// should normally NOT happend if the uid is unique !!
175
SearchResult sr = (SearchResult) answer.next ();
176             Attributes attrs = sr.getAttributes ();
177             personName =
178                     (String JavaDoc) 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