]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d79b54ff810ba1ae67294000b44d8d8899d3013b
[tmp/jakarta-migration.git] /
1 /**\r
2  *  This document is a part of the source code and related artifacts\r
3  *  for CollectionSpace, an open source collections management system\r
4  *  for museums and related institutions:\r
5 \r
6  *  http://www.collectionspace.org\r
7  *  http://wiki.collectionspace.org\r
8 \r
9  *  Copyright 2009 University of California at Berkeley\r
10 \r
11  *  Licensed under the Educational Community License (ECL), Version 2.0.\r
12  *  You may not use this file except in compliance with this License.\r
13 \r
14  *  You may obtain a copy of the ECL 2.0 License at\r
15 \r
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt\r
17 \r
18  *  Unless required by applicable law or agreed to in writing, software\r
19  *  distributed under the License is distributed on an "AS IS" BASIS,\r
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
21  *  See the License for the specific language governing permissions and\r
22  *  limitations under the License.\r
23  */\r
24 package org.collectionspace.authentication;\r
25 \r
26 import java.security.acl.Group;\r
27 import java.util.ArrayList;\r
28 import java.util.Collection;\r
29 import java.util.List;\r
30 \r
31 import java.util.Map;\r
32 import javax.security.auth.Subject;\r
33 import javax.security.auth.callback.CallbackHandler;\r
34 import javax.security.auth.login.LoginException;\r
35 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;\r
36 \r
37 /**\r
38  * CollectionSpace default identity provider supporting multi-tenancy\r
39  * @author\r
40  */\r
41 public class CSpaceDBLoginModule extends UsernamePasswordLoginModule {\r
42 \r
43     private DatabaseRealm realm;\r
44 \r
45     /**\r
46      * Initialize CSpaceDBLoginModule\r
47      *\r
48      * @param options -\r
49      * dsJndiName: The name of the DataSource of the database containing the\r
50      *    Principals, Roles tables\r
51      * principalsQuery: The prepared statement query, equivalent to:\r
52      *    "select Password from Principals where PrincipalID=?"\r
53      * rolesQuery: The prepared statement query, equivalent to:\r
54      *    "select Role, RoleGroup from Roles where PrincipalID=?"\r
55      * tenantsQuery:\r
56      * "select TenantId, TenantName, TenantGroup from Tenants where PrincipalID=?"\r
57      */\r
58     public void initialize(Subject subject, CallbackHandler callbackHandler,\r
59             Map sharedState, Map options) {\r
60         super.initialize(subject, callbackHandler, sharedState, options);\r
61         realm = new DatabaseRealm(options);\r
62     }\r
63     //disabled due to classloading problem\r
64 //    private Logger logger = LoggerFactory.getLogger(CSpaceDBLoginModule.class);\r
65 \r
66     protected String getUsersPassword() throws LoginException {\r
67 \r
68         String username = getUsername();\r
69         String password = null;\r
70         try {\r
71             password = realm.getUsersPassword(username);\r
72             password = convertRawPassword(password);\r
73             if (log.isDebugEnabled()) {\r
74                 log.debug("Obtained user password");\r
75             }\r
76         } catch (LoginException lex) {\r
77             throw lex;\r
78         } catch (Exception ex) {\r
79             LoginException le = new LoginException("Unknown Exception");\r
80             le.initCause(ex);\r
81             throw le;\r
82         }\r
83         return password;\r
84     }\r
85 \r
86     /** Execute the rolesQuery against the dsJndiName to obtain the roles for\r
87     the authenticated user.\r
88 \r
89     @return Group[] containing the sets of roles\r
90      */\r
91     protected Group[] getRoleSets() throws LoginException {\r
92         String username = getUsername();\r
93 \r
94         Collection<Group> roles = realm.getRoles(username,\r
95                 "org.collectionspace.authentication.CSpacePrincipal",\r
96                 "org.jboss.security.SimpleGroup");\r
97 \r
98         Collection<Group> tenants = realm.getTenants(username,\r
99                 "org.collectionspace.authentication.CSpacePrincipal",\r
100                 "org.jboss.security.SimpleGroup");\r
101 \r
102         List<Group> all = new ArrayList<Group>();\r
103         all.addAll(roles);\r
104         all.addAll(tenants);\r
105         Group[] roleSets = new Group[all.size()];\r
106         all.toArray(roleSets);\r
107         return roleSets;\r
108     }\r
109 \r
110     /** A hook to allow subclasses to convert a password from the database\r
111     into a plain text string or whatever form is used for matching against\r
112     the user input. It is called from within the getUsersPassword() method.\r
113     @param rawPassword - the password as obtained from the database\r
114     @return the argument rawPassword\r
115      */\r
116     protected String convertRawPassword(String rawPassword) {\r
117         return rawPassword;\r
118     }\r
119 }\r