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