use of org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf 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);
}
use of org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf in project syncope by apache.
the class MappingManagerImpl method prepareAttr.
/**
* Prepare an attribute to be sent to a connector instance.
*
* @param provision external resource
* @param mapItem mapping item for the given attribute
* @param any given any object
* @param password clear-text password
* @return connObjectKey + prepared attribute
*/
private Pair<String, Attribute> prepareAttr(final Provision provision, final Item mapItem, final Any<?> any, final String password) {
IntAttrName intAttrName;
try {
intAttrName = intAttrNameParser.parse(mapItem.getIntAttrName(), provision.getAnyType().getKind());
} catch (ParseException e) {
LOG.error("Invalid intAttrName '{}' specified, ignoring", mapItem.getIntAttrName(), e);
return null;
}
boolean readOnlyVirSchema = false;
Schema schema = null;
AttrSchemaType schemaType = AttrSchemaType.String;
if (intAttrName.getSchemaType() != null) {
switch(intAttrName.getSchemaType()) {
case PLAIN:
schema = plainSchemaDAO.find(intAttrName.getSchemaName());
if (schema != null) {
schemaType = schema.getType();
}
break;
case VIRTUAL:
schema = virSchemaDAO.find(intAttrName.getSchemaName());
readOnlyVirSchema = (schema != null && schema.isReadonly());
break;
default:
}
}
List<PlainAttrValue> values = getIntValues(provision, mapItem, intAttrName, any);
LOG.debug("Define mapping for: " + "\n* ExtAttrName " + mapItem.getExtAttrName() + "\n* is connObjectKey " + mapItem.isConnObjectKey() + "\n* is password " + mapItem.isPassword() + "\n* mandatory condition " + mapItem.getMandatoryCondition() + "\n* Schema " + intAttrName.getSchemaName() + "\n* ClassType " + schemaType.getType().getName() + "\n* Values " + values);
Pair<String, Attribute> result;
if (readOnlyVirSchema) {
result = null;
} else {
List<Object> objValues = new ArrayList<>();
for (PlainAttrValue value : values) {
if (FrameworkUtil.isSupportedAttributeType(schemaType.getType())) {
objValues.add(value.getValue());
} else {
objValues.add(value.getValueAsString(schemaType));
}
}
if (mapItem.isConnObjectKey()) {
result = Pair.of(objValues.isEmpty() ? null : objValues.iterator().next().toString(), null);
} else if (mapItem.isPassword() && any instanceof User) {
String passwordAttrValue = password;
if (StringUtils.isBlank(passwordAttrValue)) {
User user = (User) any;
if (user.canDecodePassword()) {
try {
passwordAttrValue = ENCRYPTOR.decode(user.getPassword(), user.getCipherAlgorithm());
} catch (Exception e) {
LOG.error("Could not decode password for {}", user, e);
}
} else if (provision.getResource().isRandomPwdIfNotProvided()) {
try {
passwordAttrValue = passwordGenerator.generate(provision.getResource());
} catch (InvalidPasswordRuleConf e) {
LOG.error("Could not generate policy-compliant random password for {}", user, e);
}
}
}
if (passwordAttrValue == null) {
result = null;
} else {
result = Pair.of(null, AttributeBuilder.buildPassword(passwordAttrValue.toCharArray()));
}
} else if (schema != null && schema.isMultivalue()) {
result = Pair.of(null, AttributeBuilder.build(mapItem.getExtAttrName(), objValues));
} else {
result = Pair.of(null, objValues.isEmpty() ? AttributeBuilder.build(mapItem.getExtAttrName()) : AttributeBuilder.build(mapItem.getExtAttrName(), objValues.iterator().next()));
}
}
return result;
}
use of org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf in project syncope by apache.
the class UserTest method testPasswordGenerator.
@Test
public void testPasswordGenerator() {
String password = "";
try {
password = passwordGenerator.generate(resourceDAO.find("ws-target-resource-nopropagation"));
} catch (InvalidPasswordRuleConf e) {
fail(e.getMessage());
}
assertNotNull(password);
User user = userDAO.find("c9b2dec2-00a7-4855-97c0-d854842b4b24");
user.setPassword(password, CipherAlgorithm.SHA);
userDAO.save(user);
}
use of org.apache.syncope.core.provisioning.api.utils.policy.InvalidPasswordRuleConf 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.provisioning.api.utils.policy.InvalidPasswordRuleConf 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);
}
Aggregations