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
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright 2009 University of California at Berkeley
\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
14 * You may obtain a copy of the ECL 2.0 License at
\r
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
\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
24 package org.collectionspace.authentication.jaas;
\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
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
36 import org.collectionspace.authentication.realm.db.CSpaceDbRealm;
\r
37 import org.jboss.security.auth.spi.UsernamePasswordLoginModule;
\r
39 import org.slf4j.Logger;
\r
40 import org.slf4j.LoggerFactory;
\r
43 * CollectionSpace default identity provider supporting multi-tenancy
\r
46 public class CSpaceJBossDBLoginModule extends UsernamePasswordLoginModule {
\r
48 private Logger logger = LoggerFactory.getLogger(CSpaceJBossDBLoginModule.class);
\r
50 private CSpaceDbRealm realm;
\r
53 * Initialize CSpaceDBLoginModule
\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
63 * "select TenantId, TenantName, TenantGroup from Tenants where PrincipalID=?"
\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
71 protected String getUsersPassword() throws LoginException {
\r
73 String username = getUsername();
\r
74 String password = null;
\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
81 } catch (LoginException lex) {
\r
83 } catch (Exception ex) {
\r
84 LoginException le = new LoginException("Unknown Exception");
\r
92 public boolean commit() throws LoginException {
\r
94 result = super.commit();
\r
99 public boolean abort() throws LoginException {
\r
101 result = super.abort();
\r
105 /** Execute the rolesQuery against the dsJndiName to obtain the roles for
\r
106 the authenticated user.
\r
108 @return Group[] containing the sets of roles
\r
110 protected Group[] getRoleSets() throws LoginException {
\r
111 String username = getUsername();
\r
113 Collection<Group> roles = realm.getRoles(username,
\r
114 "org.collectionspace.authentication.CSpacePrincipal",
\r
115 "org.jboss.security.SimpleGroup");
\r
117 Collection<Group> tenants = realm.getTenants(username,
\r
118 "org.jboss.security.SimpleGroup");
\r
120 List<Group> all = new ArrayList<Group>();
\r
122 all.addAll(tenants);
\r
123 Group[] roleSets = new Group[all.size()];
\r
124 all.toArray(roleSets);
\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
134 protected String convertRawPassword(String rawPassword) {
\r
135 return rawPassword;
\r