]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a9587889194a5fc6f292a77cd480ddf003c9c712
[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.Set;
56 import javax.security.auth.Subject;
57 import org.collectionspace.authentication.SecurityContextUtils;
58 import org.collectionspace.authentication.CSpaceTenant;
59 import org.springframework.security.authentication.jaas.JaasAuthenticationToken;
60 import org.springframework.security.core.Authentication;
61 import org.springframework.security.core.context.SecurityContextHolder;
62
63 /**
64  * SpringSecurityContextUtils provides utilities to CSpace services runtime
65  * @author 
66  */
67 final public class SpringSecurityContextUtils extends SecurityContextUtils {
68     //private static final String SUBJECT_CONTEXT_KEY = "javax.security.auth.Subject.container";
69
70     public String getUserId() {
71         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
72         return authToken.getName();
73     }
74
75     /**
76      * retrieve tenant ids from Jaas LoginContext
77      * @return
78      */
79     @Override
80     public String[] getTenantIds() {
81
82         ArrayList<String> tenants = new ArrayList<String>();
83         Subject caller = null;
84         Authentication authToken = SecurityContextHolder.getContext().getAuthentication();
85         JaasAuthenticationToken jaasToken = null;
86         if (authToken instanceof JaasAuthenticationToken) {
87             jaasToken = (JaasAuthenticationToken) authToken;
88             caller = (Subject) jaasToken.getLoginContext().getSubject();
89         }
90         //caller = (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
91         if (caller == null) {
92             String msg = "security not enabled!";
93             //TODO: find out why subject is not null
94             //FIXME: if logger is loaded when authn comes up, use it
95             //logger.warn(msg);
96             System.err.println(msg);
97             return tenants.toArray(new String[0]);
98         }
99         Set<Group> groups = null;
100         groups = caller.getPrincipals(Group.class);
101         if (groups != null && groups.size() == 0) {
102             String msg = "no role(s)/tenant(s) found!";
103             //TODO: find out why no roles / tenants found
104             //FIXME: if logger is loaded when authn comes up, use it
105             //logger.warn(msg);
106             System.err.println(msg);
107             return tenants.toArray(new String[0]);
108         }
109         for (Group g : groups) {
110             if ("Tenants".equals(g.getName())) {
111                 Enumeration members = g.members();
112                 while (members.hasMoreElements()) {
113                     CSpaceTenant tenant = (CSpaceTenant) members.nextElement();
114                     tenants.add(tenant.getId());
115                     //FIXME: if logger is loaded when authn comes up, use it
116 //                    if (logger.isDebugEnabled()) {
117 //                        logger.debug("found tenant id=" + tenant.getId()
118 //                                + " name=" + tenant.getName());
119 //                    }
120                 }
121             }
122         }
123         return tenants.toArray(new String[0]);
124     }
125 }