View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  package org.apache.jetspeed.security.impl.ext;
18  
19  import java.security.Principal;
20  import java.security.acl.Group;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.Enumeration;
24  import java.util.List;
25  
26  import javax.security.auth.Subject;
27  
28  import org.apache.jetspeed.security.User;
29  import org.apache.jetspeed.security.UserManager;
30  import org.apache.jetspeed.security.impl.DefaultLoginModule;
31  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
32  
33  /***
34   * <p>Configures Subject principals for JBoss JAAS implementation
35   * @author <a href="mailto:ate@douma.nu">Ate Douma</a>
36   */
37  public class JBossLoginModule extends DefaultLoginModule
38  {
39      private static class JBossGroup implements Group
40      {
41          private String name;
42          private ArrayList members = new ArrayList();
43          
44          public JBossGroup(String name, List members)
45          {
46              this.name = name;
47              this.members.addAll(members);
48          }
49  
50          public boolean addMember(Principal user)
51          {
52              if ( !isMember(user) )
53              {
54                  members.add(user);
55                  return true;
56              }
57              return false;
58          }
59  
60          public boolean isMember(Principal member)
61          {
62              return members.contains(member);
63          }
64  
65          public boolean removeMember(Principal user)
66          {
67              return members.remove(user);
68          }
69  
70          public Enumeration members()
71          {
72              return Collections.enumeration(members);
73          }
74  
75          public String getName()
76          {
77              return name;
78          }        
79      }
80      
81      /***
82       * Create a new JBoss login module
83       */
84      public JBossLoginModule () {
85          super ();
86      }
87  
88      /***
89       * Create a new JBoss login module that uses the given user manager.
90       * @param userManager
91       * @see DefaultLoginModule#DefaultLoginModule(UserManager)
92       */
93      protected JBossLoginModule (UserManager userManager) {
94          super (userManager);
95      }
96      
97      protected void commitPrincipals(Subject subject, User user)
98      {
99          // add UserPrincipal to subject
100         subject.getPrincipals().add(getUserPrincipal(user));
101         JBossGroup roles = new JBossGroup("Roles", getUserRoles(user));
102         roles.addMember(new RolePrincipalImpl(portalUserRole));
103         subject.getPrincipals().add(roles);        
104     }
105 }