You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-dev@portals.apache.org by at...@apache.org on 2008/09/08 16:15:09 UTC

svn commit: r693107 - in /portals/jetspeed-2/portal/branches/security-refactoring: components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/ jetspeed-api/src/main/java/org/apache/jetspeed/security/

Author: ate
Date: Mon Sep  8 07:15:06 2008
New Revision: 693107

URL: http://svn.apache.org/viewvc?rev=693107&view=rev
Log:
refactoring the PasswordCredential implementation 

Added:
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java   (contents, props changed)
      - copied, changed from r693053, portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/DefaultPasswordCredentialImpl.java
Removed:
    portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/DefaultPasswordCredentialImpl.java
Modified:
    portals/jetspeed-2/portal/branches/security-refactoring/jetspeed-api/src/main/java/org/apache/jetspeed/security/PasswordCredential.java

Copied: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java (from r693053, portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/DefaultPasswordCredentialImpl.java)
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java?p2=portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java&p1=portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/DefaultPasswordCredentialImpl.java&r1=693053&r2=693107&rev=693107&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/DefaultPasswordCredentialImpl.java (original)
+++ portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java Mon Sep  8 07:15:06 2008
@@ -16,37 +16,50 @@
  */
 package org.apache.jetspeed.security.spi.impl;
 
-import java.io.Serializable;
 import java.sql.Date;
 import java.sql.Timestamp;
-import java.util.Arrays;
 
 import org.apache.jetspeed.security.PasswordCredential;
-import org.apache.jetspeed.security.om.InternalCredential;
+import org.apache.jetspeed.security.User;
 
 /**
  * <p>
- * Default Password credential implementation. Provides the same mechanism as J2EE
- * <code>javax.resource.spi.security.PasswordCredential</code>.
+ * Default Password credential implementation
  * </p>
  * 
- * <p>
- * Code borrowed from the Geronimo project.
- * </p>
- * 
- * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
+ * @version $Id$
  */
-public class DefaultPasswordCredentialImpl implements PasswordCredential, Serializable
+public class PasswordCredentialImpl implements PasswordCredential
 {
-
-    /** The default uid. */
     private static final long serialVersionUID = -4975305752376365096L;
+    
+    private User user;
 
-    /** The user name. */
-    private String userName;
-
-    /** The password. */
+    /** The "raw" password value */
     private char[] password;
+    
+    /**
+     * the "old" password for authenticating password change (if set)
+     */
+    private String oldPassword;
+    /**
+     * the "new" password to be used for changing the real password
+     */
+    private String newPassword;
+    
+    /**
+     * flag indicating a new password value is set to be processed
+     */
+    private boolean newPasswordSet;
+    
+    /**
+     * flag indicating if the current password is encoded
+     */
+    private boolean passwordEncoded;
+    
+    private boolean updateAllowed;
+    
+    private boolean stateReadOnly;
 
     /** The update required state */
     private boolean updateRequired;
@@ -69,26 +82,30 @@
     /** The number of authentication failures */
     private int authenticationFailures;
 
-    /**
-     * @param userName
-     * @param password
-     */
-    public DefaultPasswordCredentialImpl(String userName, char[] password)
+    public PasswordCredentialImpl()
+    {        
+    }
+    
+    public PasswordCredentialImpl(User user, char[] password)
     {
-        this.userName = userName;
-        this.password = (char[]) password.clone();
+        this.user = user;
+        this.password = password;
     }
     
-    public DefaultPasswordCredentialImpl(String userName, InternalCredential credential)
+    private void checkUpdatePassword()
     {
-        this(userName, credential.getValue().toCharArray());
-        this.updateRequired = credential.isUpdateRequired();
-        this.enabled = credential.isEnabled();
-        this.expired = credential.isExpired();
-        this.expirationDate = credential.getExpirationDate();
-        this.previousAuthenticationDate = credential.getPreviousAuthenticationDate();
-        this.lastAuthenticationDate = credential.getLastAuthenticationDate();
-        this.authenticationFailures = credential.getAuthenticationFailures();
+        if (!updateAllowed)
+        {
+            throw new IllegalStateException();
+        }
+    }
+    
+    private void checkUpdateState()
+    {
+        if (stateReadOnly)
+        {
+            throw new IllegalStateException();
+        }
     }
     
     /**
@@ -96,7 +113,12 @@
      */
     public String getUserName()
     {
-        return userName;
+        return user.getName();
+    }
+    
+    public User getUser()
+    {
+        return user;
     }
 
     /**
@@ -104,96 +126,169 @@
      */
     public char[] getPassword()
     {
-        return (char[]) password.clone();
+        return password != null ? (char[]) password.clone() : null;
     }
     
+    public void setPassword(char[] password, boolean encoded)
+    {
+        checkUpdatePassword();
+        char[] value = password.clone();
+        this.passwordEncoded = encoded;
+        if (!value.equals(password))
+        {
+            this.password = value;
+            oldPassword = null;
+            newPassword = null;
+            newPasswordSet = true;
+        }
+    }
+    
+    public void setPassword(String oldPassword, String newPassword)
+    {
+        checkUpdatePassword();
+        if (!newPassword.equals(oldPassword))
+        {
+            this.newPassword = newPassword;
+            this.oldPassword = oldPassword;
+            password = null;
+            passwordEncoded = false;
+            newPasswordSet = true;
+        }
+    }
+    
+    public void clearNewPasswordSet()
+    {
+        oldPassword = null;
+        newPassword = null;
+        newPasswordSet = false;
+    }
+    
+    public String getOldPassword()
+    {
+        return oldPassword;
+    }
+    
+    public String getNewPassword()
+    {
+        return newPassword;
+    }
+    
+    public boolean isNewPasswordSet()
+    {
+        return newPasswordSet;
+    }
+    
+    public boolean isPasswordEncoded()
+    {
+        return passwordEncoded;
+    }
+    
+    public void setPasswordEncoded(boolean passwordEncoded)
+    {
+        checkUpdatePassword();
+        this.passwordEncoded = passwordEncoded;
+    }
+    
+    public boolean isUpdateAllowed()
+    {
+        return updateAllowed;
+    }
+    
+    public void setUpdateAllowed(boolean updateAllowed)
+    {
+        this.updateAllowed = true;
+    }
+    
+    public boolean isStateReadOnly()
+    {
+        return stateReadOnly;
+    }
+    
+    public void setStateReadOnly(boolean stateReadOnly)
+    {
+        this.stateReadOnly = stateReadOnly; 
+    }
     
-    /**
-     * @see org.apache.jetspeed.security.PasswordCredential#isUpdateRequired()
-     */
     public boolean isUpdateRequired()
     {
         return updateRequired;
     }
+    
+    public void setUpdateRequired(boolean updateRequired)
+    {
+        checkUpdateState();
+        this.updateRequired = updateRequired;
+    }
 
-    /**
-     * @see org.apache.jetspeed.security.PasswordCredential#isEnabled()
-     */
     public boolean isEnabled()
     {
         return enabled;
     }
+    
+    public void setEnabled(boolean enabled)
+    {
+        checkUpdateState();
+        this.enabled = enabled;
+    }
 
-    /**
-     * @see org.apache.jetspeed.security.PasswordCredential#isExpired()
-     */
     public boolean isExpired()
     {
         return expired;
     }
+    
+    public void setExpired(boolean expired)
+    {
+        checkUpdateState();
+        this.expired = expired;
+    }
 
-    /**
-     * @see org.apache.jetspeed.security.PasswordCredential#getExpirationDate()
-     */
     public Date getExpirationDate()
     {
         return expirationDate;
     }
     
-    /**
-     * @see org.apache.jetspeed.security.PasswordCredential#getPreviousAuthenticationDate()
-     */
+    public void setExpirationDate(Date expirationDate)
+    {
+        checkUpdateState();
+        this.expirationDate = expirationDate;
+    }
+    
     public Timestamp getPreviousAuthenticationDate()
     {
         return previousAuthenticationDate;
     }
+    
+    public void setPreviousAuthenticationDate(Timestamp previousAuthenticationDate)
+    {
+        checkUpdateState();
+        this.previousAuthenticationDate = previousAuthenticationDate;
+    }
 
-    /**
-     * @see org.apache.jetspeed.security.PasswordCredential#getLastAuthenticationDate()
-     */
     public Timestamp getLastAuthenticationDate()
     {
         return lastAuthenticationDate;
     }
 
-    /** 
-     * @see org.apache.jetspeed.security.PasswordCredential#getAuthenticationFailures()
-     */
+    public void setLastAuthenticationDate(Timestamp lastAuthenticationDate)
+    {
+        checkUpdateState();
+        this.lastAuthenticationDate = lastAuthenticationDate;
+    }
+
     public int getAuthenticationFailures()
     {
         return authenticationFailures;
     }
 
-    /**
-     * @see java.lang.Object#equals(java.lang.Object)
-     */
-    public boolean equals(Object o)
+    public void resetAuthenticationFailures()
     {
-        if (this == o)
-            return true;
-        if (!(o instanceof DefaultPasswordCredentialImpl))
-            return false;
-
-        final DefaultPasswordCredentialImpl credential = (DefaultPasswordCredentialImpl) o;
-
-        if (!Arrays.equals(password, credential.password))
-            return false;
-        if (!userName.equals(credential.userName))
-            return false;
-
-        return true;
+        checkUpdateState();
+        authenticationFailures = 0;
     }
-
-    /**
-     * @see java.lang.Object#hashCode()
-     */
-    public int hashCode()
+    
+    public void setAuthenticationFailures(int authenticationFailures)
     {
-        int result = userName.hashCode();
-        for (int i = 0; i < password.length; i++)
-        {
-            result *= password[i];
-        }
-        return result;
+        checkUpdateState();
+        this.authenticationFailures = authenticationFailures;
     }
 }
\ No newline at end of file

Propchange: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java
------------------------------------------------------------------------------
    cvs2svn:cvs-rev = 1.4

Propchange: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: portals/jetspeed-2/portal/branches/security-refactoring/components/jetspeed-security/src/main/java/org/apache/jetspeed/security/spi/impl/PasswordCredentialImpl.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: portals/jetspeed-2/portal/branches/security-refactoring/jetspeed-api/src/main/java/org/apache/jetspeed/security/PasswordCredential.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/portal/branches/security-refactoring/jetspeed-api/src/main/java/org/apache/jetspeed/security/PasswordCredential.java?rev=693107&r1=693106&r2=693107&view=diff
==============================================================================
--- portals/jetspeed-2/portal/branches/security-refactoring/jetspeed-api/src/main/java/org/apache/jetspeed/security/PasswordCredential.java (original)
+++ portals/jetspeed-2/portal/branches/security-refactoring/jetspeed-api/src/main/java/org/apache/jetspeed/security/PasswordCredential.java Mon Sep  8 07:15:06 2008
@@ -35,8 +35,6 @@
      */
     User getUser();    
     
-    void setUserName(String name);
-    
     /**
      * @return raw (possibly encoded) password.
      */
@@ -55,8 +53,9 @@
     /**
      * Set a new raw (possibly encoded) password
      * @param password
+     * @param encoded
      */
-    void setPassword(char[] password);
+    void setPassword(char[] password, boolean encoded);
     
     /**
      * Set a new (plain text) password also (optionally) providing the old (plain text) password
@@ -65,9 +64,11 @@
     
     boolean isNewPasswordSet();
     
+    void clearNewPasswordSet();
+    
     boolean isPasswordEncoded();
     
-    boolean setPasswordEncoded(boolean encoded);
+    void setPasswordEncoded(boolean passwordEncoded);
     
     void setUpdateRequired(boolean updateRequired);
     



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org