]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
754c588f8842d0f6bd1a8b632acdc6c223a4c74a
[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         Boolean requireSSO = null;
79         Set<CSpaceTenant> tenants = null;
80         Set<GrantedAuthority> grantedAuthorities = null;
81         
82         try {
83             password = realm.getPassword(username);
84             salt = realm.getSalt(username);
85             requireSSO = realm.isRequireSSO(username);
86             tenants = getTenants(username);
87             grantedAuthorities = getAuthorities(username);
88         }
89         catch (AccountNotFoundException e) {
90             throw new UsernameNotFoundException(e.getMessage(), e);
91         }
92         catch (AccountException e) {
93             throw new AuthenticationServiceException(e.getMessage(), e);
94         }
95         
96         CSpaceUser cspaceUser = 
97             new CSpaceUser(
98                 username,
99                 password,
100                 salt,
101                 requireSSO,
102                 tenants,
103                 grantedAuthorities);
104                 
105         return cspaceUser;
106     }
107     
108     protected Set<GrantedAuthority> getAuthorities(String username) throws AccountException {
109         Set<String> roles = realm.getRoles(username);
110         Set<GrantedAuthority> authorities = new LinkedHashSet<GrantedAuthority>(roles.size());
111         
112         for (String role : roles) {
113             authorities.add(new SimpleGrantedAuthority(role));
114         }
115         
116         return authorities;
117     }
118     
119     protected Set<CSpaceTenant> getTenants(String username) throws AccountException {
120         Set<CSpaceTenant> tenants = realm.getTenants(username);
121         
122         return tenants;
123     }
124 }