]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4a2dec8b3d25eaf2ef5ad462bef6c8a3591343a8
[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.util.ArrayList;\r
27 import java.util.Collection;\r
28 import java.util.List;\r
29 import java.util.Map;\r
30 \r
31 import java.security.acl.Group;\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 \r
36 import org.collectionspace.authentication.realm.db.CSpaceDbRealm;\r
37 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;\r
38 \r
39 import org.slf4j.Logger;\r
40 import org.slf4j.LoggerFactory;\r
41 \r
42 /**\r
43  * CollectionSpace default identity provider supporting multi-tenancy\r
44  * @author\r
45  */\r
46 public class CSpaceJBossDBLoginModule extends UsernamePasswordLoginModule {\r
47 \r
48     private Logger logger = LoggerFactory.getLogger(CSpaceJBossDBLoginModule.class);\r
49 \r
50     private CSpaceDbRealm realm;\r
51 \r
52     /**\r
53      * Initialize CSpaceDBLoginModule\r
54      *\r
55      * @param options -\r
56      * dsJndiName: The name of the DataSource of the database containing the\r
57      *    Principals, Roles tables\r
58      * principalsQuery: The prepared statement query, equivalent to:\r
59      *    "select Password from Principals where PrincipalID=?"\r
60      * rolesQuery: The prepared statement query, equivalent to:\r
61      *    "select Role, RoleGroup from Roles where PrincipalID=?"\r
62      * tenantsQuery:\r
63      * "select TenantId, TenantName, TenantGroup from Tenants where PrincipalID=?"\r
64      */\r
65     public void initialize(Subject subject, CallbackHandler callbackHandler,\r
66             Map sharedState, Map options) {\r
67         super.initialize(subject, callbackHandler, sharedState, options);\r
68         realm = new CSpaceDbRealm(options);\r
69     }\r
70 \r
71     protected String getUsersPassword() throws LoginException {\r
72 \r
73         String username = getUsername();\r
74         String password = null;\r
75         try {\r
76             password = realm.getUsersPassword(username);\r
77             password = convertRawPassword(password);\r
78             if (logger.isDebugEnabled()) {\r
79                 logger.debug("Obtained user password for: " + username);\r
80             }\r
81         } catch (LoginException lex) {\r
82             throw lex;\r
83         } catch (Exception ex) {\r
84             LoginException le = new LoginException("Unknown Exception");\r
85             le.initCause(ex);\r
86             throw le;\r
87         }\r
88         return password;\r
89     }\r
90     \r
91     @Override\r
92     public boolean commit() throws LoginException {\r
93         boolean result;\r
94         result = super.commit();\r
95         return result;\r
96     }\r
97     \r
98     @Override\r
99     public boolean abort() throws LoginException {\r
100         boolean result;\r
101         result = super.abort();\r
102         return result;\r
103     }\r
104 \r
105     /** Execute the rolesQuery against the dsJndiName to obtain the roles for\r
106     the authenticated user.\r
107 \r
108     @return Group[] containing the sets of roles\r
109      */\r
110     protected Group[] getRoleSets() throws LoginException {\r
111         String username = getUsername();\r
112 \r
113         Collection<Group> roles = realm.getRoles(username,\r
114                 "org.collectionspace.authentication.CSpacePrincipal",\r
115                 "org.jboss.security.SimpleGroup");\r
116 \r
117         Collection<Group> tenants = realm.getTenants(username,\r
118                 "org.jboss.security.SimpleGroup");\r
119 \r
120         List<Group> all = new ArrayList<Group>();\r
121         all.addAll(roles);\r
122         all.addAll(tenants);\r
123         Group[] roleSets = new Group[all.size()];\r
124         all.toArray(roleSets);\r
125         return roleSets;\r
126     }\r
127 \r
128     /** A hook to allow subclasses to convert a password from the database\r
129     into a plain text string or whatever form is used for matching against\r
130     the user input. It is called from within the getUsersPassword() method.\r
131     @param rawPassword - the password as obtained from the database\r
132     @return the argument rawPassword\r
133      */\r
134     protected String convertRawPassword(String rawPassword) {\r
135         return rawPassword;\r
136     }\r
137 }\r