From: Mathieu Baudier Date: Sat, 17 Jan 2015 14:33:19 +0000 (+0000) Subject: Fix user management with Jackrabbit user service X-Git-Tag: argeo-commons-2.1.30~443 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=38607e1d34188a4dc1b47bff08940381b703135e;p=lgpl%2Fargeo-commons.git Fix user management with Jackrabbit user service git-svn-id: https://svn.argeo.org/commons/trunk@7681 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/org.argeo.security.jackrabbit/src/org/argeo/security/jackrabbit/JackrabbitUserAdminService.java b/org.argeo.security.jackrabbit/src/org/argeo/security/jackrabbit/JackrabbitUserAdminService.java index 97cb538b4..d5ef2ff17 100644 --- a/org.argeo.security.jackrabbit/src/org/argeo/security/jackrabbit/JackrabbitUserAdminService.java +++ b/org.argeo.security.jackrabbit/src/org/argeo/security/jackrabbit/JackrabbitUserAdminService.java @@ -103,15 +103,19 @@ public class JackrabbitUserAdminService implements UserAdminService, try { User user = (User) getUserManager().getAuthorizable( userDetails.getUsername()); + if (user == null) + throw new ArgeoException("No user " + userDetails.getUsername()); // new password - char[] newPassword = userDetails.getPassword().toCharArray(); - SimpleCredentials sp = new SimpleCredentials( - userDetails.getUsername(), newPassword); - CryptedSimpleCredentials credentials = (CryptedSimpleCredentials) user - .getCredentials(); - if (!credentials.matches(sp)) - user.changePassword(new String(newPassword)); + String newPassword = userDetails.getPassword(); + if (!newPassword.trim().equals("")) { + SimpleCredentials sp = new SimpleCredentials( + userDetails.getUsername(), newPassword.toCharArray()); + CryptedSimpleCredentials credentials = (CryptedSimpleCredentials) user + .getCredentials(); + if (!credentials.matches(sp)) + user.changePassword(new String(newPassword)); + } List roles = new ArrayList(); for (GrantedAuthority ga : userDetails.getAuthorities()) { @@ -265,15 +269,14 @@ public class JackrabbitUserAdminService implements UserAdminService, if (user == null) throw new UsernameNotFoundException("User " + username + " cannot be found"); - return loadJcrUserDetails(adminSession, username, - user.getCredentials()); + return loadJcrUserDetails(adminSession, username); } catch (RepositoryException e) { throw new ArgeoException("Cannot load user " + username, e); } } - protected JcrUserDetails loadJcrUserDetails(Session session, - String username, Object credentials) throws RepositoryException { + protected JcrUserDetails loadJcrUserDetails(Session session, String username) + throws RepositoryException { if (username == null) username = session.getUserID(); User user = (User) getUserManager().getAuthorizable(username); @@ -290,8 +293,8 @@ public class JackrabbitUserAdminService implements UserAdminService, } Node userProfile = UserJcrUtils.getUserProfile(session, username); - JcrUserDetails userDetails = new JcrUserDetails(userProfile, - credentials.toString(), authorities); + JcrUserDetails userDetails = new JcrUserDetails(userProfile, "", + authorities); return userDetails; } @@ -304,17 +307,21 @@ public class JackrabbitUserAdminService implements UserAdminService, SimpleCredentials sp = new SimpleCredentials(siteAuth.getName(), siteAuth.getCredentials().toString().toCharArray()); User user = (User) getUserManager().getAuthorizable(username); + if (user == null) + throw new BadCredentialsException("Bad credentials"); CryptedSimpleCredentials credentials = (CryptedSimpleCredentials) user .getCredentials(); // String providedPassword = siteAuth.getCredentials().toString(); - if (!credentials.matches(sp)) { - throw new BadCredentialsException("Passwords do not match"); - } + if (!credentials.matches(sp)) + throw new BadCredentialsException("Bad credentials"); + // session = repository.login(sp, null); Node userProfile = UserJcrUtils.getUserProfile(adminSession, username); JcrUserDetails.checkAccountStatus(userProfile); + } catch (BadCredentialsException e) { + throw e; } catch (Exception e) { throw new BadCredentialsException( "Cannot authenticate " + siteAuth, e); @@ -322,7 +329,7 @@ public class JackrabbitUserAdminService implements UserAdminService, try { JcrUserDetails userDetails = loadJcrUserDetails(adminSession, - username, siteAuth.getCredentials()); + username); UsernamePasswordAuthenticationToken authenticated = new UsernamePasswordAuthenticationToken( siteAuth, "", userDetails.getAuthorities()); authenticated.setDetails(userDetails);