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