]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
80aee4ef94cf4cd32bb7254120068ee36de7ed4a
[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.jaas;\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.collectionspace.authentication.realm.CSpaceDbRealm;\r
36 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;\r
37 \r
38 /**\r
39  * CollectionSpace default identity provider supporting multi-tenancy\r
40  * @author\r
41  */\r
42 public class CSpaceJBossDBLoginModule extends UsernamePasswordLoginModule {\r
43 \r
44     private CSpaceDbRealm realm;\r
45 \r
46     /**\r
47      * Initialize CSpaceDBLoginModule\r
48      *\r
49      * @param options -\r
50      * dsJndiName: The name of the DataSource of the database containing the\r
51      *    Principals, Roles tables\r
52      * principalsQuery: The prepared statement query, equivalent to:\r
53      *    "select Password from Principals where PrincipalID=?"\r
54      * rolesQuery: The prepared statement query, equivalent to:\r
55      *    "select Role, RoleGroup from Roles where PrincipalID=?"\r
56      * tenantsQuery:\r
57      * "select TenantId, TenantName, TenantGroup from Tenants where PrincipalID=?"\r
58      */\r
59     public void initialize(Subject subject, CallbackHandler callbackHandler,\r
60             Map sharedState, Map options) {\r
61         super.initialize(subject, callbackHandler, sharedState, options);\r
62         realm = new CSpaceDbRealm(options);\r
63     }\r
64     //disabled due to classloading problem\r
65 //    private Logger logger = LoggerFactory.getLogger(CSpaceDBLoginModule.class);\r
66 \r
67     protected String getUsersPassword() throws LoginException {\r
68 \r
69         String username = getUsername();\r
70         String password = null;\r
71         try {\r
72             password = realm.getUsersPassword(username);\r
73             password = convertRawPassword(password);\r
74             if (log.isDebugEnabled()) {\r
75                 log.debug("Obtained user password");\r
76             }\r
77         } catch (LoginException lex) {\r
78             throw lex;\r
79         } catch (Exception ex) {\r
80             LoginException le = new LoginException("Unknown Exception");\r
81             le.initCause(ex);\r
82             throw le;\r
83         }\r
84         return password;\r
85     }\r
86 \r
87     /** Execute the rolesQuery against the dsJndiName to obtain the roles for\r
88     the authenticated user.\r
89 \r
90     @return Group[] containing the sets of roles\r
91      */\r
92     protected Group[] getRoleSets() throws LoginException {\r
93         String username = getUsername();\r
94 \r
95         Collection<Group> roles = realm.getRoles(username,\r
96                 "org.collectionspace.authentication.CSpacePrincipal",\r
97                 "org.jboss.security.SimpleGroup");\r
98 \r
99         Collection<Group> tenants = realm.getTenants(username,\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