01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: private static class JBossGroup implements Group {
39: private String name;
40: private ArrayList members = new ArrayList();
41:
42: public JBossGroup(String name, List members) {
43: this .name = name;
44: this .members.addAll(members);
45: }
46:
47: public boolean addMember(Principal user) {
48: if (!isMember(user)) {
49: members.add(user);
50: return true;
51: }
52: return false;
53: }
54:
55: public boolean isMember(Principal member) {
56: return members.contains(member);
57: }
58:
59: public boolean removeMember(Principal user) {
60: return members.remove(user);
61: }
62:
63: public Enumeration members() {
64: return Collections.enumeration(members);
65: }
66:
67: public String getName() {
68: return name;
69: }
70: }
71:
72: /**
73: * Create a new JBoss login module
74: */
75: public JBossLoginModule() {
76: super ();
77: }
78:
79: /**
80: * Create a new JBoss login module that uses the given user manager.
81: * @param userManager
82: * @see DefaultLoginModule#DefaultLoginModule(UserManager)
83: */
84: protected JBossLoginModule(UserManager userManager) {
85: super (userManager);
86: }
87:
88: protected void commitPrincipals(Subject subject, User user) {
89: // add UserPrincipal to subject
90: subject.getPrincipals().add(getUserPrincipal(user));
91: JBossGroup roles = new JBossGroup("Roles", getUserRoles(user));
92: roles.addMember(new RolePrincipalImpl(portalUserRole));
93: subject.getPrincipals().add(roles);
94: }
95: }
|