]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
27801a2b41f4882ded1549618334b6ca9c5d1a8f
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.authentication.spring;
25
26 import java.security.acl.Group;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
30 import java.util.Set;
31 import javax.security.auth.Subject;
32 import org.collectionspace.authentication.CSpaceTenant;
33 import org.collectionspace.authentication.spi.AuthNContext;
34 import org.springframework.security.authentication.jaas.JaasAuthenticationToken;
35 import org.springframework.security.core.Authentication;
36 import org.springframework.security.core.context.SecurityContextHolder;
37
38 /**
39  * SpringAuthNContext provides utilities to CSpace services runtime
40  * @author 
41  */
42 final public class SpringAuthNContext extends AuthNContext {
43     //private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
44
45     public String getUserId() {
46         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
47         return authToken.getName();
48     }
49
50     /**
51      * retrieve tenant ids from Jaas LoginContext
52      * @return
53      */
54     @Override
55     public String[] getTenantIds() {
56
57         ArrayList<String> tenantList = new ArrayList<String>();
58         CSpaceTenant[] tenants = getTenants();
59         for (CSpaceTenant tenant : tenants) {
60             tenantList.add(tenant.getId());
61         }
62         return tenantList.toArray(new String[0]);
63     }
64
65     @Override
66     public String getCurrentTenantId() {
67         //FIXME assumption in 1.0: each user is associated with a single tenant
68         String[] tenantIds = getTenantIds();
69         if (tenantIds.length < 1) {
70             throw new IllegalStateException("No tenant associated with user=" + getUserId());
71         }
72         return getTenantIds()[0];
73     }
74
75     public CSpaceTenant[] getTenants() {
76         List<CSpaceTenant> tenants = new ArrayList<CSpaceTenant>();
77         Subject caller = getSubject();
78         if (caller == null) {
79             String msg = "Could not find Subject!";
80             //TODO: find out why subject is not null
81             //FIXME: if logger is loaded when authn comes up, use it
82             //logger.warn(msg);
83             System.err.println(msg);
84             return tenants.toArray(new CSpaceTenant[0]);
85         }
86         Set<Group> groups = null;
87         groups = caller.getPrincipals(Group.class);
88         if (groups != null && groups.size() == 0) {
89             String msg = "no role(s)/tenant(s) found!";
90             //TODO: find out why no roles / tenants found
91             //FIXME: if logger is loaded when authn comes up, use it
92             //logger.warn(msg);
93             System.err.println(msg);
94             return tenants.toArray(new CSpaceTenant[0]);
95         }
96         for (Group g : groups) {
97             if ("Tenants".equals(g.getName())) {
98                 Enumeration members = g.members();
99                 while (members.hasMoreElements()) {
100                     CSpaceTenant tenant = (CSpaceTenant) members.nextElement();
101                     tenants.add(tenant);
102                     //FIXME: if logger is loaded when authn comes up, use it
103 //                    if (logger.isDebugEnabled()) {
104 //                        logger.debug("found tenant id=" + tenant.getId()
105 //                                + " name=" + tenant.getName());
106 //                    }
107                 }
108             }
109         }
110         return tenants.toArray(new CSpaceTenant[0]);
111     }
112
113     @Override
114     public String getCurrentTenantName() {
115         //FIXME assumption in 1.0: each user is associated with a single tenant
116         CSpaceTenant[] tenants = getTenants();
117         if (tenants.length < 1) {
118             throw new IllegalStateException("No tenant associated with user=" + getUserId());
119         }
120         return getTenants()[0].getName();
121     }
122
123     public Subject getSubject() {
124         Subject caller = null;
125         //if Spring was not used....
126         //caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
127
128         //FIXME the follow call should be protected with a privileged action
129         //and must only be available to users with super privileges
130         //Spring does not offer any easy mechanism
131         //It is a bad idea to ship with a kernel user...kernel user should be
132         //created at startup time perhaps and used it here
133         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
134         JaasAuthenticationToken jaasToken = null;
135         if (authToken instanceof JaasAuthenticationToken) {
136             jaasToken = (JaasAuthenticationToken) authToken;
137             caller = (Subject) jaasToken.getLoginContext().getSubject();
138         }
139         return caller;
140     }
141 }