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.util.ArrayList;
27 import java.util.Enumeration;
28 import java.util.List;
31 import java.security.acl.Group;
32 import javax.security.auth.Subject;
34 import org.collectionspace.authentication.CSpaceTenant;
35 import org.collectionspace.authentication.spi.AuthNContext;
37 import org.springframework.security.authentication.jaas.JaasAuthenticationToken;
38 import org.springframework.security.core.Authentication;
39 import org.springframework.security.core.context.SecurityContextHolder;
42 * SpringAuthNContext provides utilities to CSpace services runtime
45 final public class SpringAuthNContext extends AuthNContext {
46 //private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
48 public String getUserId() {
49 String result = ANONYMOUS_USER;
51 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
52 if (authToken != null) {
53 result = authToken.getName();
60 * retrieve tenant ids from Jaas LoginContext
64 public String[] getTenantIds() {
66 ArrayList<String> tenantList = new ArrayList<String>();
67 CSpaceTenant[] tenants = getTenants();
68 for (CSpaceTenant tenant : tenants) {
69 tenantList.add(tenant.getId());
71 return tenantList.toArray(new String[0]);
75 public String getCurrentTenantId() {
76 String result = ANONYMOUS_TENANT_ID;
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());
84 result = getTenantIds()[0];
90 public CSpaceTenant[] getTenants() {
91 List<CSpaceTenant> tenants = new ArrayList<CSpaceTenant>();
92 Subject caller = getSubject();
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);
98 return tenants.toArray(new CSpaceTenant[0]);
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
108 System.err.println(msg);
109 return tenants.toArray(new CSpaceTenant[0]);
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();
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());
125 return tenants.toArray(new CSpaceTenant[0]);
129 public String getCurrentTenantName() {
130 String result = ANONYMOUS_TENANT_NAME;
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());
137 result = getTenants()[0].getName();
143 public Subject getSubject() {
144 Subject caller = null;
145 //if Spring was not used....
146 //caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
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();