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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.authentication.jaas;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
30 import java.security.acl.Group;
32 import javax.security.auth.Subject;
33 import javax.security.auth.callback.CallbackHandler;
34 import javax.security.auth.login.LoginException;
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;
42 * CollectionSpace default identity provider supporting multi-tenancy
45 public class CSpaceJBossDBLoginModule extends UsernamePasswordLoginModule {
47 private Logger logger = LoggerFactory.getLogger(CSpaceJBossDBLoginModule.class);
49 private CSpaceDbRealm realm;
52 * Initialize CSpaceDBLoginModule
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=?"
62 * "select TenantId, TenantName, TenantGroup from Tenants where PrincipalID=?"
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);
71 protected String createPasswordHash(String username, String password,
73 throws LoginException {
74 String result = super.createPasswordHash(username, password, digestOption);
77 String message = "Could not create a password hash for the supplied password. Check your login.conf configuration's hash algorithm setting.";
79 throw new LoginException(message);
85 protected String getUsersPassword() throws LoginException {
87 String username = getUsername();
88 String password = null;
91 password = realm.getUsersPassword(username);
92 password = convertRawPassword(password);
93 if (logger.isDebugEnabled()) {
94 logger.debug("Obtained user password for: " + username);
96 } catch (LoginException lex) {
97 log.error("Could not retrieve user password for: " + username, lex);
99 } catch (Exception ex) {
100 log.error("Could not retrieve user password for: " + username, ex);
101 LoginException le = new LoginException("Unknown Exception");
110 public boolean commit() throws LoginException {
112 result = super.commit();
117 public boolean abort() throws LoginException {
119 result = super.abort();
123 /** Execute the rolesQuery against the dsJndiName to obtain the roles for
124 the authenticated user.
126 @return Group[] containing the sets of roles
128 protected Group[] getRoleSets() throws LoginException {
129 String username = getUsername();
131 Collection<Group> roles = realm.getRoles(username,
132 "org.collectionspace.authentication.CSpacePrincipal",
133 "org.jboss.security.SimpleGroup");
135 Collection<Group> tenants = realm.getTenants(username,
136 "org.jboss.security.SimpleGroup");
138 List<Group> all = new ArrayList<Group>();
141 Group[] roleSets = new Group[all.size()];
142 all.toArray(roleSets);
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
152 protected String convertRawPassword(String rawPassword) {