Search in sources :

Example 1 with Response

use of org.ldaptive.Response 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
     */
public static boolean executePasswordModifyOperation(final String currentDn, final ConnectionFactory connectionFactory, final String oldPassword, final String newPassword, final AbstractLdapProperties.LdapType type) {
    try (Connection modifyConnection = createConnection(connectionFactory)) {
        if (!modifyConnection.getConnectionConfig().getUseSSL() && !modifyConnection.getConnectionConfig().getUseStartTLS()) {
            LOGGER.warn("Executing password modification op under a non-secure LDAP connection; " + "To modify password attributes, the connection to the LDAP server SHOULD be secured and/or encrypted.");
        }
        if (type == AbstractLdapProperties.LdapType.AD) {
            LOGGER.debug("Executing password modification op for active directory based on " + "[https://support.microsoft.com/en-us/kb/269190]");
            final ModifyOperation operation = new ModifyOperation(modifyConnection);
            final Response response = operation.execute(new ModifyRequest(currentDn, new AttributeModification(AttributeModificationType.REPLACE, new UnicodePwdAttribute(newPassword))));
            LOGGER.debug("Result code [{}], message: [{}]", response.getResult(), response.getMessage());
            return response.getResultCode() == ResultCode.SUCCESS;
        }
        LOGGER.debug("Executing password modification op for generic LDAP");
        final PasswordModifyOperation operation = new PasswordModifyOperation(modifyConnection);
        final Response response = operation.execute(new PasswordModifyRequest(currentDn, StringUtils.isNotBlank(oldPassword) ? new Credential(oldPassword) : null, new Credential(newPassword)));
        LOGGER.debug("Result code [{}], message: [{}]", response.getResult(), response.getMessage());
        return response.getResultCode() == ResultCode.SUCCESS;
    } catch (final LdapException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : Response(org.ldaptive.Response) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) Credential(org.ldaptive.Credential) UnicodePwdAttribute(org.ldaptive.ad.UnicodePwdAttribute) AttributeModification(org.ldaptive.AttributeModification) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) Connection(org.ldaptive.Connection) ModifyOperation(org.ldaptive.ModifyOperation) PasswordModifyOperation(org.ldaptive.extended.PasswordModifyOperation) PasswordModifyRequest(org.ldaptive.extended.PasswordModifyRequest) ModifyRequest(org.ldaptive.ModifyRequest) LdapException(org.ldaptive.LdapException)

Aggregations

AttributeModification (org.ldaptive.AttributeModification)1 Connection (org.ldaptive.Connection)1 Credential (org.ldaptive.Credential)1 LdapException (org.ldaptive.LdapException)1 ModifyOperation (org.ldaptive.ModifyOperation)1 ModifyRequest (org.ldaptive.ModifyRequest)1 Response (org.ldaptive.Response)1 UnicodePwdAttribute (org.ldaptive.ad.UnicodePwdAttribute)1 PasswordModifyOperation (org.ldaptive.extended.PasswordModifyOperation)1 PasswordModifyRequest (org.ldaptive.extended.PasswordModifyRequest)1