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 * 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:
28 * http://www.collectionspace.org
29 * http://wiki.collectionspace.org
31 * Copyright 2009 University of California at Berkeley
33 * Licensed under the Educational Community License (ECL), Version 2.0.
34 * You may not use this file except in compliance with this License.
36 * You may obtain a copy of the ECL 2.0 License at
38 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
47 * To change this template, choose Tools | Templates
48 * and open the template in the editor.
50 package org.collectionspace.authentication.spring;
52 import java.security.acl.Group;
53 import java.util.ArrayList;
54 import java.util.Enumeration;
55 import java.util.List;
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;
65 * SpringAuthNContext provides utilities to CSpace services runtime
68 final public class SpringAuthNContext extends AuthNContext {
69 //private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
71 public String getUserId() {
72 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
73 return authToken.getName();
77 * retrieve tenant ids from Jaas LoginContext
81 public String[] getTenantIds() {
83 ArrayList<String> tenantList = new ArrayList<String>();
84 CSpaceTenant[] tenants = getTenants();
85 for(CSpaceTenant tenant : tenants) {
86 tenantList.add(tenant.getId());
88 return tenantList.toArray(new String[0]);
91 public CSpaceTenant[] getTenants() {
92 List<CSpaceTenant> tenants = new ArrayList<CSpaceTenant>();
93 Subject caller = getSubject();
95 String msg = "Could not find Subject!";
96 //TODO: find out why subject is not null
97 //FIXME: if logger is loaded when authn comes up, use it
99 System.err.println(msg);
100 return tenants.toArray(new CSpaceTenant[0]);
102 Set<Group> groups = null;
103 groups = caller.getPrincipals(Group.class);
104 if (groups != null && groups.size() == 0) {
105 String msg = "no role(s)/tenant(s) found!";
106 //TODO: find out why no roles / tenants found
107 //FIXME: if logger is loaded when authn comes up, use it
109 System.err.println(msg);
110 return tenants.toArray(new CSpaceTenant[0]);
112 for (Group g : groups) {
113 if ("Tenants".equals(g.getName())) {
114 Enumeration members = g.members();
115 while (members.hasMoreElements()) {
116 CSpaceTenant tenant = (CSpaceTenant) members.nextElement();
118 //FIXME: if logger is loaded when authn comes up, use it
119 // if (logger.isDebugEnabled()) {
120 // logger.debug("found tenant id=" + tenant.getId()
121 // + " name=" + tenant.getName());
126 return tenants.toArray(new CSpaceTenant[0]);
129 public Subject getSubject() {
130 Subject caller = null;
131 //if Spring was not used....
132 //caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
134 //FIXME the follow call should be protected with a privileged action
135 //and must only be available to users with super privileges
136 //Spring does not offer any easy mechanism
137 //It is a bad idea to ship with a kernel user...kernel user should be
138 //created at startup time perhaps and used it here
139 Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
140 JaasAuthenticationToken jaasToken = null;
141 if (authToken instanceof JaasAuthenticationToken) {
142 jaasToken = (JaasAuthenticationToken) authToken;
143 caller = (Subject) jaasToken.getLoginContext().getSubject();