@Test
public void testSMD5() throws Exception
{
apply( service, getUserAddLdif() );
String userDn = "uid=akarasulu,ou=users,ou=system";
LdapContext ctx = new ServerLdapContext( service,
service.getSession( new DN( userDn ), "test".getBytes() ), new DN( userDn ) );
// Check that we can get the attributes
Attributes attrs = ctx.getAttributes( "" );
assertNotNull( attrs );
assertTrue( attrs.get( "uid" ).contains( "akarasulu" ) );
// now modify the password for akarasulu : 'secret', encrypted using SMD5
Attribute userPasswordAttribute = new BasicAttribute( "userPassword", "{SMD5}tQ9wo/VBuKsqBtylMMCcORbnYOJFMyDJ" );
ctx.modifyAttributes( "", new ModificationItem[] {
new ModificationItem( DirContext.REPLACE_ATTRIBUTE, userPasswordAttribute ) } );
// close and try with old password (should fail)
ctx.close();
try
{
ctx = new ServerLdapContext( service,
service.getSession( new DN( userDn ), "test".getBytes() ), new DN( userDn ) );
fail( "Authentication with old password should fail" );
}
catch ( Exception e )
{
// we should fail
}
finally
{
if ( ctx != null )
{
ctx.close();
}
}
// try again now with new password (should be successful)
ctx = new ServerLdapContext( service,
service.getSession( new DN( userDn ), "secret".getBytes() ), new DN( userDn ) );
attrs = ctx.getAttributes( "" );
assertNotNull( attrs );
assertTrue( attrs.get( "uid" ).contains( "akarasulu" ) );
// try again now with new password, to check that the
// cache is updated (should be successfull)
ctx = new ServerLdapContext( service,
service.getSession( new DN( userDn ), "secret".getBytes() ), new DN( userDn ) );
attrs = ctx.getAttributes( "" );
assertNotNull( attrs );
assertTrue( attrs.get( "uid" ).contains( "akarasulu" ) );
}