Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfish3r/66bd81ed1294780e00b0a9177fa97ec9 to your computer and use it in GitHub Desktop.
Save dfish3r/66bd81ed1294780e00b0a9177fa97ec9 to your computer and use it in GitHub Desktop.
Ldaptive AuthenticationHandler for Active Directory
import org.ldaptive.LdapException;
import org.ldaptive.auth.AuthenticationCriteria;
import org.ldaptive.auth.AuthenticationHandler;
import org.ldaptive.auth.AuthenticationHandlerResponse;
/**
* Provides an LDAP authentication implementation that wraps the invocation of another handler and then inspects the
* response. If the response result is false and it contains the {@link ActiveDirectoryAccountState.Error#ACCOUNT_EXPIRED}
* error, then the response result is changed from false to true.
*
* @author Middleware Services
*/
public class ActiveDirectoryAuthenticationHandler implements AuthenticationHandler
{
/** Authentication handler to invoke. */
private final AuthenticationHandler authHandler;
/**
* Creates a new active directory authentication handler.
*
* @param ah authentication handler
*/
public ActiveDirectoryAuthenticationHandler(final AuthenticationHandler ah)
{
authHandler = ah;
}
@Override
public AuthenticationHandlerResponse authenticate(final AuthenticationCriteria criteria)
throws LdapException
{
AuthenticationHandlerResponse response = authHandler.authenticate(criteria);
if (response != null && !response.getResult() && response.getMessage() != null) {
final ActiveDirectoryAccountState.Error adError = ActiveDirectoryAccountState.Error.parse(response.getMessage());
if (ActiveDirectoryAccountState.Error.ACCOUNT_EXPIRED.equals(adError)) {
response = new AuthenticationHandlerResponse(
true,
response.getResultCode(),
response.getConnection(),
response.getMessage(),
response.getControls(),
response.getMessageId());
}
}
return response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment