]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
74901a35e3429aae87f267f326ebac100b147d12
[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  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 package org.collectionspace.authentication.spring;
47
48 import java.util.LinkedHashSet;
49 import java.util.Set;
50
51 import javax.security.auth.login.AccountException;
52 import javax.security.auth.login.AccountNotFoundException;
53
54 import org.collectionspace.authentication.CSpaceTenant;
55 import org.collectionspace.authentication.CSpaceUser;
56 import org.collectionspace.authentication.realm.CSpaceRealm;
57 import org.springframework.security.authentication.AuthenticationServiceException;
58 import org.springframework.security.core.GrantedAuthority;
59 import org.springframework.security.core.authority.SimpleGrantedAuthority;
60 import org.springframework.security.core.userdetails.UserDetails;
61 import org.springframework.security.core.userdetails.UserDetailsService;
62 import org.springframework.security.core.userdetails.UsernameNotFoundException;
63
64 /**
65  * A Spring UserDetailsService for CollectionSpace.
66  */
67 public class CSpaceUserDetailsService implements UserDetailsService {
68     private CSpaceRealm realm = null;
69
70     public CSpaceUserDetailsService(CSpaceRealm realm) {
71         this.realm = realm;
72     }
73
74     @Override
75     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
76         String password = null;
77         String salt = null;
78         Set<CSpaceTenant> tenants = null;
79         Set<GrantedAuthority> grantedAuthorities = null;
80
81         try {
82             password = realm.getPassword(username);
83             salt = realm.getSalt(username);
84             tenants = getTenants(username);
85             grantedAuthorities = getAuthorities(username);
86         }
87         catch (AccountNotFoundException e) {
88             throw new UsernameNotFoundException(e.getMessage(), e);
89         }
90         catch (AccountException e) {
91             throw new AuthenticationServiceException(e.getMessage(), e);
92         }
93
94         CSpaceUser cspaceUser =
95             new CSpaceUser(
96                 username,
97                 password,
98                 salt,
99                 tenants,
100                 grantedAuthorities);
101
102         return cspaceUser;
103     }
104
105     protected Set<GrantedAuthority> getAuthorities(String username) throws AccountException {
106         Set<String> roles = realm.getRoles(username);
107         Set<GrantedAuthority> authorities = new LinkedHashSet<GrantedAuthority>(roles.size());
108
109         for (String role : roles) {
110             authorities.add(new SimpleGrantedAuthority(role));
111         }
112
113         return authorities;
114     }
115
116     protected Set<CSpaceTenant> getTenants(String username) throws AccountException {
117         Set<CSpaceTenant> tenants = realm.getTenants(username);
118
119         return tenants;
120     }
121 }