String principal;
if ( ! ctx.getEnvironment().containsKey( Context.SECURITY_PRINCIPAL ) )
{
throw new LdapAuthenticationException();
}
else
{
principal = ( String ) ctx.getEnvironment().get( Context.SECURITY_PRINCIPAL );
if ( principal == null )
{
throw new LdapAuthenticationException();
}
}
// ---- lookup the principal entry's userPassword attribute
LdapName principalDn = new LdapName( principal );
Invocation invocation = InvocationStack.getInstance().peek();
DirectoryPartitionNexusProxy proxy = invocation.getProxy();
Attributes userEntry;
try
{
userEntry = proxy.lookup( principalDn, new String[] {"userPassword"}, USERLOOKUP_BYPASS );
if ( userEntry == null )
{
throw new LdapAuthenticationException( "Failed to lookup user for authentication: " + principal );
}
}
catch( Exception cause )
{
LdapAuthenticationException e = new LdapAuthenticationException();
e.setRootCause( e );
throw e;
}
Object userPassword;
Attribute userPasswordAttr = userEntry.get( "userPassword" );
// ---- assert that credentials match
if ( userPasswordAttr == null )
{
userPassword = ArrayUtils.EMPTY_BYTE_ARRAY;
}
else
{
userPassword = userPasswordAttr.get();
if ( userPassword instanceof String )
{
userPassword = ( ( String ) userPassword ).getBytes();
}
}
if ( ! ArrayUtils.isEquals( creds, userPassword ) )
{
throw new LdapAuthenticationException();
}
return new LdapPrincipal( principalDn, AuthenticationLevel.SIMPLE );
}