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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.authentication.spring;
26 import java.security.acl.Group;
27 import java.util.ArrayList;
28 import java.util.Enumeration;
29 import java.util.List;
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;
39 * SpringAuthNContext provides utilities to CSpace services runtime
42 final public class SpringAuthNContext extends AuthNContext {
43 //private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
45 public String getUserId() {
46 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
47 return authToken.getName();
51 * retrieve tenant ids from Jaas LoginContext
55 public String[] getTenantIds() {
57 ArrayList<String> tenantList = new ArrayList<String>();
58 CSpaceTenant[] tenants = getTenants();
59 for (CSpaceTenant tenant : tenants) {
60 tenantList.add(tenant.getId());
62 return tenantList.toArray(new String[0]);
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());
72 return getTenantIds()[0];
75 public CSpaceTenant[] getTenants() {
76 List<CSpaceTenant> tenants = new ArrayList<CSpaceTenant>();
77 Subject caller = getSubject();
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
83 System.err.println(msg);
84 return tenants.toArray(new CSpaceTenant[0]);
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
93 System.err.println(msg);
94 return tenants.toArray(new CSpaceTenant[0]);
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();
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());
110 return tenants.toArray(new CSpaceTenant[0]);
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());
120 return getTenants()[0].getName();
123 public Subject getSubject() {
124 Subject caller = null;
125 //if Spring was not used....
126 //caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
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();