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.spring;
26 import java.util.LinkedHashSet;
29 import javax.security.auth.login.AccountException;
30 import javax.security.auth.login.AccountNotFoundException;
32 import org.collectionspace.authentication.CSpaceTenant;
33 import org.collectionspace.authentication.CSpaceUser;
34 import org.collectionspace.authentication.realm.CSpaceRealm;
35 import org.springframework.security.authentication.AuthenticationServiceException;
36 import org.springframework.security.core.GrantedAuthority;
37 import org.springframework.security.core.authority.SimpleGrantedAuthority;
38 import org.springframework.security.core.userdetails.UserDetails;
39 import org.springframework.security.core.userdetails.UserDetailsService;
40 import org.springframework.security.core.userdetails.UsernameNotFoundException;
43 * A Spring UserDetailsService for CollectionSpace.
45 public class CSpaceUserDetailsService implements UserDetailsService {
46 private CSpaceRealm realm = null;
48 public CSpaceUserDetailsService(CSpaceRealm realm) {
53 public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
54 String password = null;
56 Boolean requireSSO = null;
57 Set<CSpaceTenant> tenants = null;
58 Set<GrantedAuthority> grantedAuthorities = null;
61 password = realm.getPassword(username);
62 salt = realm.getSalt(username);
63 requireSSO = realm.isRequireSSO(username);
64 tenants = getTenants(username);
65 if (tenants == null || tenants.isEmpty()) {
66 String msg = String.format("The account '%s' is not associated with any tenants. " +
67 "Please contact your administrator.", username);
68 throw new AccountException(msg);
70 grantedAuthorities = getAuthorities(username);
72 catch (AccountNotFoundException e) {
73 throw new UsernameNotFoundException(e.getMessage(), e);
75 catch (AccountException e) {
76 throw new AuthenticationServiceException(e.getMessage(), e);
79 CSpaceUser cspaceUser =
91 protected Set<GrantedAuthority> getAuthorities(String username) throws AccountException {
92 Set<String> roles = realm.getRoles(username);
93 Set<GrantedAuthority> authorities = new LinkedHashSet<GrantedAuthority>(roles.size());
95 for (String role : roles) {
96 authorities.add(new SimpleGrantedAuthority(role));
102 protected Set<CSpaceTenant> getTenants(String username) throws AccountException {
103 Set<CSpaceTenant> tenants = realm.getTenants(username);