use of org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy in project syncope by apache.
the class ConnObjectUtils method getAnyTO.
/**
* Build a UserTO / GroupTO / AnyObjectTO out of connector object attributes and schema mapping.
*
* @param obj connector object
* @param pullTask pull task
* @param provision provision information
* @param anyUtils utils
* @param <T> any object
* @return UserTO for the user to be created
*/
@Transactional(readOnly = true)
public <T extends AnyTO> T getAnyTO(final ConnectorObject obj, final PullTask pullTask, final Provision provision, final AnyUtils anyUtils) {
T anyTO = getAnyTOFromConnObject(obj, pullTask, provision, anyUtils);
// (for users) if password was not set above, generate if resource is configured for that
if (anyTO instanceof UserTO && StringUtils.isBlank(((UserTO) anyTO).getPassword()) && provision.getResource().isRandomPwdIfNotProvided()) {
UserTO userTO = (UserTO) anyTO;
List<PasswordPolicy> passwordPolicies = new ArrayList<>();
Realm realm = realmDAO.findByFullPath(userTO.getRealm());
if (realm != null) {
realmDAO.findAncestors(realm).stream().filter(ancestor -> ancestor.getPasswordPolicy() != null).forEach(ancestor -> {
passwordPolicies.add(ancestor.getPasswordPolicy());
});
}
userTO.getResources().stream().map(resource -> resourceDAO.find(resource)).filter(resource -> resource != null && resource.getPasswordPolicy() != null).forEach(resource -> {
passwordPolicies.add(resource.getPasswordPolicy());
});
String password;
try {
password = passwordGenerator.generate(passwordPolicies);
} catch (InvalidPasswordRuleConf e) {
LOG.error("Could not generate policy-compliant random password for {}", userTO, e);
password = SecureRandomUtils.generateRandomPassword(16);
}
userTO.setPassword(password);
}
return anyTO;
}
use of org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy in project syncope by apache.
the class DefaultPasswordGenerator method generate.
@Override
public String generate(final List<PasswordPolicy> policies) throws InvalidPasswordRuleConf {
List<DefaultPasswordRuleConf> defaultRuleConfs = new ArrayList<>();
policies.stream().forEach(policy -> policy.getRules().forEach(impl -> {
try {
Optional<PasswordRule> rule = ImplementationManager.buildPasswordRule(impl);
if (rule.isPresent() && rule.get().getConf() instanceof DefaultPasswordRuleConf) {
defaultRuleConfs.add((DefaultPasswordRuleConf) rule.get().getConf());
}
} catch (Exception e) {
LOG.error("Invalid {}, ignoring...", impl, e);
}
}));
DefaultPasswordRuleConf ruleConf = merge(defaultRuleConfs);
check(ruleConf);
return generate(ruleConf);
}
use of org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy in project syncope by apache.
the class PasswordGeneratorTest method startEndWithDigit.
@Test
public void startEndWithDigit() 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.setMustEndWithDigit(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.isDigit(generatedPassword.charAt(generatedPassword.length() - 1)));
}
use of org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy in project syncope by apache.
the class PasswordGeneratorTest method passwordWithNonAlpha.
@Test
public void passwordWithNonAlpha() throws InvalidPasswordRuleConf {
DefaultPasswordRuleConf pwdRuleConf1 = createBaseDefaultPasswordRuleConf();
pwdRuleConf1.setNonAlphanumericRequired(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(PolicyPattern.NON_ALPHANUMERIC.matcher(generatedPassword).matches());
assertTrue(Character.isLetter(generatedPassword.charAt(generatedPassword.length() - 1)));
}
use of org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy in project syncope by apache.
the class PasswordGeneratorTest method incopatiblePolicies.
@Test
public void incopatiblePolicies() {
assertThrows(InvalidPasswordRuleConf.class, () -> {
DefaultPasswordRuleConf pwdRuleConf1 = createBaseDefaultPasswordRuleConf();
pwdRuleConf1.setMinLength(12);
TestImplementation passwordRule1 = new TestImplementation();
passwordRule1.setBody(POJOHelper.serialize(pwdRuleConf1));
TestPasswordPolicy policy1 = new TestPasswordPolicy();
policy1.add(passwordRule1);
DefaultPasswordRuleConf pwdRuleConf2 = createBaseDefaultPasswordRuleConf();
pwdRuleConf2.setMaxLength(10);
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);
passwordGenerator.generate(policies);
});
}
Aggregations