1 package org
.argeo
.security
.jcr
.jackrabbit
;
3 import java
.util
.ArrayList
;
4 import java
.util
.Arrays
;
5 import java
.util
.Iterator
;
6 import java
.util
.LinkedHashSet
;
10 import javax
.jcr
.Node
;
11 import javax
.jcr
.Repository
;
12 import javax
.jcr
.RepositoryException
;
13 import javax
.jcr
.Session
;
14 import javax
.jcr
.SimpleCredentials
;
16 import org
.apache
.jackrabbit
.api
.JackrabbitSession
;
17 import org
.apache
.jackrabbit
.api
.security
.user
.Authorizable
;
18 import org
.apache
.jackrabbit
.api
.security
.user
.Group
;
19 import org
.apache
.jackrabbit
.api
.security
.user
.User
;
20 import org
.apache
.jackrabbit
.api
.security
.user
.UserManager
;
21 import org
.apache
.jackrabbit
.core
.security
.authentication
.CryptedSimpleCredentials
;
22 import org
.argeo
.ArgeoException
;
23 import org
.argeo
.jcr
.JcrUtils
;
24 import org
.argeo
.jcr
.UserJcrUtils
;
25 import org
.argeo
.security
.UserAdminService
;
26 import org
.argeo
.security
.jcr
.JcrSecurityModel
;
27 import org
.argeo
.security
.jcr
.JcrUserDetails
;
28 import org
.springframework
.dao
.DataAccessException
;
29 import org
.springframework
.security
.authentication
.AuthenticationProvider
;
30 import org
.springframework
.security
.authentication
.BadCredentialsException
;
31 import org
.springframework
.security
.authentication
.UsernamePasswordAuthenticationToken
;
32 import org
.springframework
.security
.core
.Authentication
;
33 import org
.springframework
.security
.core
.AuthenticationException
;
34 import org
.springframework
.security
.core
.GrantedAuthority
;
35 import org
.springframework
.security
.core
.authority
.SimpleGrantedAuthority
;
36 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
37 import org
.springframework
.security
.core
.userdetails
.UserDetails
;
38 import org
.springframework
.security
.core
.userdetails
.UsernameNotFoundException
;
41 * An implementation of {@link UserAdminService} which closely wraps Jackrabbits
42 * implementation. Roles are implemented with Groups.
44 public class JackrabbitUserAdminService
implements UserAdminService
,
45 AuthenticationProvider
{
46 final static String userRole
= "ROLE_USER";
47 final static String adminRole
= "ROLE_ADMIN";
49 private Repository repository
;
50 private JcrSecurityModel securityModel
;
52 private JackrabbitSession adminSession
= null;
54 private String superUsername
= "root";
55 private String superUserInitialPassword
= "demo";
57 public void init() throws RepositoryException
{
58 Authentication authentication
= SecurityContextHolder
.getContext()
60 authentication
.getName();
61 adminSession
= (JackrabbitSession
) repository
.login();
62 Authorizable adminGroup
= getUserManager().getAuthorizable(adminRole
);
63 if (adminGroup
== null) {
64 adminGroup
= getUserManager().createGroup(adminRole
);
67 Authorizable superUser
= getUserManager()
68 .getAuthorizable(superUsername
);
69 if (superUser
== null) {
70 superUser
= getUserManager().createUser(superUsername
,
71 superUserInitialPassword
);
72 ((Group
) adminGroup
).addMember(superUser
);
73 securityModel
.sync(adminSession
, superUsername
, null);
78 public void destroy() throws RepositoryException
{
79 JcrUtils
.logoutQuietly(adminSession
);
82 private UserManager
getUserManager() throws RepositoryException
{
83 return adminSession
.getUserManager();
87 public void createUser(UserDetails user
) {
89 // FIXME workaround for issue in new user wizard where
90 // security model is hardcoded and it already exists
91 if (getUserManager().getAuthorizable(user
.getUsername()) == null) {
92 getUserManager().createUser(user
.getUsername(),
94 securityModel
.sync(adminSession
, user
.getUsername(), null);
97 } catch (RepositoryException e
) {
98 throw new ArgeoException("Cannot create user " + user
, e
);
103 public void updateUser(UserDetails userDetails
) {
105 User user
= (User
) getUserManager().getAuthorizable(
106 userDetails
.getUsername());
108 throw new ArgeoException("No user " + userDetails
.getUsername());
111 String newPassword
= userDetails
.getPassword();
112 if (!newPassword
.trim().equals("")) {
113 SimpleCredentials sp
= new SimpleCredentials(
114 userDetails
.getUsername(), newPassword
.toCharArray());
115 CryptedSimpleCredentials credentials
= (CryptedSimpleCredentials
) user
117 if (!credentials
.matches(sp
))
118 user
.changePassword(new String(newPassword
));
121 List
<String
> roles
= new ArrayList
<String
>();
122 for (GrantedAuthority ga
: userDetails
.getAuthorities()) {
123 if (ga
.getAuthority().equals(userRole
))
125 roles
.add(ga
.getAuthority());
128 for (Iterator
<Group
> it
= user
.memberOf(); it
.hasNext();) {
129 Group group
= it
.next();
130 if (roles
.contains(group
.getPrincipal().getName()))
131 roles
.remove(group
.getPrincipal().getName());
133 group
.removeMember(user
);
136 // remaining (new ones)
137 for (String role
: roles
) {
138 Group group
= (Group
) getUserManager().getAuthorizable(role
);
140 throw new ArgeoException("Group " + role
142 + " whereas it was granted to user " + userDetails
);
143 group
.addMember(user
);
145 } catch (Exception e
) {
146 throw new ArgeoException("Cannot update user details", e
);
152 public void deleteUser(String username
) {
154 getUserManager().getAuthorizable(username
).remove();
155 } catch (RepositoryException e
) {
156 throw new ArgeoException("Cannot remove user " + username
, e
);
161 public void changePassword(String oldPassword
, String newPassword
) {
162 Authentication authentication
= SecurityContextHolder
.getContext()
163 .getAuthentication();
164 String username
= authentication
.getName();
166 SimpleCredentials sp
= new SimpleCredentials(username
,
167 oldPassword
.toCharArray());
168 User user
= (User
) getUserManager().getAuthorizable(username
);
169 CryptedSimpleCredentials credentials
= (CryptedSimpleCredentials
) user
171 if (credentials
.matches(sp
))
172 user
.changePassword(newPassword
);
174 throw new BadCredentialsException("Bad credentials provided");
175 } catch (Exception e
) {
176 throw new ArgeoException("Cannot change password for user "
182 public boolean userExists(String username
) {
184 Authorizable authorizable
= getUserManager().getAuthorizable(
186 if (authorizable
!= null && authorizable
instanceof User
)
189 } catch (RepositoryException e
) {
190 throw new ArgeoException("Cannot check whether user " + username
196 public Set
<String
> listUsers() {
197 LinkedHashSet
<String
> res
= new LinkedHashSet
<String
>();
199 Iterator
<Authorizable
> users
= getUserManager().findAuthorizables(
200 "rep:principalName", null, UserManager
.SEARCH_TYPE_USER
);
201 while (users
.hasNext()) {
202 res
.add(users
.next().getPrincipal().getName());
205 } catch (RepositoryException e
) {
206 throw new ArgeoException("Cannot list users", e
);
211 public Set
<String
> listUsersInRole(String role
) {
212 LinkedHashSet
<String
> res
= new LinkedHashSet
<String
>();
214 Group group
= (Group
) getUserManager().getAuthorizable(role
);
215 Iterator
<Authorizable
> users
= group
.getMembers();
217 while (users
.hasNext()) {
218 res
.add(users
.next().getPrincipal().getName());
221 } catch (RepositoryException e
) {
222 throw new ArgeoException("Cannot list users in role " + role
, e
);
227 public void synchronize() {
231 public void newRole(String role
) {
233 getUserManager().createGroup(role
);
234 } catch (RepositoryException e
) {
235 throw new ArgeoException("Cannot create role " + role
, e
);
240 public Set
<String
> listEditableRoles() {
241 LinkedHashSet
<String
> res
= new LinkedHashSet
<String
>();
243 Iterator
<Authorizable
> groups
= getUserManager().findAuthorizables(
244 "rep:principalName", null, UserManager
.SEARCH_TYPE_GROUP
);
245 while (groups
.hasNext()) {
246 res
.add(groups
.next().getPrincipal().getName());
249 } catch (RepositoryException e
) {
250 throw new ArgeoException("Cannot list groups", e
);
255 public void deleteRole(String role
) {
257 getUserManager().getAuthorizable(role
).remove();
258 } catch (RepositoryException e
) {
259 throw new ArgeoException("Cannot remove role " + role
, e
);
264 public UserDetails
loadUserByUsername(String username
)
265 throws UsernameNotFoundException
, DataAccessException
{
267 User user
= (User
) getUserManager().getAuthorizable(username
);
269 throw new UsernameNotFoundException("User " + username
270 + " cannot be found");
271 return loadJcrUserDetails(adminSession
, username
);
272 } catch (RepositoryException e
) {
273 throw new ArgeoException("Cannot load user " + username
, e
);
277 protected JcrUserDetails
loadJcrUserDetails(Session session
, String username
)
278 throws RepositoryException
{
279 if (username
== null)
280 username
= session
.getUserID();
281 User user
= (User
) getUserManager().getAuthorizable(username
);
282 ArrayList
<GrantedAuthority
> authorities
= new ArrayList
<GrantedAuthority
>();
283 // FIXME make it more generic
284 authorities
.add(new SimpleGrantedAuthority("ROLE_USER"));
285 Iterator
<Group
> groups
= user
.declaredMemberOf();
286 while (groups
.hasNext()) {
287 Group group
= groups
.next();
288 // String role = "ROLE_"
289 // + group.getPrincipal().getName().toUpperCase();
290 String role
= group
.getPrincipal().getName();
291 authorities
.add(new SimpleGrantedAuthority(role
));
294 Node userProfile
= UserJcrUtils
.getUserProfile(session
, username
);
295 JcrUserDetails userDetails
= new JcrUserDetails(userProfile
, "",
300 // AUTHENTICATION PROVIDER
301 public synchronized Authentication
authenticate(
302 Authentication authentication
) throws AuthenticationException
{
303 UsernamePasswordAuthenticationToken siteAuth
= (UsernamePasswordAuthenticationToken
) authentication
;
304 String username
= siteAuth
.getName();
305 if (!(siteAuth
.getCredentials() instanceof char[]))
306 throw new ArgeoException("Only char array passwords are supported");
307 char[] password
= (char[]) siteAuth
.getCredentials();
309 SimpleCredentials sp
= new SimpleCredentials(siteAuth
.getName(),
311 User user
= (User
) getUserManager().getAuthorizable(username
);
313 throw new BadCredentialsException("Bad credentials");
314 CryptedSimpleCredentials credentials
= (CryptedSimpleCredentials
) user
316 // String providedPassword = siteAuth.getCredentials().toString();
317 if (!credentials
.matches(sp
))
318 throw new BadCredentialsException("Bad credentials");
320 // session = repository.login(sp, null);
322 Node userProfile
= UserJcrUtils
.getUserProfile(adminSession
,
324 JcrUserDetails
.checkAccountStatus(userProfile
);
325 } catch (BadCredentialsException e
) {
327 } catch (Exception e
) {
328 throw new BadCredentialsException(
329 "Cannot authenticate " + siteAuth
, e
);
331 Arrays
.fill(password
, '*');
335 JcrUserDetails userDetails
= loadJcrUserDetails(adminSession
,
337 UsernamePasswordAuthenticationToken authenticated
= new UsernamePasswordAuthenticationToken(
338 siteAuth
, "", userDetails
.getAuthorities());
339 authenticated
.setDetails(userDetails
);
340 return authenticated
;
341 } catch (RepositoryException e
) {
342 throw new ArgeoException(
343 "Unexpected exception when authenticating " + siteAuth
, e
);
347 @SuppressWarnings("rawtypes")
348 public boolean supports(Class authentication
) {
349 return UsernamePasswordAuthenticationToken
.class
350 .isAssignableFrom(authentication
);
353 public void setRepository(Repository repository
) {
354 this.repository
= repository
;
357 public void setSecurityModel(JcrSecurityModel securityModel
) {
358 this.securityModel
= securityModel
;