private DirContextOperations bind(DirContextOperations user, String password) {
    ContextSource contextSource = getContextSource();

    if (contextSource == null) {
      String message = "Missing ContextSource - failing authentication.";
      LOG.debug(message);
      throw new InternalAuthenticationServiceException(message);
    }

    if (!(contextSource instanceof BaseLdapPathContextSource)) {
      String message = String.format("Unexpected ContextSource type (%s) - failing authentication.", contextSource.getClass().getName());
      LOG.debug(message);
      throw new InternalAuthenticationServiceException(message);
    }

    BaseLdapPathContextSource baseLdapPathContextSource = (BaseLdapPathContextSource) contextSource;
    Name userDistinguishedName = user.getDn();
    Name fullDn = AmbariLdapUtils.getFullDn(userDistinguishedName, baseLdapPathContextSource.getBaseLdapName());

    LOG.debug("Attempting to bind as {}", fullDn);

    DirContext dirContext = null;

    try {
      // Perform the authentication.  The result is not used because it is expected that the supplied
      // user data has all of the attributes for the authenticated user. If authentication fails, it
      // expected that the supplied user data will be destroyed or orphaned.
      dirContext = baseLdapPathContextSource.getContext(fullDn.toString(), password);

      // Build a new DirContextAdapter using the attributes from the passed in user details since it
      // is expected these details will be more complete of querying for them from the bound context.
      // Some LDAP server implementations will no return all attributes to the bound context due to
      // the filter being used in the query.
      return new DirContextAdapter(user.getAttributes(), userDistinguishedName, baseLdapPathContextSource.getBaseLdapName());
    } catch (org.springframework.ldap.AuthenticationException e) {
      String message = String.format("Failed to bind as %s - %s", user.getDn().toString(), e.getMessage());
      if (LOG.isTraceEnabled()) {
        LOG.trace(message, e);
      } else if (LOG.isDebugEnabled()) {
        LOG.debug(message);
      }
      throw new BadCredentialsException("The username or password is incorrect.");
    } finally {
      LdapUtils.closeContext(dirContext);
    }
  }
Attempt to authenticate a user with the configured LDAP server by performing an LDAP bind.
<p>
Using the distinguished name provided in the supplied user data and the supplied password,
attempt to authenticate with the configured LDAP server. If authentication is successful, use the
attributes from the supplied user data rather than the attributes associated with the bound context
because some scenarios result in missing data within the bound context due to LDAP server implementations.
<p>
If authentication is not successful, throw a {@link BadCredentialsException}.

@param user     the user data containing the relevant DN and associated attributes
@param password the password
@return the authenticated user details
@throws BadCredentialsException if authentication fails
ambari-server/src/main/java/org/apache/ambari/server/security/authorization/AmbariLdapBindAuthenticator.java