]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
29a29f6967badd9823e724a109690c629697db65
[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.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.List;
29 import java.util.Set;
30
31 import java.security.acl.Group;
32 import javax.security.auth.Subject;
33
34 import org.collectionspace.authentication.CSpaceTenant;
35 import org.collectionspace.authentication.spi.AuthNContext;
36
37 import org.springframework.security.authentication.jaas.JaasAuthenticationToken;
38 import org.springframework.security.core.Authentication;
39 import org.springframework.security.core.context.SecurityContextHolder;
40
41 /**
42  * SpringAuthNContext provides utilities to CSpace services runtime
43  * @author 
44  */
45 final public class SpringAuthNContext extends AuthNContext {
46     //private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
47
48     public String getUserId() {
49         String result = ANONYMOUS_USER;
50         
51         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
52         if (authToken != null) {
53                 result = authToken.getName();
54     }
55         
56         return result;
57     }
58
59     /**
60      * retrieve tenant ids from Jaas LoginContext
61      * @return
62      */
63     @Override
64     public String[] getTenantIds() {
65
66         ArrayList<String> tenantList = new ArrayList<String>();
67         CSpaceTenant[] tenants = getTenants();
68         for (CSpaceTenant tenant : tenants) {
69             tenantList.add(tenant.getId());
70         }
71         return tenantList.toArray(new String[0]);
72     }
73
74     @Override
75     public String getCurrentTenantId() {
76         String result = ANONYMOUS_TENANT_ID;
77         
78         String userId = getUserId();
79         if (userId.equals(ANONYMOUS_USER) == false && userId.equals(SPRING_ADMIN_USER) == false) {
80         String[] tenantIds = getTenantIds();
81         if (tenantIds.length < 1) {
82             throw new IllegalStateException("No tenant associated with user=" + getUserId());
83         }
84                 result = getTenantIds()[0];
85     }
86         
87         return result;
88     }
89
90     public CSpaceTenant[] getTenants() {
91         List<CSpaceTenant> tenants = new ArrayList<CSpaceTenant>();
92         Subject caller = getSubject();
93         if (caller == null) {
94                 if (getUserId().equals(SPRING_ADMIN_USER) == false) {
95                     String msg = String.format("Could not find Subject in SpringAuthNContext for user '%s'!", getUserId()); 
96                     System.err.println(msg);                
97                 }
98             return tenants.toArray(new CSpaceTenant[0]);
99         }
100         
101         Set<Group> groups = null;
102         groups = caller.getPrincipals(Group.class);
103         if (groups != null && groups.size() == 0) {
104             String msg = "no role(s)/tenant(s) found!";
105             //TODO: find out why no roles / tenants found
106             //FIXME: if logger is loaded when authn comes up, use it
107             //logger.warn(msg);
108             System.err.println(msg);
109             return tenants.toArray(new CSpaceTenant[0]);
110         }
111         for (Group g : groups) {
112             if ("Tenants".equals(g.getName())) {
113                 Enumeration members = g.members();
114                 while (members.hasMoreElements()) {
115                     CSpaceTenant tenant = (CSpaceTenant) members.nextElement();
116                     tenants.add(tenant);
117                     //FIXME: if logger is loaded when authn comes up, use it
118 //                    if (logger.isDebugEnabled()) {
119 //                        logger.debug("found tenant id=" + tenant.getId()
120 //                                + " name=" + tenant.getName());
121 //                    }
122                 }
123             }
124         }
125         return tenants.toArray(new CSpaceTenant[0]);
126     }
127
128     @Override
129     public String getCurrentTenantName() {
130         String result = ANONYMOUS_TENANT_NAME;
131         
132         if (getUserId().equals(ANONYMOUS_USER) == false) {
133         CSpaceTenant[] tenants = getTenants();
134         if (tenants.length < 1) {
135             throw new IllegalStateException("No tenant associated with user=" + getUserId());
136         }
137                 result = getTenants()[0].getName();
138     }
139         
140         return result;
141     }
142
143     public Subject getSubject() {
144         Subject caller = null;
145         //if Spring was not used....
146         //caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
147
148         //FIXME the follow call should be protected with a privileged action
149         //and must only be available to users with super privileges
150         //Spring does not offer any easy mechanism
151         //It is a bad idea to ship with a kernel user...kernel user should be
152         //created at startup time perhaps and used it here
153         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
154         JaasAuthenticationToken jaasToken = null;
155         if (authToken instanceof JaasAuthenticationToken) {
156             jaasToken = (JaasAuthenticationToken) authToken;
157             caller = (Subject) jaasToken.getLoginContext().getSubject();
158         }
159         return caller;
160     }
161 }