Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,7 @@ public void addToPasswordHistory(final String password) {
@Override
public void removeOldestEntriesFromPasswordHistory(final int n) {
List<String> ph = getPasswordHistory();
ph.subList(n, ph.size());
passwordHistory = POJOHelper.serialize(ph);
passwordHistory = POJOHelper.serialize(new ArrayList<>(ph.subList(Math.min(n, ph.size()), ph.size())));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@
*/
package org.apache.syncope.core.persistence.jpa.inner;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.OffsetDateTime;
import java.util.List;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import org.apache.syncope.common.lib.types.CipherAlgorithm;
import org.apache.syncope.core.persistence.api.EncryptorManager;
import org.apache.syncope.core.persistence.api.dao.DerSchemaDAO;
Expand Down Expand Up @@ -253,4 +259,41 @@ public void issueSYNCOPE1666() {
assertTrue(encryptorManager.getInstance().
verify(securityAnswer, CipherAlgorithm.SSHA256, actual.getSecurityAnswer()));
}

@Test
public void issueSYNCOPE1937()
throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException,
InvalidKeyException {
User user = entityFactory.newEntity(User.class);
user.setUsername("username");
user.setRealm(realmDAO.getRoot());
user.setCreator("admin");
user.setCreationDate(OffsetDateTime.now());

user.setCipherAlgorithm(CipherAlgorithm.SHA1);
user.setPassword("password123");

User actual = userDAO.save(user);

assertEquals(0, user.getPasswordHistory().size());

// add some other password to history
user.addToPasswordHistory(encryptorManager.getInstance().encode("Password123!", CipherAlgorithm.SHA1));
user.addToPasswordHistory(encryptorManager.getInstance().encode("Password124!", CipherAlgorithm.SHA1));
user.addToPasswordHistory(encryptorManager.getInstance().encode("Password125!", CipherAlgorithm.SHA1));
user.addToPasswordHistory(encryptorManager.getInstance().encode("Password126!", CipherAlgorithm.SHA1));
user.addToPasswordHistory(encryptorManager.getInstance().encode("Password127!", CipherAlgorithm.SHA1));

assertEquals(5, user.getPasswordHistory().size());

// keep only the last three passwords into history
user.removeOldestEntriesFromPasswordHistory(2);

assertEquals(3, user.getPasswordHistory().size());

// try with an exceeding number
assertDoesNotThrow(() -> user.removeOldestEntriesFromPasswordHistory(user.getPasswordHistory().size() + 5));

assertNotNull(actual);
}
}
Loading