Skip to content

Commit 071b0cf

Browse files
committed
#532 Only change password if it has actually changed
Closes #532
1 parent 7e6f450 commit 071b0cf

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

accesscontroltool-bundle/src/main/java/biz/netcentric/cq/tools/actool/authorizableinstaller/impl/AuthorizableInstallerServiceImpl.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.jcr.Node;
2626
import javax.jcr.RepositoryException;
2727
import javax.jcr.Session;
28+
import javax.jcr.SimpleCredentials;
2829
import javax.jcr.UnsupportedRepositoryOperationException;
2930
import javax.jcr.ValueFactory;
3031

@@ -43,6 +44,7 @@
4344
import org.apache.sling.api.resource.Resource;
4445
import org.apache.sling.api.resource.ResourceResolver;
4546
import org.apache.sling.api.resource.ResourceResolverFactory;
47+
import org.apache.sling.jcr.api.SlingRepository;
4648
import org.apache.sling.jcr.resource.JcrResourceConstants;
4749
import org.osgi.service.component.annotations.Reference;
4850
import org.osgi.service.component.annotations.ReferenceCardinality;
@@ -99,6 +101,9 @@ public class AuthorizableInstallerServiceImpl implements
99101
@Reference(policyOption = ReferencePolicyOption.GREEDY)
100102
ResourceResolverFactory resourceResolverFactory;
101103

104+
@Reference(policyOption = ReferencePolicyOption.GREEDY)
105+
SlingRepository repository;
106+
102107
@Override
103108
public void installAuthorizables(
104109
AcConfiguration acConfiguration,
@@ -155,7 +160,7 @@ private void installAuthorizableConfigurationBean(final Session session,
155160
// update password for users
156161
if (!authorizableToInstall.isGroup() && !authorizableConfigBean.isSystemUser()
157162
&& StringUtils.isNotBlank(authorizableConfigBean.getPassword())) {
158-
setUserPassword(authorizableConfigBean, (User) authorizableToInstall);
163+
setUserPassword(authorizableConfigBean, (User) authorizableToInstall, installLog);
159164
}
160165

161166
// move authorizable if path changed (retaining existing members)
@@ -226,11 +231,25 @@ private void installKeys(Map<String, Key> keys, String userId, ResourceResolver
226231
}
227232
}
228233

229-
230234
void setUserPassword(final AuthorizableConfigBean authorizableConfigBean,
231-
final User authorizableToInstall) throws RepositoryException, AuthorizableCreatorException {
235+
final User authorizableToInstall, InstallationLogger installLog) throws RepositoryException, AuthorizableCreatorException {
236+
237+
String userId = authorizableToInstall.getID();
232238
String password = getPassword(authorizableConfigBean);
233-
authorizableToInstall.changePassword(password);
239+
Session sessionForUser = null;
240+
try {
241+
sessionForUser = repository.login(new SimpleCredentials(userId, password.toCharArray()));
242+
LOG.trace("Could obtain session {} for user {}, will not update password", sessionForUser, userId);
243+
installLog.addVerboseMessage(LOG, "Password of user " + userId + " has not changed");
244+
} catch (javax.jcr.LoginException e) {
245+
LOG.trace("User {} could not log in with existing password", userId, e);
246+
authorizableToInstall.changePassword(password);
247+
installLog.addMessage(LOG, "Changed password of user " + userId);
248+
} finally {
249+
if (sessionForUser != null) {
250+
sessionForUser.logout();
251+
}
252+
}
234253
}
235254

236255
private String getPassword(final AuthorizableConfigBean authorizableConfigBean)

accesscontroltool-bundle/src/test/java/biz/netcentric/cq/tools/actool/authorizableinstaller/impl/AuthorizableInstallerServiceImplTest.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@
1414
import static org.mockito.Matchers.eq;
1515
import static org.mockito.Mockito.doAnswer;
1616
import static org.mockito.Mockito.doReturn;
17+
import static org.mockito.Mockito.doThrow;
1718
import static org.mockito.Mockito.mock;
1819
import static org.mockito.Mockito.reset;
1920
import static org.mockito.Mockito.times;
2021
import static org.mockito.Mockito.verify;
2122
import static org.mockito.Mockito.verifyNoMoreInteractions;
2223
import static org.mockito.Mockito.verifyZeroInteractions;
24+
import static org.mockito.MockitoAnnotations.initMocks;
2325

2426
import java.util.Arrays;
25-
import java.util.Collection;
2627
import java.util.HashSet;
2728
import java.util.Set;
2829

2930
import javax.jcr.RepositoryException;
3031
import javax.jcr.Session;
32+
import javax.jcr.SimpleCredentials;
3133
import javax.jcr.Value;
3234
import javax.jcr.ValueFactory;
3335

@@ -36,17 +38,17 @@
3638
import org.apache.jackrabbit.api.security.user.Group;
3739
import org.apache.jackrabbit.api.security.user.User;
3840
import org.apache.jackrabbit.api.security.user.UserManager;
41+
import org.apache.sling.jcr.api.SlingRepository;
3942
import org.hamcrest.BaseMatcher;
4043
import org.hamcrest.Description;
4144
import org.junit.Before;
4245
import org.junit.Test;
4346
import org.junit.runner.RunWith;
4447
import org.junit.runners.Enclosed;
45-
import org.junit.runners.Parameterized;
48+
import org.mockito.InjectMocks;
4649
import org.mockito.Matchers;
4750
import org.mockito.Mock;
4851
import org.mockito.Mockito;
49-
import org.mockito.MockitoAnnotations;
5052
import org.mockito.Spy;
5153
import org.mockito.invocation.InvocationOnMock;
5254
import org.mockito.runners.MockitoJUnitRunner;
@@ -59,6 +61,7 @@
5961
import biz.netcentric.cq.tools.actool.configmodel.AuthorizableConfigBean;
6062
import biz.netcentric.cq.tools.actool.configmodel.GlobalConfiguration;
6163
import biz.netcentric.cq.tools.actool.crypto.DecryptionService;
64+
import biz.netcentric.cq.tools.actool.history.InstallationLogger;
6265
import biz.netcentric.cq.tools.actool.history.PersistableInstallationLogger;
6366

6467
@RunWith(Enclosed.class)
@@ -300,6 +303,7 @@ public void describeTo(Description desc) {
300303

301304
public static final class SetUserPassword {
302305

306+
private static final String USER_ID = "userid";
303307
private static final String UNPROTECTED_PASSWORD = "unprotected_pass";
304308

305309
@Mock
@@ -308,26 +312,48 @@ public static final class SetUserPassword {
308312
@Mock
309313
private DecryptionService decryptionService;
310314

315+
@Mock
316+
private InstallationLogger installationLogger;
317+
318+
@Mock
319+
private SlingRepository repository;
320+
321+
@Mock
322+
private Session session;
323+
324+
@Spy
325+
@InjectMocks
311326
private AuthorizableInstallerServiceImpl service;
312327

328+
private AuthorizableConfigBean configBean;
329+
313330
@Before
314-
public void setUp() throws CryptoException {
315-
MockitoAnnotations.initMocks(this);
331+
public void setUp() throws CryptoException, RepositoryException {
332+
initMocks(this);
316333

317-
service = new AuthorizableInstallerServiceImpl();
318-
service.decryptionService = decryptionService;
334+
doReturn(USER_ID).when(user).getID();
319335

320336
doReturn(UNPROTECTED_PASSWORD).when(decryptionService).decrypt(anyString());
337+
338+
configBean = new AuthorizableConfigBean();
339+
configBean.setPassword("{some_protected_pass1}");
321340
}
322341

323342
@Test
324-
public void test() throws RepositoryException, AuthorizableCreatorException {
325-
final AuthorizableConfigBean bean = new AuthorizableConfigBean();
326-
bean.setPassword("{some_protected_pass1}");
343+
public void testPasswordExists() throws RepositoryException, AuthorizableCreatorException {
327344

328-
service.setUserPassword(bean, user);
345+
doReturn(session).when(repository).login(any(SimpleCredentials.class));
346+
service.setUserPassword(configBean, user, installationLogger);
347+
verify(user, times(0)).changePassword(anyString());
348+
}
329349

330-
verify(user).changePassword(eq(UNPROTECTED_PASSWORD));
350+
@Test
351+
public void testPasswordDifferent() throws RepositoryException, AuthorizableCreatorException {
352+
final AuthorizableConfigBean bean = new AuthorizableConfigBean();
353+
bean.setPassword("{some_protected_pass1}");
354+
doThrow(javax.jcr.LoginException.class).when(repository).login(any(SimpleCredentials.class));
355+
service.setUserPassword(configBean, user, installationLogger);
356+
verify(user, times(1)).changePassword(UNPROTECTED_PASSWORD);
331357
}
332358
}
333359
}

0 commit comments

Comments
 (0)