KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > security > management > jndi > JNDIUserManagerImpl


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17 package org.sape.carbon.services.security.management.jndi;
18
19 import java.security.Principal JavaDoc;
20 import java.security.acl.Group JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import javax.naming.Context JavaDoc;
27 import javax.naming.NameAlreadyBoundException JavaDoc;
28 import javax.naming.NameNotFoundException JavaDoc;
29 import javax.naming.NamingEnumeration JavaDoc;
30 import javax.naming.NamingException JavaDoc;
31 import javax.naming.directory.Attribute JavaDoc;
32 import javax.naming.directory.Attributes JavaDoc;
33 import javax.naming.directory.BasicAttribute JavaDoc;
34 import javax.naming.directory.BasicAttributes JavaDoc;
35 import javax.naming.directory.DirContext JavaDoc;
36 import javax.naming.directory.SearchResult JavaDoc;
37
38 import org.sape.carbon.core.component.Component;
39 import org.sape.carbon.core.component.ComponentConfiguration;
40 import org.sape.carbon.core.component.lifecycle.Configurable;
41 import org.sape.carbon.core.component.lifecycle.Initializable;
42 import org.sape.carbon.core.config.InvalidConfigurationException;
43 import org.sape.carbon.services.security.management.DefaultGroupImpl;
44 import org.sape.carbon.services.security.management.DefaultUserImpl;
45 import org.sape.carbon.services.security.management.DuplicateGroupException;
46 import org.sape.carbon.services.security.management.DuplicatePrincipalException;
47 import org.sape.carbon.services.security.management.RuntimeSecurityManagementException;
48 import org.sape.carbon.services.security.management.UnknownGroupException;
49 import org.sape.carbon.services.security.management.UnknownPrincipalException;
50 import org.sape.carbon.services.security.management.UserManager;
51
52 /**
53  * <p></p>
54  *
55  * Copyright 2003 Sapient
56  * @since carbon 2.1
57  * @author Douglas Voet, Jun 12, 2003
58  * @version $Revision: 1.2 $($Author: dvoet $ / $Date: 2003/11/03 19:48:31 $)
59  */

60 public class JNDIUserManagerImpl implements UserManager, Initializable, Configurable {
61     
62     protected String JavaDoc serviceName;
63     protected JNDIUserManagerConfiguration config;
64
65     /* (non-Javadoc)
66      * @see org.sape.carbon.services.security.management.UserManager#createUser(java.lang.String, java.lang.Object)
67      */

68     public Principal JavaDoc createUser(String JavaDoc userName, Map JavaDoc userInfo)
69         throws DuplicatePrincipalException {
70             
71         Principal JavaDoc newPrincipal = new DefaultUserImpl(userName);
72         
73         try {
74             Attributes JavaDoc userAttributes = new BasicAttributes JavaDoc();
75             
76             userAttributes.put(new BasicAttribute JavaDoc(
77                 this.config.getUserNameAttribute(),
78                 userName));
79                 
80             Iterator JavaDoc userInfoIterator = userInfo.entrySet().iterator();
81             while (userInfoIterator.hasNext()) {
82                 Map.Entry JavaDoc attribute = (Map.Entry JavaDoc) userInfoIterator.next();
83                 
84                 userAttributes.put(new BasicAttribute JavaDoc(
85                     attribute.getKey().toString(),
86                     attribute.getValue()));
87             }
88                 
89             getUserParentContext().createSubcontext(
90                 getUserContextName(userName),
91                 userAttributes);
92                 
93         } catch (NameAlreadyBoundException JavaDoc nabe) {
94             throw new DuplicatePrincipalException(
95                 this.getClass(),
96                 newPrincipal,
97                 nabe);
98                 
99         } catch (NamingException JavaDoc ne) {
100             throw new RuntimeSecurityManagementException(
101                 this.getClass(),
102                 "Caught NamingException adding Principal to directory",
103                 ne);
104         }
105
106         return newPrincipal;
107     }
108
109     /* (non-Javadoc)
110      * @see org.sape.carbon.services.security.management.UserManager#authenticate(java.lang.String, java.lang.Object)
111      */

112     public boolean authenticate(String JavaDoc userName, Object JavaDoc credential) {
113         try {
114             Attributes JavaDoc userAttributes = new BasicAttributes JavaDoc();
115             userAttributes.put(this.config.getUserNameAttribute(), userName);
116             userAttributes.put(this.config.getCredentialAttribute(), credential);
117             
118             // if the query returns results, the user exists with the
119
// specified credentials and is authentic,
120
// otherwise, the user is either unknown or the credentials are
121
// invalid
122
return getUserParentContext().search("", userAttributes).hasMore();
123
124         } catch (NamingException JavaDoc ne) {
125             throw new RuntimeSecurityManagementException(
126                 this.getClass(),
127                 "Caught NamingException authenticating user",
128                 ne);
129         }
130     }
131
132     /* (non-Javadoc)
133      * @see org.sape.carbon.services.security.management.UserManager#removeUser(java.security.Principal)
134      */

135     public void removeUser(Principal JavaDoc user) throws UnknownPrincipalException {
136         try {
137             // remove user
138
getUserParentContext().unbind(getUserContextName(user.getName()));
139             
140             // remove user from groups
141
removeUserFromGroups(user);
142             
143         } catch (NameNotFoundException JavaDoc nfe) {
144             throw new UnknownPrincipalException(
145                 this.getClass(),
146                 user,
147                 nfe);
148         } catch (NamingException JavaDoc ne) {
149             throw new RuntimeSecurityManagementException(
150                 this.getClass(),
151                 "Caught NamingException removing Principal",
152                 ne);
153         }
154     }
155     
156     protected void removeUserFromGroups(Principal JavaDoc user) throws NamingException JavaDoc {
157         Attributes JavaDoc searchCriteria = new BasicAttributes JavaDoc(
158             this.config.getUserNameAttribute(),
159             user.getName());
160         
161         NamingEnumeration JavaDoc groups =
162             getGroupParentContext().search("", searchCriteria);
163             
164         while (groups.hasMore()) {
165             SearchResult JavaDoc group = (SearchResult JavaDoc) groups.next();
166             
167             Attribute JavaDoc members =
168                 group.getAttributes().get(this.config.getMembersAttribute());
169                 
170             members.remove(user.getName());
171             
172             this.getGroupParentContext().modifyAttributes(
173                 group.getName(),
174                 DirContext.REPLACE_ATTRIBUTE,
175                 group.getAttributes());
176         }
177     }
178
179     /* (non-Javadoc)
180      * @see org.sape.carbon.services.security.management.UserManager#updateCredential(java.security.Principal, java.lang.Object)
181      */

182     public void updateCredential(Principal JavaDoc user, Object JavaDoc credential)
183         throws UnknownPrincipalException {
184             
185         try {
186             Attributes JavaDoc newCredential = new BasicAttributes JavaDoc(
187                 this.config.getCredentialAttribute(),
188                 credential);
189                 
190             getUserParentContext().modifyAttributes(
191                 getUserContextName(user.getName()),
192                 DirContext.REPLACE_ATTRIBUTE,
193                 newCredential);
194                 
195         } catch (NameNotFoundException JavaDoc nfe) {
196             throw new UnknownPrincipalException(
197                 this.getClass(),
198                 user,
199                 nfe);
200                 
201         } catch (NamingException JavaDoc ne) {
202             throw new RuntimeSecurityManagementException(
203                 this.getClass(),
204                 "Caught NamingException updating credentials",
205                 ne);
206         }
207     }
208
209     /* (non-Javadoc)
210      * @see org.sape.carbon.services.security.management.UserManager#createGroup(java.lang.String)
211      */

212     public Group JavaDoc createGroup(String JavaDoc groupName) throws DuplicateGroupException {
213         
214         Group JavaDoc newGroup = constructGroup(groupName);
215         
216         try {
217             Attributes JavaDoc groupAttributes = new BasicAttributes JavaDoc(
218                 this.config.getGroupNameAttribute(),
219                 groupName);
220             
221             getGroupParentContext().createSubcontext(
222                 getGroupContextName(groupName),
223                 groupAttributes);
224                 
225         } catch (NameAlreadyBoundException JavaDoc nabe) {
226             throw new DuplicateGroupException(
227                 this.getClass(),
228                 newGroup,
229                 nabe);
230                 
231         } catch (NamingException JavaDoc ne) {
232             throw new RuntimeSecurityManagementException(
233                 this.getClass(),
234                 "Caught NamingException adding Group to directory",
235                 ne);
236         }
237
238         return newGroup;
239     }
240
241     /* (non-Javadoc)
242      * @see org.sape.carbon.services.security.management.UserManager#removeGroup(java.security.acl.Group)
243      */

244     public void removeGroup(Group JavaDoc group) throws UnknownGroupException {
245         try {
246             getGroupParentContext().unbind(getGroupContextName(group.getName()));
247
248         } catch (NameNotFoundException JavaDoc nfe) {
249             throw new UnknownGroupException(
250                 this.getClass(),
251                 group.getName(),
252                 nfe);
253         } catch (NamingException JavaDoc ne) {
254             throw new RuntimeSecurityManagementException(
255                 this.getClass(),
256                 "Caught NamingException removing Group, group name [" +
257                     group.getName() + "]",
258                 ne);
259         }
260     }
261
262     /* (non-Javadoc)
263      * @see org.sape.carbon.services.security.management.UserManager#retreiveUser(java.lang.String)
264      */

265     public Principal JavaDoc retreiveUser(String JavaDoc userName) {
266         try {
267             getUserParentContext().lookup(getUserContextName(userName));
268             // did not get an exception, so userName exists
269
// construct a new user Principal and return it
270
return constructUser(userName);
271         } catch (NameNotFoundException JavaDoc nfe) {
272             // null is the valid response if userName not found
273
return null;
274         } catch (NamingException JavaDoc ne) {
275             throw new RuntimeSecurityManagementException(
276                 this.getClass(),
277                 "Caught NamingException looking up user context, userName [" +
278                     userName + "]",
279                 ne);
280         }
281     }
282
283     /* (non-Javadoc)
284      * @see org.sape.carbon.services.security.management.UserManager#retreiveGroup(java.lang.String)
285      */

286     public Group JavaDoc retreiveGroup(String JavaDoc groupName) {
287         try {
288             getGroupParentContext().lookup(getGroupContextName(groupName));
289             // did not get an exception, so groupName exists
290
// construct a new group Principal and return it
291
return constructGroup(groupName);
292         } catch (NameNotFoundException JavaDoc nfe) {
293             // null is the valid response if userName not found
294
return null;
295         } catch (NamingException JavaDoc ne) {
296             throw new RuntimeSecurityManagementException(
297                 this.getClass(),
298                 "Caught NamingException looking up group context, groupName [" +
299                     groupName + "]",
300                 ne);
301         }
302     }
303
304     /* (non-Javadoc)
305      * @see org.sape.carbon.services.security.management.UserManager#retreiveGroups(java.security.Principal)
306      */

307     public Set JavaDoc retreiveGroups(Principal JavaDoc principal)
308         throws UnknownPrincipalException {
309
310         try {
311             Set JavaDoc groups = new HashSet JavaDoc();
312             
313             Attributes JavaDoc searchCriteria = new BasicAttributes JavaDoc(
314                 this.config.getMembersAttribute(),
315                 principal.getName());
316                 
317             NamingEnumeration JavaDoc groupResults =
318                 getGroupParentContext().search("", searchCriteria);
319                 
320             while (groupResults.hasMore()) {
321                 SearchResult JavaDoc group = (SearchResult JavaDoc) groupResults.next();
322                 String JavaDoc groupName = group.getAttributes().get(
323                     this.config.getGroupNameAttribute()).get().toString();
324                 groups.add(constructGroup(groupName));
325             }
326             
327             return groups;
328             
329         } catch (NamingException JavaDoc ne) {
330             throw new RuntimeSecurityManagementException(
331                 this.getClass(),
332                 "Caught NamingException looking up groups for principal [" +
333                     principal.getName() + "]",
334                 ne);
335         }
336     }
337
338     /* (non-Javadoc)
339      * @see org.sape.carbon.services.security.management.UserManager#addPrincipalToGroup(java.security.Principal, java.security.acl.Group)
340      */

341     public boolean addPrincipalToGroup(Principal JavaDoc principal, Group JavaDoc group)
342         throws UnknownPrincipalException, UnknownGroupException {
343             
344         try {
345             Context JavaDoc principalContext = lookupPrincipalContext(principal);
346         
347             Attributes JavaDoc groupAttributes = getGroupParentContext().getAttributes(
348                 getGroupContextName(group.getName()),
349                 new String JavaDoc[] { this.config.getGroupNameAttribute() });
350             
351             Attribute JavaDoc members =
352                 groupAttributes.get(this.config.getGroupNameAttribute());
353                 
354             // TODO DV does this affect groupAttributes?
355
boolean memberAdded =
356                 members.add(principalContext.getNameInNamespace());
357                 
358             if (memberAdded) {
359                 getGroupParentContext().modifyAttributes(
360                     getGroupContextName(group.getName()),
361                     DirContext.REPLACE_ATTRIBUTE,
362                     groupAttributes);
363             }
364             
365             return memberAdded;
366             
367         } catch (NameNotFoundException JavaDoc nfe) {
368             throw new UnknownGroupException(
369                 this.getClass(),
370                 group.getName(),
371                 nfe);
372         } catch (NamingException JavaDoc ne) {
373             throw new RuntimeSecurityManagementException(
374                 this.getClass(),
375                 "Caught NamingException adding Principal to group",
376                 ne);
377         }
378     }
379
380     /* (non-Javadoc)
381      * @see org.sape.carbon.services.security.management.UserManager#removePrincipalFromGroup(java.security.Principal, java.security.acl.Group)
382      */

383     public boolean removePrincipalFromGroup(Principal JavaDoc principal, Group JavaDoc group)
384         throws UnknownPrincipalException, UnknownGroupException {
385
386         try {
387             Context JavaDoc principalContext = lookupPrincipalContext(principal);
388         
389             Attributes JavaDoc groupAttributes = getGroupParentContext().getAttributes(
390                 getGroupContextName(group.getName()),
391                 new String JavaDoc[] { this.config.getGroupNameAttribute() });
392         
393             Attribute JavaDoc members =
394                 groupAttributes.get(this.config.getGroupNameAttribute());
395             
396             // TODO DV does this affect groupAttributes?
397
boolean memberRemoved =
398                 members.remove(principalContext.getNameInNamespace());
399             
400             if (memberRemoved) {
401                 getGroupParentContext().modifyAttributes(
402                     getGroupContextName(group.getName()),
403                     DirContext.REPLACE_ATTRIBUTE,
404                     groupAttributes);
405             }
406         
407             return memberRemoved;
408         
409         } catch (NameNotFoundException JavaDoc nfe) {
410             throw new UnknownGroupException(
411                 this.getClass(),
412                 group.getName(),
413                 nfe);
414         } catch (NamingException JavaDoc ne) {
415             throw new RuntimeSecurityManagementException(
416                 this.getClass(),
417                 "Caught NamingException adding Principal to group",
418                 ne);
419         }
420     }
421     
422     private Context JavaDoc lookupPrincipalContext(Principal JavaDoc principal)
423         throws UnknownPrincipalException, NamingException JavaDoc {
424             
425         Context JavaDoc principalContext;
426         // ensure that principal exists
427
try {
428             // is principal a user?
429
principalContext = (Context JavaDoc) getUserParentContext().
430                 lookup(getUserContextName(principal.getName()));
431         } catch (NameNotFoundException JavaDoc nfe) {
432             try {
433                 // principal is not a user, is it a group?
434
principalContext = (Context JavaDoc) getGroupParentContext().
435                     lookup(getGroupContextName(principal.getName()));
436             } catch (NameNotFoundException JavaDoc nfe2) {
437                 // principal was not found as a user or group
438
throw new UnknownPrincipalException(
439                     this.getClass(),
440                     principal,
441                     nfe2);
442             }
443         }
444         
445         return principalContext;
446     }
447
448     /* (non-Javadoc)
449      * @see org.sape.carbon.services.security.management.UserManager#retreiveAllUserNames()
450      */

451     public Set JavaDoc retreiveAllUserNames() {
452         try {
453             NamingEnumeration JavaDoc allUserContexts =
454                 getUserParentContext().search("", null);
455                 
456             Set JavaDoc allUserNames = new HashSet JavaDoc();
457             while (allUserContexts.hasMore()) {
458                 SearchResult JavaDoc user = (SearchResult JavaDoc) allUserContexts.next();
459                 
460                 allUserNames.add(user.getAttributes().get(
461                     this.config.getUserNameAttribute()).get());
462             }
463             
464             return allUserNames;
465         } catch (NamingException JavaDoc ne) {
466             throw new RuntimeSecurityManagementException(
467                 this.getClass(),
468                 "Caught NamingException retrieving all user names",
469                 ne);
470         }
471     }
472
473     /* (non-Javadoc)
474      * @see org.sape.carbon.services.security.management.UserManager#retreiveAllGroupNames()
475      */

476     public Set JavaDoc retreiveAllGroupNames() {
477         try {
478             NamingEnumeration JavaDoc allGroupContexts =
479                 getGroupParentContext().search("", null);
480                 
481             Set JavaDoc allGroupNames = new HashSet JavaDoc();
482             while (allGroupContexts.hasMore()) {
483                 SearchResult JavaDoc group = (SearchResult JavaDoc) allGroupContexts.next();
484                 
485                 allGroupNames.add(group.getAttributes().get(
486                     this.config.getGroupNameAttribute()).get());
487             }
488             
489             return allGroupNames;
490         } catch (NamingException JavaDoc ne) {
491             throw new RuntimeSecurityManagementException(
492                 this.getClass(),
493                 "Caught NamingException retrieving all group names",
494                 ne);
495         }
496     }
497
498     /* (non-Javadoc)
499      * @see org.sape.carbon.core.component.lifecycle.Configurable#configure(org.sape.carbon.core.component.ComponentConfiguration)
500      */

501     public void configure(ComponentConfiguration configuration)
502         throws Exception JavaDoc {
503
504         // TODO validate config
505
try {
506             this.config = (JNDIUserManagerConfiguration) configuration;
507         } catch (ClassCastException JavaDoc cce) {
508             throw new InvalidConfigurationException(
509                 this.getClass(),
510                 configuration.getConfigurationName(),
511                 "ConfigurationInterface",
512                 "Configuration object was not of type " +
513                     JNDIUserManagerConfiguration.class.getName(),
514                 cce);
515         }
516     }
517
518     /**
519      * @return
520      */

521     protected DirContext JavaDoc getInitialContext() throws NamingException JavaDoc {
522         return (DirContext JavaDoc) this.config.getInitialContextFactory().getContext();
523     }
524     
525     protected DirContext JavaDoc getUserParentContext() throws NamingException JavaDoc {
526         return (DirContext JavaDoc) getInitialContext().lookup(
527             this.config.getUserParentContextName());
528     }
529     
530     protected DirContext JavaDoc getGroupParentContext() throws NamingException JavaDoc {
531         return (DirContext JavaDoc) getInitialContext().lookup(
532             this.config.getGroupParentContextName());
533     }
534     
535     protected String JavaDoc getUserContextName(String JavaDoc userName) throws NamingException JavaDoc {
536         return this.config.getUserNameAttribute() +
537             this.config.getAttributeNameValueSeparator() +
538             userName;
539     }
540
541     protected String JavaDoc getGroupContextName(String JavaDoc groupName) throws NamingException JavaDoc {
542         return this.config.getGroupNameAttribute() +
543             this.config.getAttributeNameValueSeparator() +
544             groupName;
545     }
546     
547     protected Principal JavaDoc constructUser(String JavaDoc userName) {
548         return new DefaultUserImpl(userName);
549     }
550
551     protected Group JavaDoc constructGroup(String JavaDoc groupName) {
552         return new DefaultGroupImpl(groupName, null, this.serviceName);
553     }
554     
555     public void initialize(Component thisComponent) throws Exception JavaDoc {
556         this.serviceName = thisComponent.getComponentName();
557     }
558
559 }
560
Popular Tags