Search in sources :

Example 1 with DefaultPasswordRuleConf

use of org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf in project syncope by apache.

the class DefaultPasswordGenerator method merge.

private DefaultPasswordRuleConf merge(final List<DefaultPasswordRuleConf> defaultRuleConfs) {
    DefaultPasswordRuleConf result = new DefaultPasswordRuleConf();
    result.setMinLength(VERY_MIN_LENGTH);
    result.setMaxLength(VERY_MAX_LENGTH);
    defaultRuleConfs.forEach(ruleConf -> {
        if (ruleConf.getMinLength() > result.getMinLength()) {
            result.setMinLength(ruleConf.getMinLength());
        }
        if ((ruleConf.getMaxLength() != 0) && ((ruleConf.getMaxLength() < result.getMaxLength()))) {
            result.setMaxLength(ruleConf.getMaxLength());
        }
        result.getPrefixesNotPermitted().addAll(ruleConf.getPrefixesNotPermitted());
        result.getSuffixesNotPermitted().addAll(ruleConf.getSuffixesNotPermitted());
        if (!result.isNonAlphanumericRequired()) {
            result.setNonAlphanumericRequired(ruleConf.isNonAlphanumericRequired());
        }
        if (!result.isAlphanumericRequired()) {
            result.setAlphanumericRequired(ruleConf.isAlphanumericRequired());
        }
        if (!result.isDigitRequired()) {
            result.setDigitRequired(ruleConf.isDigitRequired());
        }
        if (!result.isLowercaseRequired()) {
            result.setLowercaseRequired(ruleConf.isLowercaseRequired());
        }
        if (!result.isUppercaseRequired()) {
            result.setUppercaseRequired(ruleConf.isUppercaseRequired());
        }
        if (!result.isMustStartWithDigit()) {
            result.setMustStartWithDigit(ruleConf.isMustStartWithDigit());
        }
        if (!result.isMustntStartWithDigit()) {
            result.setMustntStartWithDigit(ruleConf.isMustntStartWithDigit());
        }
        if (!result.isMustEndWithDigit()) {
            result.setMustEndWithDigit(ruleConf.isMustEndWithDigit());
        }
        if (result.isMustntEndWithDigit()) {
            result.setMustntEndWithDigit(ruleConf.isMustntEndWithDigit());
        }
        if (!result.isMustStartWithAlpha()) {
            result.setMustStartWithAlpha(ruleConf.isMustStartWithAlpha());
        }
        if (!result.isMustntStartWithAlpha()) {
            result.setMustntStartWithAlpha(ruleConf.isMustntStartWithAlpha());
        }
        if (!result.isMustStartWithNonAlpha()) {
            result.setMustStartWithNonAlpha(ruleConf.isMustStartWithNonAlpha());
        }
        if (!result.isMustntStartWithNonAlpha()) {
            result.setMustntStartWithNonAlpha(ruleConf.isMustntStartWithNonAlpha());
        }
        if (!result.isMustEndWithNonAlpha()) {
            result.setMustEndWithNonAlpha(ruleConf.isMustEndWithNonAlpha());
        }
        if (!result.isMustntEndWithNonAlpha()) {
            result.setMustntEndWithNonAlpha(ruleConf.isMustntEndWithNonAlpha());
        }
        if (!result.isMustEndWithAlpha()) {
            result.setMustEndWithAlpha(ruleConf.isMustEndWithAlpha());
        }
        if (!result.isMustntEndWithAlpha()) {
            result.setMustntEndWithAlpha(ruleConf.isMustntEndWithAlpha());
        }
        if (!result.isUsernameAllowed()) {
            result.setUsernameAllowed(ruleConf.isUsernameAllowed());
        }
    });
    if (result.getMinLength() == 0) {
        result.setMinLength(result.getMaxLength() < MIN_LENGTH_IF_ZERO ? result.getMaxLength() : MIN_LENGTH_IF_ZERO);
    }
    return result;
}
Also used : DefaultPasswordRuleConf(org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf)

Example 2 with DefaultPasswordRuleConf

use of org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf in project syncope by apache.

the class PasswordGeneratorTest method issueSYNCOPE678.

@Test
public void issueSYNCOPE678() {
    String password = null;
    try {
        password = passwordGenerator.generate(Collections.<PasswordPolicy>emptyList());
    } catch (InvalidPasswordRuleConf e) {
        fail(e.getMessage());
    }
    assertNotNull(password);
    DefaultPasswordRuleConf pwdRuleConf1 = createBaseDefaultPasswordRuleConf();
    pwdRuleConf1.setMinLength(0);
    TestImplementation passwordRule1 = new TestImplementation();
    passwordRule1.setBody(POJOHelper.serialize(pwdRuleConf1));
    TestPasswordPolicy policy1 = new TestPasswordPolicy();
    password = null;
    try {
        password = passwordGenerator.generate(Collections.<PasswordPolicy>singletonList(policy1));
    } catch (InvalidPasswordRuleConf e) {
        fail(e.getMessage());
    }
    assertNotNull(password);
}
Also used : DefaultPasswordRuleConf(org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf) InvalidPasswordRuleConf(org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) Test(org.junit.jupiter.api.Test)

Example 3 with DefaultPasswordRuleConf

use of org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf in project syncope by apache.

the class PasswordGeneratorTest method startWithDigitAndWithAlpha.

@Test
public void startWithDigitAndWithAlpha() throws InvalidPasswordRuleConf {
    DefaultPasswordRuleConf pwdRuleConf1 = createBaseDefaultPasswordRuleConf();
    pwdRuleConf1.setMustStartWithDigit(true);
    TestImplementation passwordRule1 = new TestImplementation();
    passwordRule1.setBody(POJOHelper.serialize(pwdRuleConf1));
    TestPasswordPolicy policy1 = new TestPasswordPolicy();
    policy1.add(passwordRule1);
    DefaultPasswordRuleConf pwdRuleConf2 = createBaseDefaultPasswordRuleConf();
    pwdRuleConf2.setMustEndWithAlpha(true);
    TestImplementation passwordRule2 = new TestImplementation();
    passwordRule2.setBody(POJOHelper.serialize(pwdRuleConf2));
    TestPasswordPolicy policy2 = new TestPasswordPolicy();
    policy2.add(passwordRule2);
    List<PasswordPolicy> policies = new ArrayList<>();
    policies.add(policy1);
    policies.add(policy2);
    String generatedPassword = passwordGenerator.generate(policies);
    assertTrue(Character.isDigit(generatedPassword.charAt(0)));
    assertTrue(Character.isLetter(generatedPassword.charAt(generatedPassword.length() - 1)));
}
Also used : DefaultPasswordRuleConf(org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 4 with DefaultPasswordRuleConf

use of org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf in project syncope by apache.

the class UserIssuesITCase method issueSYNCOPE626.

@Test
public void issueSYNCOPE626() {
    DefaultPasswordRuleConf ruleConf = new DefaultPasswordRuleConf();
    ruleConf.setUsernameAllowed(false);
    ImplementationTO rule = new ImplementationTO();
    rule.setKey("DefaultPasswordRuleConf" + getUUIDString());
    rule.setEngine(ImplementationEngine.JAVA);
    rule.setType(ImplementationType.PASSWORD_RULE);
    rule.setBody(POJOHelper.serialize(ruleConf));
    Response response = implementationService.create(rule);
    rule.setKey(response.getHeaderString(RESTHeaders.RESOURCE_KEY));
    PasswordPolicyTO passwordPolicy = new PasswordPolicyTO();
    passwordPolicy.setDescription("Password Policy for SYNCOPE-626");
    passwordPolicy.getRules().add(rule.getKey());
    passwordPolicy = createPolicy(PolicyType.PASSWORD, passwordPolicy);
    assertNotNull(passwordPolicy);
    RealmTO realm = realmService.list("/even/two").get(0);
    String oldPasswordPolicy = realm.getPasswordPolicy();
    realm.setPasswordPolicy(passwordPolicy.getKey());
    realmService.update(realm);
    try {
        UserTO user = UserITCase.getUniqueSampleTO("syncope626@syncope.apache.org");
        user.setRealm(realm.getFullPath());
        user.setPassword(user.getUsername());
        try {
            createUser(user);
            fail("This should not happen");
        } catch (SyncopeClientException e) {
            assertEquals(ClientExceptionType.InvalidUser, e.getType());
            assertTrue(e.getElements().iterator().next().startsWith("InvalidPassword"));
        }
        user.setPassword("password123");
        user = createUser(user).getEntity();
        assertNotNull(user);
    } finally {
        realm.setPasswordPolicy(oldPasswordPolicy);
        realmService.update(realm);
        policyService.delete(PolicyType.PASSWORD, passwordPolicy.getKey());
    }
}
Also used : ImplementationTO(org.apache.syncope.common.lib.to.ImplementationTO) Response(javax.ws.rs.core.Response) DefaultPasswordRuleConf(org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf) UserTO(org.apache.syncope.common.lib.to.UserTO) RealmTO(org.apache.syncope.common.lib.to.RealmTO) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) PasswordPolicyTO(org.apache.syncope.common.lib.policy.PasswordPolicyTO) Test(org.junit.jupiter.api.Test)

Example 5 with DefaultPasswordRuleConf

use of org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf in project syncope by apache.

the class PolicyITCase method update.

@Test
public void update() {
    PasswordPolicyTO globalPolicy = policyService.read(PolicyType.PASSWORD, "ce93fcda-dc3a-4369-a7b0-a6108c261c85");
    PasswordPolicyTO policy = SerializationUtils.clone(globalPolicy);
    policy.setDescription("A simple password policy");
    // create a new password policy using the former as a template
    policy = createPolicy(PolicyType.PASSWORD, policy);
    assertNotNull(policy);
    assertNotEquals("ce93fcda-dc3a-4369-a7b0-a6108c261c85", policy.getKey());
    ImplementationTO rule = implementationService.read(ImplementationType.PASSWORD_RULE, policy.getRules().get(0));
    assertNotNull(rule);
    DefaultPasswordRuleConf ruleConf = POJOHelper.deserialize(rule.getBody(), DefaultPasswordRuleConf.class);
    ruleConf.setMaxLength(22);
    rule.setBody(POJOHelper.serialize(ruleConf));
    // update new password policy
    policyService.update(PolicyType.PASSWORD, policy);
    policy = policyService.read(PolicyType.PASSWORD, policy.getKey());
    assertNotNull(policy);
    ruleConf = POJOHelper.deserialize(rule.getBody(), DefaultPasswordRuleConf.class);
    assertEquals(22, ruleConf.getMaxLength());
    assertEquals(8, ruleConf.getMinLength());
}
Also used : ImplementationTO(org.apache.syncope.common.lib.to.ImplementationTO) DefaultPasswordRuleConf(org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf) PasswordPolicyTO(org.apache.syncope.common.lib.policy.PasswordPolicyTO) Test(org.junit.jupiter.api.Test)

Aggregations

DefaultPasswordRuleConf (org.apache.syncope.common.lib.policy.DefaultPasswordRuleConf)11 Test (org.junit.jupiter.api.Test)8 PasswordPolicy (org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy)7 ArrayList (java.util.ArrayList)5 PasswordPolicyTO (org.apache.syncope.common.lib.policy.PasswordPolicyTO)2 ImplementationTO (org.apache.syncope.common.lib.to.ImplementationTO)2 InvalidPasswordRuleConf (org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf)2 List (java.util.List)1 Optional (java.util.Optional)1 Response (javax.ws.rs.core.Response)1 StringUtils (org.apache.commons.lang3.StringUtils)1 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)1 RealmTO (org.apache.syncope.common.lib.to.RealmTO)1 UserTO (org.apache.syncope.common.lib.to.UserTO)1 PasswordRule (org.apache.syncope.core.persistence.api.dao.PasswordRule)1 Implementation (org.apache.syncope.core.persistence.api.entity.Implementation)1 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)1 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)1 PolicyPattern (org.apache.syncope.core.provisioning.api.utils.policy.PolicyPattern)1 ImplementationManager (org.apache.syncope.core.spring.ImplementationManager)1