Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id CA0CD961B for ; Thu, 13 Oct 2011 20:01:09 +0000 (UTC) Received: (qmail 34168 invoked by uid 500); 13 Oct 2011 20:01:09 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 34132 invoked by uid 500); 13 Oct 2011 20:01:09 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 34125 invoked by uid 99); 13 Oct 2011 20:01:09 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Oct 2011 20:01:09 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 13 Oct 2011 20:01:06 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id A86062388A02 for ; Thu, 13 Oct 2011 20:00:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1183056 - in /directory/apacheds/trunk: core-integ/src/test/java/org/apache/directory/server/core/authn/ppolicy/ core/src/main/java/org/apache/directory/server/core/authn/ Date: Thu, 13 Oct 2011 20:00:44 -0000 To: commits@directory.apache.org From: kayyagari@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111013200044.A86062388A02@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: kayyagari Date: Thu Oct 13 20:00:44 2011 New Revision: 1183056 URL: http://svn.apache.org/viewvc?rev=1183056&view=rev Log: o fixed an issue with maintaining the password history o added a new test Modified: directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/authn/ppolicy/PasswordPolicyTest.java directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/AuthenticationInterceptor.java directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/PasswordHistory.java Modified: directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/authn/ppolicy/PasswordPolicyTest.java URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/authn/ppolicy/PasswordPolicyTest.java?rev=1183056&r1=1183055&r2=1183056&view=diff ============================================================================== --- directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/authn/ppolicy/PasswordPolicyTest.java (original) +++ directory/apacheds/trunk/core-integ/src/test/java/org/apache/directory/server/core/authn/ppolicy/PasswordPolicyTest.java Thu Oct 13 20:00:44 2011 @@ -432,6 +432,73 @@ public class PasswordPolicyTest extends } + @Test + public void testPwdHistory() throws Exception + { + policyConfig.setPwdInHistory( 2 ); + + LdapConnection connection = getAdminNetworkConnection( getLdapServer() ); + + Dn userDn = new Dn( "cn=userPwdHist,ou=system" ); + Entry userEntry = new DefaultEntry( + userDn.toString(), + "ObjectClass: top", + "ObjectClass: person", + "cn: userPwdHist", + "sn: userPwdHist_sn", + "userPassword: 12345" ); + + AddRequest addRequest = new AddRequestImpl(); + addRequest.setEntry( userEntry ); + addRequest.addControl( PP_REQ_CTRL ); + + connection.add( addRequest ); + + Entry entry = connection.lookup( userDn, "*", "+" ); + + Attribute pwdHistAt = entry.get( PasswordPolicySchemaConstants.PWD_HISTORY_AT ); + assertNotNull( pwdHistAt ); + assertEquals( 1, pwdHistAt.size() ); + + Thread.sleep( 1000 );// to avoid creating a history value with the same timestamp + ModifyRequest modReq = new ModifyRequestImpl(); + modReq.setName( userDn ); + modReq.addControl( PP_REQ_CTRL ); + modReq.replace( SchemaConstants.USER_PASSWORD_AT, "67891" ); + + connection.modify( modReq ); + + entry = connection.lookup( userDn, "*", "+" ); + + pwdHistAt = entry.get( PasswordPolicySchemaConstants.PWD_HISTORY_AT ); + assertNotNull( pwdHistAt ); + assertEquals( 2, pwdHistAt.size() ); + + Thread.sleep( 1000 );// to avoid creating a history value with the same timestamp + modReq = new ModifyRequestImpl(); + modReq.setName( userDn ); + modReq.addControl( PP_REQ_CTRL ); + modReq.replace( SchemaConstants.USER_PASSWORD_AT, "abcde" ); + + ModifyResponse modResp = connection.modify( modReq ); + assertEquals( ResultCodeEnum.SUCCESS, modResp.getLdapResult().getResultCode() ); + + entry = connection.lookup( userDn, "*", "+" ); + pwdHistAt = entry.get( PasswordPolicySchemaConstants.PWD_HISTORY_AT ); + assertNotNull( pwdHistAt ); + + // it should still hold only 2 values + assertEquals( 2, pwdHistAt.size() ); + + // try to reuse the password, should fail + modResp = connection.modify( modReq ); + assertEquals( ResultCodeEnum.CONSTRAINT_VIOLATION, modResp.getLdapResult().getResultCode() ); + + PasswordPolicy respCtrl = getPwdRespCtrl( modResp ); + assertEquals( PASSWORD_IN_HISTORY, respCtrl.getResponse().getPasswordPolicyError() ); + } + + private PasswordPolicy getPwdRespCtrl( Response resp ) throws Exception { Control control = resp.getControls().get( PP_REQ_CTRL.getOid() ); Modified: directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/AuthenticationInterceptor.java URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/AuthenticationInterceptor.java?rev=1183056&r1=1183055&r2=1183056&view=diff ============================================================================== --- directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/AuthenticationInterceptor.java (original) +++ directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/AuthenticationInterceptor.java Thu Oct 13 20:00:44 2011 @@ -624,7 +624,7 @@ public class AuthenticationInterceptor e pwdHistoryAt = new DefaultAttribute( AT_PWD_HISTORY ); } - Set pwdHistSet = new TreeSet(); + List pwdHistLst = new ArrayList(); for ( Value value : pwdHistoryAt ) { @@ -646,22 +646,25 @@ public class AuthenticationInterceptor e "invalid reuse of password present in password history" ); } - pwdHistSet.add( pwdh ); + pwdHistLst.add( pwdh ); } - PasswordHistory newPwdHist = new PasswordHistory( pwdChangedTime, newPassword ); - pwdHistSet.add( newPwdHist ); - + if ( pwdHistLst.size() >= histSize ) + { + // see the javadoc of PasswordHistory + Collections.sort( pwdHistLst ); + + // remove the oldest value + PasswordHistory remPwdHist = ( PasswordHistory ) pwdHistLst.toArray()[histSize - 1]; + Attribute tempAt = new DefaultAttribute( AT_PWD_HISTORY ); + tempAt.add( remPwdHist.getHistoryValue() ); + pwdRemHistMod = new DefaultModification( REMOVE_ATTRIBUTE, tempAt ); + } + pwdHistoryAt.clear(); + PasswordHistory newPwdHist = new PasswordHistory( pwdChangedTime, newPassword ); pwdHistoryAt.add( newPwdHist.getHistoryValue() ); pwdAddHistMod = new DefaultModification( ADD_ATTRIBUTE, pwdHistoryAt ); - - if ( pwdHistSet.size() > histSize ) - { - PasswordHistory remPwdHist = ( PasswordHistory ) pwdHistSet.toArray()[histSize - 1]; - pwdHistoryAt.add( remPwdHist.getHistoryValue() ); - pwdRemHistMod = new DefaultModification( REMOVE_ATTRIBUTE, pwdHistoryAt ); - } } next.modify( modifyContext ); Modified: directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/PasswordHistory.java URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/PasswordHistory.java?rev=1183056&r1=1183055&r2=1183056&view=diff ============================================================================== --- directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/PasswordHistory.java (original) +++ directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/authn/PasswordHistory.java Thu Oct 13 20:00:44 2011 @@ -23,13 +23,15 @@ package org.apache.directory.server.core import org.apache.directory.shared.ldap.model.constants.SchemaConstants; import org.apache.directory.shared.util.Base64; -import org.apache.directory.shared.util.DateUtils; import org.apache.directory.shared.util.Strings; /** * A class to hold the data of historical passwords of a entry. - * + * Note: This class's natural ordering is inconsistent with the equals() method + * hence it is advised not to use this in any implementations of sorted sets + * Instead use Collections.sort() to sort the collection of PasswordHistory objects. + * * @author Apache Directory Project * @version $Rev$, $Date$ */ @@ -131,7 +133,8 @@ public class PasswordHistory implements PasswordHistory other = ( PasswordHistory ) o; - return this.getTime().equals( other.getTime() ); + return this.getTime().equals( other.getTime() ) && + this.data.equals( other.data ); } @@ -154,11 +157,4 @@ public class PasswordHistory implements return "PasswordHistory [time=" + time + ", syntaxOID=" + syntaxOID + ", length=" + length + ", data=" + data + "]"; } - - public static void main( String[] args ) - { - byte[] pwdhBytes = new PasswordHistory( DateUtils.getGeneralizedTime(), "secret".getBytes() ).getHistoryValue(); - PasswordHistory pwdHistory = new PasswordHistory( Strings.utf8ToString(pwdhBytes) ); - System.out.println( pwdHistory ); - } }