use of org.ldaptive.ModifyRequest in project cas by apereo.
the class LdapTestUtils method modifyLdapEntry.
/**
* Modify ldap entry.
*
* @param serverCon the server con
* @param dn the dn
* @param attr the attr
* @param add the add
*/
public static void modifyLdapEntry(final LDAPConnection serverCon, final String dn, final LdapAttribute attr, final AttributeModificationType add) {
try {
final String address = "ldap://" + serverCon.getConnectedAddress() + ':' + serverCon.getConnectedPort();
try (Connection conn = DefaultConnectionFactory.getConnection(address)) {
try {
conn.open();
final ModifyOperation modify = new ModifyOperation(conn);
modify.execute(new ModifyRequest(dn, new AttributeModification(add, attr)));
} catch (final Exception e) {
LOGGER.debug(e.getMessage(), e);
}
}
} finally {
serverCon.close();
}
}
use of org.ldaptive.ModifyRequest in project cas by apereo.
the class LdapUtils method executePasswordModifyOperation.
/**
* Execute a password modify operation.
*
* @param currentDn the current dn
* @param connectionFactory the connection factory
* @param oldPassword the old password
* @param newPassword the new password
* @param type the type
* @return true /false
* <p>
* AD NOTE: Resetting passwords requires binding to AD as user with privileges to reset other users passwords
* and it does not validate old password or respect directory policies such as history or minimum password age.
* Changing a password with the old password does respect directory policies and requires no account operator
* privileges on the bind user. Pass in blank old password if reset is in order (e.g. forgot password) vs.
* letting user change their own (e.g. expiring) password.
*/
public static boolean executePasswordModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final String oldPassword, final String newPassword, final AbstractLdapProperties.LdapType type) {
try {
val connConfig = connectionFactory.getConnectionConfig();
val secureLdap = connConfig.getLdapUrl() != null && !connConfig.getLdapUrl().toLowerCase().contains("ldaps://");
if (connConfig.getUseStartTLS() || secureLdap) {
LOGGER.warn("Executing password modification op under a non-secure LDAP connection; " + "To modify password attributes, the connection to the LDAP server {} be secured and/or encrypted.", type == AbstractLdapProperties.LdapType.AD ? "MUST" : "SHOULD");
}
if (type == AbstractLdapProperties.LdapType.AD) {
LOGGER.debug("Executing password change op for active directory based on " + "[https://support.microsoft.com/en-us/kb/269190]" + "change type: [{}]", StringUtils.isBlank(oldPassword) ? "reset" : "change");
val operation = new ModifyOperation(connectionFactory);
val response = StringUtils.isBlank(oldPassword) ? operation.execute(new ModifyRequest(currentDn, new AttributeModification(AttributeModification.Type.REPLACE, new UnicodePwdAttribute(newPassword)))) : operation.execute(new ModifyRequest(currentDn, new AttributeModification(AttributeModification.Type.DELETE, new UnicodePwdAttribute(oldPassword)), new AttributeModification(AttributeModification.Type.ADD, new UnicodePwdAttribute(newPassword))));
LOGGER.debug("Result code [{}], message: [{}]", response.getResultCode(), response.getDiagnosticMessage());
return response.getResultCode() == ResultCode.SUCCESS;
}
LOGGER.debug("Executing password modification op for generic LDAP");
val operation = new ExtendedOperation(connectionFactory);
val response = operation.execute(new PasswordModifyRequest(currentDn, StringUtils.isNotBlank(oldPassword) ? oldPassword : null, newPassword));
LOGGER.debug("Result code [{}], message: [{}]", response.getResultCode(), response.getDiagnosticMessage());
return response.getResultCode() == ResultCode.SUCCESS;
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
return false;
}
use of org.ldaptive.ModifyRequest in project cas by apereo.
the class LdapUtils method executeModifyOperation.
/**
* Execute modify operation boolean.
*
* @param currentDn the current dn
* @param connectionFactory the connection factory
* @param attributes the attributes
* @return true/false
*/
public static boolean executeModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final Map<String, Set<String>> attributes) {
try {
val operation = new ModifyOperation(connectionFactory);
val mods = attributes.entrySet().stream().map(entry -> {
val values = entry.getValue().toArray(ArrayUtils.EMPTY_STRING_ARRAY);
val attr = new LdapAttribute(entry.getKey(), values);
LOGGER.debug("Constructed new attribute [{}]", attr);
return new AttributeModification(AttributeModification.Type.REPLACE, attr);
}).toArray(AttributeModification[]::new);
val request = new ModifyRequest(currentDn, mods);
val response = operation.execute(request);
LOGGER.debug("Result code [{}], message: [{}]", response.getResultCode(), response.getDiagnosticMessage());
return response.getResultCode() == ResultCode.SUCCESS;
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
return false;
}
use of org.ldaptive.ModifyRequest in project cas by apereo.
the class LdapTestUtils method modifyLdapEntry.
/**
* Modify ldap entry.
*
* @param serverCon the server con
* @param dn the dn
* @param attr the attr
* @param add the add
* @param connInit the connection initializer
*/
public static void modifyLdapEntry(final LDAPConnection serverCon, final String dn, final LdapAttribute attr, final AttributeModification.Type add, final BindConnectionInitializer connInit) {
val address = "ldap://" + serverCon.getConnectedAddress() + ':' + serverCon.getConnectedPort();
val config = new ConnectionConfig(address);
if (connInit != null) {
config.setConnectionInitializers(connInit);
}
LOGGER.debug("Created modification request connection configuration [{}] for [{}]", config, address);
val connectionFactory = new DefaultConnectionFactory(config);
try {
val modify = new ModifyOperation(connectionFactory);
val request = new ModifyRequest(dn, new AttributeModification(add, attr));
LOGGER.debug("Executing modification request [{}] with type [{}] for [{}]", request, add, dn);
val result = modify.execute(request);
if (!result.isSuccess()) {
LOGGER.warn("Result [{}]:[{}]", result.getResultCode(), result.getDiagnosticMessage());
}
} catch (final Exception e) {
LOGGER.info(e.getMessage(), e);
} finally {
connectionFactory.close();
}
}
use of org.ldaptive.ModifyRequest in project cas by apereo.
the class LdapPasswordSynchronizationAuthenticationPostProcessor method process.
@Override
public void process(final AuthenticationBuilder builder, final AuthenticationTransaction transaction) throws AuthenticationException {
val primaryCredential = transaction.getPrimaryCredential();
if (primaryCredential.isEmpty()) {
LOGGER.warn("Current authentication transaction does not have a primary credential");
return;
}
try {
val credential = UsernamePasswordCredential.class.cast(primaryCredential.get());
val filter = LdapUtils.newLdaptiveSearchFilter(ldapProperties.getSearchFilter(), LdapUtils.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Collections.singletonList(credential.getUsername()));
LOGGER.trace("Constructed LDAP filter [{}] to locate user and update password", filter);
val response = LdapUtils.executeSearchOperation(searchFactory, ldapProperties.getBaseDn(), filter, this.ldapProperties.getPageSize());
LOGGER.debug("LDAP response is [{}]", response);
if (LdapUtils.containsResultEntry(response)) {
val dn = response.getEntry().getDn();
LOGGER.trace("Updating account password for [{}]", dn);
val operation = new ModifyOperation(searchFactory);
val mod = new AttributeModification(AttributeModification.Type.REPLACE, getLdapPasswordAttribute(credential));
val updateResponse = operation.execute(new ModifyRequest(dn, mod));
LOGGER.trace("Result code [{}], message: [{}]", response.getResultCode(), response.getDiagnosticMessage());
val result = updateResponse.getResultCode() == ResultCode.SUCCESS;
if (result) {
LOGGER.info("Updated the LDAP entry's password for [{}] and base DN [{}]", filter.format(), ldapProperties.getBaseDn());
} else {
LOGGER.warn("Could not update the LDAP entry's password for [{}] and base DN [{}]", filter.format(), ldapProperties.getBaseDn());
}
} else {
LOGGER.error("Could not locate an LDAP entry for [{}] and base DN [{}]", filter.format(), ldapProperties.getBaseDn());
}
} catch (final Exception e) {
LoggingUtils.error(LOGGER, e);
}
}
Aggregations