KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > authentication > ldap > LDAPInitialDirContextFactoryImpl


1 /*
2  * Copyright (C) 2006 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.security.authentication.ldap;
18
19 import java.util.Collections JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.naming.Context JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import javax.naming.directory.Attribute JavaDoc;
26 import javax.naming.directory.Attributes JavaDoc;
27 import javax.naming.directory.BasicAttribute JavaDoc;
28 import javax.naming.directory.BasicAttributes JavaDoc;
29 import javax.naming.directory.InitialDirContext JavaDoc;
30
31 import org.alfresco.repo.security.authentication.AuthenticationException;
32 import org.alfresco.util.ApplicationContextHelper;
33 import org.springframework.context.ApplicationContext;
34
35 public class LDAPInitialDirContextFactoryImpl implements LDAPInitialDirContextFactory
36 {
37     private Map JavaDoc<String JavaDoc, String JavaDoc> initialDirContextEnvironment = Collections.<String JavaDoc, String JavaDoc> emptyMap();
38
39     static
40     {
41         System.setProperty("javax.security.auth.useSubjectCredentialsOnly", "false");
42     }
43     
44     public LDAPInitialDirContextFactoryImpl()
45     {
46         super();
47     }
48
49     public void setInitialDirContextEnvironment(Map JavaDoc<String JavaDoc, String JavaDoc> initialDirContextEnvironment)
50
51     {
52         this.initialDirContextEnvironment = initialDirContextEnvironment;
53     }
54
55     public Map JavaDoc<String JavaDoc, String JavaDoc> getInitialDirContextEnvironment()
56     {
57         return initialDirContextEnvironment;
58     }
59
60     public InitialDirContext JavaDoc getDefaultIntialDirContext() throws AuthenticationException
61     {
62         Hashtable JavaDoc<String JavaDoc, String JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>(initialDirContextEnvironment.size());
63         env.putAll(initialDirContextEnvironment);
64         env.put("javax.security.auth.useSubjectCredsOnly", "false");
65         return buildInitialDirContext(env);
66     }
67
68     private InitialDirContext JavaDoc buildInitialDirContext(Hashtable JavaDoc<String JavaDoc, String JavaDoc> env) throws AuthenticationException
69     {
70         try
71         {
72             return new InitialDirContext JavaDoc(env);
73         }
74         catch (javax.naming.AuthenticationException JavaDoc ax)
75         {
76             throw new AuthenticationException("LDAP authentication failed.", ax);
77         }
78         catch (NamingException JavaDoc nx)
79         {
80             throw new AuthenticationException("Unable to connect to LDAP Server; check LDAP configuration", nx);
81         }
82     }
83
84     public InitialDirContext JavaDoc getInitialDirContext(String JavaDoc principal, String JavaDoc credentials) throws AuthenticationException
85     {
86         if (principal == null)
87         {
88             throw new AuthenticationException("Null user name provided.");
89         }
90
91         if (credentials == null)
92         {
93             throw new AuthenticationException("No credentials provided.");
94         }
95         Hashtable JavaDoc<String JavaDoc, String JavaDoc> env = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>(initialDirContextEnvironment.size());
96         env.putAll(initialDirContextEnvironment);
97         env.put(Context.SECURITY_PRINCIPAL, principal);
98         env.put(Context.SECURITY_CREDENTIALS, credentials);
99
100         return buildInitialDirContext(env);
101     }
102
103     public static void main(String JavaDoc[] args)
104     {
105         // ....build a pyramid selling scheme .....
106

107         // A group has three user members and 2 group members .... and off we go ....
108
// We make the people and groups to represent this and stick them into LDAP ...used to populate a test data base for user and groups
109

110         int userMembers = Integer.parseInt(args[3]);
111
112         ApplicationContext applicationContext = ApplicationContextHelper.getApplicationContext();
113         LDAPInitialDirContextFactory factory = (LDAPInitialDirContextFactory) applicationContext
114                 .getBean("ldapInitialDirContextFactory");
115
116         InitialDirContext JavaDoc ctx = null;
117         try
118         {
119             ctx = factory.getInitialDirContext("cn=" + args[0] + "," + args[2], args[1]);
120
121             /* Values we'll use in creating the entry */
122             Attribute JavaDoc objClasses = new BasicAttribute JavaDoc("objectclass");
123             objClasses.add("top");
124             objClasses.add("person");
125             objClasses.add("organizationalPerson");
126             objClasses.add("inetOrgPerson");
127
128             for (int i = 0; i < userMembers; i++)
129             {
130
131                 Attribute JavaDoc cn = new BasicAttribute JavaDoc("cn", "User" + i + " TestUser");
132                 Attribute JavaDoc sn = new BasicAttribute JavaDoc("sn", "TestUser");
133                 Attribute JavaDoc givenNames = new BasicAttribute JavaDoc("givenName", "User" + i);
134                 Attribute JavaDoc telephoneNumber = new BasicAttribute JavaDoc("telephoneNumber", "123");
135                 Attribute JavaDoc uid = new BasicAttribute JavaDoc("uid", "User" + i);
136                 Attribute JavaDoc mail = new BasicAttribute JavaDoc("mail", "woof@woof");
137                 Attribute JavaDoc o = new BasicAttribute JavaDoc("o", "Alfresco");
138                 Attribute JavaDoc userPassword = new BasicAttribute JavaDoc("userPassword", "bobbins");
139                 /* Specify the DN we're adding */
140                 String JavaDoc dn = "cn=User" + i + " TestUser," + args[2];
141
142                 Attributes JavaDoc orig = new BasicAttributes JavaDoc();
143                 orig.put(objClasses);
144                 orig.put(cn);
145                 orig.put(sn);
146                 orig.put(givenNames);
147                 orig.put(telephoneNumber);
148                 orig.put(uid);
149                 orig.put(mail);
150                 orig.put(o);
151                 orig.put(userPassword);
152
153                 try
154                 {
155                     ctx.destroySubcontext(dn);
156                 }
157                 catch (NamingException JavaDoc e)
158                 {
159                     // TODO Auto-generated catch block
160
e.printStackTrace();
161                 }
162
163                 ctx.createSubcontext(dn, orig);
164             }
165
166         }
167         catch (NamingException JavaDoc e)
168         {
169             // TODO Auto-generated catch block
170
e.printStackTrace();
171         }
172         finally
173         {
174             if (ctx != null)
175             {
176                 try
177                 {
178                     ctx.close();
179                 }
180                 catch (NamingException JavaDoc e)
181                 {
182
183                     e.printStackTrace();
184                 }
185             }
186         }
187
188     }
189
190 }
191
Popular Tags