{
//TODO: support for empty password should be configurable
passwordString = credential.getValue().toString();
if (passwordString.length() == 0 && !typeConfig.isAllowEmptyPassword())
{
new IdentityException("Empty password is not allowed by configuration");;
}
}
else
{
if (!typeConfig.isAllowEmptyPassword())
{
new IdentityException("Null password value");
}
passwordString = "";
}
if (typeConfig.getEnclosePasswordWith() != null)
{
String enc = typeConfig.getEnclosePasswordWith();
passwordString = enc + passwordString + enc;
}
byte[] encodedPassword = null;
if (typeConfig.getPasswordEncoding() != null)
{
try
{
encodedPassword = passwordString.getBytes(typeConfig.getPasswordEncoding());
}
catch (UnsupportedEncodingException e)
{
throw new IdentityException("Error while encoding password with configured setting: " + typeConfig.getPasswordEncoding(),
e);
}
}
String attributeName = getTypeConfiguration(ctx, ldapIO.getIdentityType()).getPasswordAttributeName();
if (attributeName == null)
{
throw new IdentityException("IdentityType doesn't have passwordAttributeName option set: "
+ ldapIO.getIdentityType().getName());
}
LdapContext ldapContext = getLDAPContext(ctx);
try
{
//TODO: maybe perform a schema check if this attribute is allowed for such entry
Attributes attrs = new BasicAttributes(true);
Attribute attr = new BasicAttribute(attributeName);
if (encodedPassword != null)
{
attr.add(encodedPassword);
}
else
{
attr.add(passwordString);
}
attrs.put(attr);
if(typeConfig.getUpdatePasswordAttributeValues().size() > 0)
{
Map<String, String[]> attributesToAdd = typeConfig.getUpdatePasswordAttributeValues();
for (Map.Entry<String, String[]> entry : attributesToAdd.entrySet())
{
Attribute additionalAttr = new BasicAttribute(entry.getKey());
for (String val : entry.getValue())
{
additionalAttr.add(val);
}
attrs.put(additionalAttr);
}
}
ldapContext.modifyAttributes(ldapIO.getDn(), DirContext.REPLACE_ATTRIBUTE, attrs);
}
catch (NamingException e)
{
throw new IdentityException("Cannot set identity password value.", e);
}
finally
{
try
{
ldapContext.close();
}
catch (NamingException e)
{
throw new IdentityException("Failed to close LDAP connection", e);
}
}
}
else
{
throw new IdentityException("CredentialType not supported for a given IdentityObjectType");
}
}