Skip to content

Commit

Permalink
fix for number of unique char of generated password when valuePolicy …
Browse files Browse the repository at this point in the history
…doesn't contain limits
  • Loading branch information
skublik committed Apr 9, 2020
1 parent a572af4 commit 776c1b4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Expand Up @@ -7,14 +7,7 @@

package com.evolveum.midpoint.model.common.stringpolicy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;

import com.evolveum.midpoint.prism.MutablePrismPropertyDefinition;
Expand Down Expand Up @@ -96,6 +89,7 @@ public class ValuePolicyProcessor {
private static final Trace LOGGER = TraceManager.getTrace(ValuePolicyProcessor.class);

private static final Random RAND = new Random(System.currentTimeMillis());
private static final String ALPHANUMERIC_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

private static final String DOT_CLASS = ValuePolicyProcessor.class.getName() + ".";
private static final String OPERATION_STRING_POLICY_VALIDATION = DOT_CLASS + "stringPolicyValidation";
Expand Down Expand Up @@ -596,6 +590,17 @@ private String generateAttempt(ValuePolicyType policy, int defaultLength, boolea

boolean uniquenessReached = false;

//fake limit with all alphanumeric character, because of number of unique char
if(lims.isEmpty()){
StringLimitType fakeLimit = new StringLimitType();
CharacterClassType charClass = new CharacterClassType();
charClass.setValue(ALPHANUMERIC_CHARS);
fakeLimit.setCharacterClass(charClass);
fakeLimit.setMustBeFirst(false);
fakeLimit.setMaxOccurs(maxLen);
fakeLimit.setMinOccurs(minLen);
lims.put(fakeLimit, StringPolicyUtils.stringTokenizer(ALPHANUMERIC_CHARS));
}
// Count cardinality of elements
Map<Integer, List<String>> chars;
for (int i = 0; i < minLen; i++) {
Expand Down
Expand Up @@ -159,6 +159,27 @@ public void testValueGenerateRandomPin() throws Exception {

}

@Test
public void testValueGenerateMailNonce() throws Exception {
Task task = getTestTask();
OperationResult result = task.getResult();

ValuePolicyType pp = parsePasswordPolicy("value-policy-generate-without-limit-with-unique.xml");

// WHEN
when();
String mailNonce = valuePolicyProcessor.generate(SchemaConstants.PATH_PASSWORD_VALUE, pp, 24, false, null, getTestNameShort(), task, result);

// THEN
then();
displayValue("Generated password", mailNonce);
result.computeStatus();
TestUtil.assertSuccess(result);
assertNotNull(mailNonce);
assertPassword(mailNonce, pp);

}

@Test
public void testValueGenerate() throws Exception {
Task task = getTestTask();
Expand Down

0 comments on commit 776c1b4

Please sign in to comment.