]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a0974dc7719bce8b54607ee9a2ccd6177ed8e93d
[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.spring;
25
26 import java.util.LinkedHashSet;
27 import java.util.Set;
28
29 import javax.security.auth.login.AccountException;
30 import javax.security.auth.login.AccountNotFoundException;
31
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;
41
42 /**
43  * A Spring UserDetailsService for CollectionSpace.
44  */
45 public class CSpaceUserDetailsService implements UserDetailsService {
46     private CSpaceRealm realm = null;
47
48     public CSpaceUserDetailsService(CSpaceRealm realm) {
49         this.realm = realm;
50     }
51
52     @Override
53     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
54         String password = null;
55         String salt = null;
56         Boolean requireSSO = null;
57         Set<CSpaceTenant> tenants = null;
58         Set<GrantedAuthority> grantedAuthorities = null;
59         
60         try {
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);
69             }
70             grantedAuthorities = getAuthorities(username);
71         }
72         catch (AccountNotFoundException e) {
73             throw new UsernameNotFoundException(e.getMessage(), e);
74         }
75         catch (AccountException e) {
76             throw new AuthenticationServiceException(e.getMessage(), e);
77         }
78         
79         CSpaceUser cspaceUser = 
80             new CSpaceUser(
81                 username,
82                 password,
83                 salt,
84                 requireSSO,
85                 tenants,
86                 grantedAuthorities);
87                 
88         return cspaceUser;
89     }
90     
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());
94         
95         for (String role : roles) {
96             authorities.add(new SimpleGrantedAuthority(role));
97         }
98         
99         return authorities;
100     }
101     
102     protected Set<CSpaceTenant> getTenants(String username) throws AccountException {
103         Set<CSpaceTenant> tenants = realm.getTenants(username);
104         
105         return tenants;
106     }
107 }