View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi.impl;
18  
19  import java.io.Serializable;
20  import java.sql.Date;
21  import java.sql.Timestamp;
22  import java.util.Arrays;
23  
24  import org.apache.jetspeed.security.PasswordCredential;
25  import org.apache.jetspeed.security.om.InternalCredential;
26  
27  /***
28   * <p>
29   * Default Password credential implementation. Provides the same mechanism as J2EE
30   * <code>javax.resource.spi.security.PasswordCredential</code>.
31   * </p>
32   * 
33   * <p>
34   * Code borrowed from the Geronimo project.
35   * </p>
36   * 
37   * @author <a href="mailto:dlestrat@apache.org">David Le Strat </a>
38   */
39  public class DefaultPasswordCredentialImpl implements PasswordCredential, Serializable
40  {
41  
42      /*** The default uid. */
43      private static final long serialVersionUID = -4975305752376365096L;
44  
45      /*** The user name. */
46      private String userName;
47  
48      /*** The password. */
49      private char[] password;
50  
51      /*** The update required state */
52      private boolean updateRequired;
53      
54      /*** The enabled state. */
55      private boolean enabled = true;
56      
57      /*** The expired state. */
58      private boolean expired;
59      
60      /*** The expiration date. */
61      private Date expirationDate;
62      
63      /*** The previous authentication in date */
64      private Timestamp previousAuthenticationDate;
65      
66      /*** The last authentication in date */
67      private Timestamp lastAuthenticationDate;
68      
69      /*** The number of authentication failures */
70      private int authenticationFailures;
71  
72      /***
73       * @param userName
74       * @param password
75       */
76      public DefaultPasswordCredentialImpl(String userName, char[] password)
77      {
78          this.userName = userName;
79          this.password = (char[]) password.clone();
80      }
81      
82      public DefaultPasswordCredentialImpl(String userName, InternalCredential credential)
83      {
84          this(userName, credential.getValue().toCharArray());
85          this.updateRequired = credential.isUpdateRequired();
86          this.enabled = credential.isEnabled();
87          this.expired = credential.isExpired();
88          this.expirationDate = credential.getExpirationDate();
89          this.previousAuthenticationDate = credential.getPreviousAuthenticationDate();
90          this.lastAuthenticationDate = credential.getLastAuthenticationDate();
91          this.authenticationFailures = credential.getAuthenticationFailures();
92      }
93      
94      /***
95       * @return The username.
96       */
97      public String getUserName()
98      {
99          return userName;
100     }
101 
102     /***
103      * @return The password.
104      */
105     public char[] getPassword()
106     {
107         return (char[]) password.clone();
108     }
109     
110     
111     /***
112      * @see org.apache.jetspeed.security.PasswordCredential#isUpdateRequired()
113      */
114     public boolean isUpdateRequired()
115     {
116         return updateRequired;
117     }
118 
119     /***
120      * @see org.apache.jetspeed.security.PasswordCredential#isEnabled()
121      */
122     public boolean isEnabled()
123     {
124         return enabled;
125     }
126 
127     /***
128      * @see org.apache.jetspeed.security.PasswordCredential#isExpired()
129      */
130     public boolean isExpired()
131     {
132         return expired;
133     }
134 
135     /***
136      * @see org.apache.jetspeed.security.PasswordCredential#getExpirationDate()
137      */
138     public Date getExpirationDate()
139     {
140         return expirationDate;
141     }
142     
143     /***
144      * @see org.apache.jetspeed.security.PasswordCredential#getPreviousAuthenticationDate()
145      */
146     public Timestamp getPreviousAuthenticationDate()
147     {
148         return previousAuthenticationDate;
149     }
150 
151     /***
152      * @see org.apache.jetspeed.security.PasswordCredential#getLastAuthenticationDate()
153      */
154     public Timestamp getLastAuthenticationDate()
155     {
156         return lastAuthenticationDate;
157     }
158 
159     /*** 
160      * @see org.apache.jetspeed.security.PasswordCredential#getAuthenticationFailures()
161      */
162     public int getAuthenticationFailures()
163     {
164         return authenticationFailures;
165     }
166 
167     /***
168      * @see java.lang.Object#equals(java.lang.Object)
169      */
170     public boolean equals(Object o)
171     {
172         if (this == o)
173             return true;
174         if (!(o instanceof DefaultPasswordCredentialImpl))
175             return false;
176 
177         final DefaultPasswordCredentialImpl credential = (DefaultPasswordCredentialImpl) o;
178 
179         if (!Arrays.equals(password, credential.password))
180             return false;
181         if (!userName.equals(credential.userName))
182             return false;
183 
184         return true;
185     }
186 
187     /***
188      * @see java.lang.Object#hashCode()
189      */
190     public int hashCode()
191     {
192         int result = userName.hashCode();
193         for (int i = 0; i < password.length; i++)
194         {
195             result *= password[i];
196         }
197         return result;
198     }
199 }