Package org.apache.directory.ldap.client.api

Examples of org.apache.directory.ldap.client.api.LdapNetworkConnection


        BindRequest bindReq = new BindRequestImpl();
        bindReq.setDn( userDn );
        bindReq.setCredentials( "12345" ); // grace login
        bindReq.addControl( PP_REQ_CTRL );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );

        Thread.sleep( 2000 ); // let the password expire
        BindResponse bindResp = userConnection.bind( bindReq );
        assertTrue( userConnection.isAuthenticated() );
        PasswordPolicy ppolicy = getPwdRespCtrl( bindResp );
        assertEquals( 1, ppolicy.getResponse().getGraceAuthNRemaining() );

        Entry userEntry = adminConnection.lookup( userDn, "+" );
        Attribute pwdGraceAuthUseTime = userEntry.get( PasswordPolicySchemaConstants.PWD_GRACE_USE_TIME_AT );
        assertNotNull( pwdGraceAuthUseTime );

        Attribute pwdChangedTime = userEntry.get( PasswordPolicySchemaConstants.PWD_CHANGED_TIME_AT );

        ModifyRequest modReq = new ModifyRequestImpl();
        modReq.setName( userDn );
        modReq.replace( SchemaConstants.USER_PASSWORD_AT, "secret1" );
        ModifyResponse modResp = userConnection.modify( modReq );
        assertEquals( ResultCodeEnum.SUCCESS, modResp.getLdapResult().getResultCode() );

        userEntry = adminConnection.lookup( userDn, "+" );
        pwdGraceAuthUseTime = userEntry.get( PasswordPolicySchemaConstants.PWD_GRACE_USE_TIME_AT );
        assertNull( pwdGraceAuthUseTime );

        Attribute latestPwdChangedTime = userEntry.get( PasswordPolicySchemaConstants.PWD_CHANGED_TIME_AT );
        assertNotSame( pwdChangedTime.getString(), latestPwdChangedTime.getString() );

        userConnection.close();
        adminConnection.close();
    }
View Full Code Here


        BindRequest bindReq = new BindRequestImpl();
        bindReq.setDn( userDn );
        bindReq.setCredentials( "1234" ); // wrong password
        bindReq.addControl( PP_REQ_CTRL );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );

        for ( int i = 0; i < 4; i++ )
        {
            userConnection.bind( bindReq );
            assertFalse( userConnection.isAuthenticated() );
        }

        Entry userEntry = adminConnection.lookup( userDn, "+" );
        Attribute pwdAccountLockedTime = userEntry.get( PasswordPolicySchemaConstants.PWD_ACCOUNT_LOCKED_TIME_AT );
        assertNotNull( pwdAccountLockedTime );

        Thread.sleep( 10000 );
        bindReq = new BindRequestImpl();
        bindReq.setDn( userDn );
        bindReq.setCredentials( "12345" ); // correct password
        bindReq.addControl( PP_REQ_CTRL );
        userConnection.setTimeOut( Long.MAX_VALUE );
        userConnection.bind( bindReq );
        assertTrue( userConnection.isAuthenticated() );

        userConnection.close();
        adminConnection.close();
    }
View Full Code Here

     * Test an add operation performance
     */
    @Test
    public void testAddPerf() throws Exception
    {
        LdapConnection connection = new LdapNetworkConnection( "localhost", getLdapServer().getPort() );

        Dn dn = new Dn( "cn=test,dc=example,dc=com" );
        Entry entry = new DefaultEntry( getService().getSchemaManager(), dn,
            "ObjectClass: top",
            "ObjectClass: person",
            "sn: TEST",
            "cn: test" );

        connection.bind( "uid=admin,ou=system", "secret" );
        connection.add( entry );
        int nbIterations = 15000;

        long t0 = System.currentTimeMillis();
        long t00 = 0L;
        long tt0 = System.currentTimeMillis();

        for ( int i = 0; i < nbIterations; i++ )
        {
            if ( i % 100 == 0 )
            {
                long tt1 = System.currentTimeMillis();

                System.out.println( i + ", " + ( tt1 - tt0 ) );
                tt0 = tt1;
            }

            if ( i == 5000 )
            {
                t00 = System.currentTimeMillis();
            }

            String name = "test" + i;
            dn = new Dn( "cn=" + name + ",dc=example,dc=com" );
            entry = new DefaultEntry( getService().getSchemaManager(), dn,
                "ObjectClass: top",
                "ObjectClass: person",
                "sn", name.toUpperCase(),
                "cn", name );

            long ttt0 = System.nanoTime();
            connection.add( entry );
            long ttt1 = System.nanoTime();
            //System.out.println("added " + i + ", delta = " + (ttt1-ttt0)/1000);
        }

        long t1 = System.currentTimeMillis();

        Long deltaWarmed = ( t1 - t00 );
        System.out.println( "Delta : " + deltaWarmed + "( " + ( ( ( nbIterations - 5000 ) * 1000 ) / deltaWarmed )
            + " per s ) /" + ( t1 - t0 ) );

        int nbFound = 0;
        long t2 = System.currentTimeMillis();
        EntryCursor result = connection.search( "dc=example,dc=com", "(sn=test123*)", SearchScope.SUBTREE, "*" );

        while ( result.next() )
        {
            Entry res = result.get();

            System.out.println( res.getDn() );
            nbFound++;
        }

        result.close();
        long t3 = System.currentTimeMillis();
        System.out.println( "Delta search : " + ( t3 - t2 ) + " for " + nbFound + " entries" );

        connection.close();
    }
View Full Code Here

        Dn userDn = new Dn( "cn=userLockout,ou=system" );
        LdapConnection adminConnection = getAdminNetworkConnection( getLdapServer() );

        addUser( adminConnection, "userLockout", "12345" );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
        userConnection.setTimeOut( 0L );

        checkBind( userConnection, userDn, "badPassword", 3,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout,ou=system" );

        checkBind( userConnection, userDn, "badPassword", 1,
            "INVALID_CREDENTIALS: Bind failed: account was permanently locked" );

        userConnection.close();

        Entry userEntry = adminConnection.lookup( userDn, "+" );
        Attribute pwdAccountLockedTime = userEntry.get( PasswordPolicySchemaConstants.PWD_ACCOUNT_LOCKED_TIME_AT );
        assertNotNull( pwdAccountLockedTime );
        adminConnection.close();
View Full Code Here

        Dn userDn = new Dn( "cn=userLockout2,ou=system" );
        LdapConnection adminConnection = getAdminNetworkConnection( getLdapServer() );

        addUser( adminConnection, "userLockout2", "12345" );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
        userConnection.setTimeOut( 0L );

        checkBind( userConnection, userDn, "badPassword", 3,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout2,ou=system" );

        // Now, try to login until the delay is elapsed
        boolean success = false;
        int t = 0;

        for ( t = 0; t < 10; t++ )
        {
            try
            {
                userConnection.bind( userDn, "12345" );
                //System.out.println( "Attempt success " + ( t + 1 ) + " at " + new Date( System.currentTimeMillis() ) );
                success = true;
                break;
            }
            catch ( LdapException le )
            {
                //System.out.println( "Attempt failure " + ( t + 1 ) + " at " + new Date( System.currentTimeMillis() ) );
                Entry userEntry = adminConnection.lookup( userDn, "+" );
                Attribute pwdAccountLockedTime = userEntry
                    .get( PasswordPolicySchemaConstants.PWD_ACCOUNT_LOCKED_TIME_AT );
                assertNotNull( pwdAccountLockedTime );

                // Expected : wait 1 second before retrying
                Thread.sleep( 1000 );
            }
        }

        assertTrue( success );
        assertTrue( t >= 5 );
        userConnection.close();

        Entry userEntry = adminConnection.lookup( userDn, "+" );
        Attribute pwdAccountLockedTime = userEntry.get( PasswordPolicySchemaConstants.PWD_ACCOUNT_LOCKED_TIME_AT );
        assertNull( pwdAccountLockedTime );
        adminConnection.close();
View Full Code Here

        Dn userDn = new Dn( "cn=userLockout3,ou=system" );
        LdapConnection adminConnection = getAdminNetworkConnection( getLdapServer() );

        addUser( adminConnection, "userLockout3", "12345" );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
        userConnection.setTimeOut( 0L );

        // First attempt
        checkBind( userConnection, userDn, "badPassword", 1,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout3,ou=system" );

        Entry userEntry = adminConnection.lookup( userDn, "+" );
        Attribute pwdFailureTime = userEntry
            .get( PasswordPolicySchemaConstants.PWD_FAILURE_TIME_AT );
        assertNotNull( pwdFailureTime );
        assertEquals( 1, pwdFailureTime.size() );

        Thread.sleep( 1000 );

        // Second attempt
        checkBind( userConnection, userDn, "badPassword", 1,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout3,ou=system" );

        userEntry = adminConnection.lookup( userDn, "+" );
        pwdFailureTime = userEntry
            .get( PasswordPolicySchemaConstants.PWD_FAILURE_TIME_AT );
        assertNotNull( pwdFailureTime );
        assertEquals( 2, pwdFailureTime.size() );

        Thread.sleep( 1000 );

        // Third attempt
        checkBind( userConnection, userDn, "badPassword", 1,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout3,ou=system" );

        userEntry = adminConnection.lookup( userDn, "+" );
        pwdFailureTime = userEntry
            .get( PasswordPolicySchemaConstants.PWD_FAILURE_TIME_AT );
        assertNotNull( pwdFailureTime );
        assertEquals( 2, pwdFailureTime.size() );

        Thread.sleep( 1000 );

        // Forth attempt
        checkBind( userConnection, userDn, "badPassword", 1,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout3,ou=system" );

        userEntry = adminConnection.lookup( userDn, "+" );
        pwdFailureTime = userEntry
            .get( PasswordPolicySchemaConstants.PWD_FAILURE_TIME_AT );
        assertNotNull( pwdFailureTime );

        // We should not have more than 2 attempts stored
        assertEquals( 2, pwdFailureTime.size() );

        userConnection.close();
        adminConnection.close();
    }
View Full Code Here

        Dn userDn = new Dn( "cn=userLockout,ou=system" );
        LdapConnection adminConnection = getAdminNetworkConnection( getLdapServer() );

        addUser( adminConnection, "userLockout", "12345" );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );

        // Do two attempts
        checkBind( userConnection, userDn, "badPassword", 1,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout,ou=system" );

        // Wait 1 second
        Thread.sleep( 1000L );

        // Retry with the correct password : we should get rejected because it's too early
        checkBind( userConnection, userDn, "12345", 1,
            "INVALID_CREDENTIALS: Bind failed: ERR_229 Cannot authenticate user cn=userLockout,ou=system" );

        // Wait 1 second and a bit more
        Thread.sleep( 1200L );

        // Retry : it should work
        userConnection.bind( userDn, "12345" );
        userConnection.close();
        adminConnection.close();
    }
View Full Code Here

        Dn userDn = new Dn( "cn=userAllowUserChange,ou=system" );
        LdapConnection adminConnection = getAdminNetworkConnection( getLdapServer() );

        addUser( adminConnection, "userAllowUserChange", "12345" );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
        userConnection.setTimeOut( 0L );

        // We should be able to bind
        checkBindSuccess( userDn, "12345" );

        // Now, try to change the password
        ModifyRequest modReq = new ModifyRequestImpl();
        modReq.setName( userDn );
        modReq.addControl( PP_REQ_CTRL );
        modReq.replace( "userPassword", "67890" );

        userConnection = getNetworkConnectionAs( getLdapServer(), userDn.getName(), "12345" );
        userConnection.setTimeOut( 0L );

        ModifyResponse modifyResponse = userConnection.modify( modReq );

        assertEquals( ResultCodeEnum.INSUFFICIENT_ACCESS_RIGHTS, modifyResponse.getLdapResult().getResultCode() );

        // Now, allow the user to change his password
        policyConfig.setPwdAllowUserChange( true );

        modifyResponse = userConnection.modify( modReq );

        assertEquals( ResultCodeEnum.SUCCESS, modifyResponse.getLdapResult().getResultCode() );

        userConnection.close();

        checkBindSuccess( userDn, "67890" );
        adminConnection.close();
    }
View Full Code Here

        Dn userDn = new Dn( "cn=userExpireWarning,ou=system" );
        LdapConnection adminConnection = getAdminNetworkConnection( getLdapServer() );

        addUser( adminConnection, "userExpireWarning", "12345" );

        LdapConnection userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
        userConnection.setTimeOut( 0L );

        BindRequest bindReq = new BindRequestImpl();
        bindReq.setDn( userDn );
        bindReq.setCredentials( "12345" );
        bindReq.addControl( PP_REQ_CTRL );

        for ( int i = 0; i < 5; i++ )
        {
            BindResponse bindResponse = userConnection.bind( bindReq );
            assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );

            PasswordPolicy respCtrl = getPwdRespCtrl( bindResponse );
            assertNotNull( respCtrl );

            if ( i < 2 )
            {
                assertNull( respCtrl.getResponse() );
            }
            else
            {
                assertEquals( 5 - i, respCtrl.getResponse().getTimeBeforeExpiration() );
            }

            // Added an one second wait
            Thread.sleep( 1000 );
        }

        // Added an one second wait
        Thread.sleep( 1000 );

        // We should not be able to login
        checkBindFailure( userDn, "12345" );

        userConnection.close();
        adminConnection.close();
    }
View Full Code Here

        try {
            String userCn = "userExpireWarningToo";
            Dn userDn = new Dn( "cn=" + userCn + ",ou=system" );
            String password = "12345";
            adminConnection = getAdminNetworkConnection( getLdapServer() );
            userConnection = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
            userConnection.setTimeOut( 0L );
            userConnection2 = new LdapNetworkConnection( "localhost", ldapServer.getPort() );
            userConnection2.setTimeOut( 0L );

            addUser( adminConnection, userCn, password );

            BindRequest bindReq = new BindRequestImpl();
View Full Code Here

TOP

Related Classes of org.apache.directory.ldap.client.api.LdapNetworkConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.