View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.server.core.authn;
22  
23  
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.util.Base64;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * A class to hold the data of historical passwords of a entry.
31   * Note: This class's natural ordering is inconsistent with the equals() method
32   *       hence it is advised not to use this in any implementations of sorted sets
33   *       Instead use Collections.sort() to sort the collection of PasswordHistory objects.
34   *
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   * @version $Rev$, $Date$
37   */
38  public class PasswordHistory implements Comparable<PasswordHistory>
39  {
40      /** time when password was last changed */
41      private String time;
42  
43      /** the syntax OID that is to be used on the password data */
44      private String syntaxOID = SchemaConstants.OCTET_STRING_SYNTAX;
45  
46      /** the length of the password data */
47      private int length;
48  
49      /** password octet string */
50      private String data;
51  
52      private static final char DELIMITER = '#';
53  
54  
55      /**
56       * Create a new instance of PasswordHistory
57       * 
58       * @param pwdHistoryVal The history date
59       */
60      public PasswordHistory( String pwdHistoryVal )
61      {
62          int pos = pwdHistoryVal.indexOf( DELIMITER );
63          time = pwdHistoryVal.substring( 0, pos );
64  
65          pos++;
66          int nextPos = pwdHistoryVal.indexOf( DELIMITER, pos );
67          syntaxOID = pwdHistoryVal.substring( pos, nextPos );
68  
69          nextPos++;
70          pos = pwdHistoryVal.indexOf( DELIMITER, nextPos );
71          length = Integer.parseInt( pwdHistoryVal.substring( nextPos, pos ) );
72  
73          data = pwdHistoryVal.substring( pos + 1 );
74      }
75  
76  
77      /**
78       * Create a new instance of PasswordHistory
79       * 
80       * @param time The time we changed the password
81       * @param password The password to store
82       */
83      public PasswordHistory( String time, byte[] password )
84      {
85          this.time = time;
86          this.data = String.valueOf( Base64.encode( password ) );
87          this.length = data.length();
88      }
89  
90  
91      public byte[] getHistoryValue()
92      {
93          StringBuilder sb = new StringBuilder();
94  
95          sb.append( time ).append( DELIMITER );
96  
97          sb.append( syntaxOID ).append( DELIMITER );
98  
99          sb.append( length ).append( DELIMITER );
100 
101         sb.append( data );
102 
103         return Strings.getBytesUtf8( sb.toString() );
104     }
105 
106 
107     public String getTime()
108     {
109         return time;
110     }
111 
112 
113     public String getSyntaxOID()
114     {
115         return syntaxOID;
116     }
117 
118 
119     public int getLength()
120     {
121         return length;
122     }
123 
124 
125     public byte[] getPassword()
126     {
127         return Base64.decode( data.toCharArray() );
128     }
129 
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public int compareTo( PasswordHistory o )
136     {
137         return o.getTime().compareTo( time );
138     }
139 
140 
141     /**
142      * @see Object#toString()
143      */
144     @Override
145     public boolean equals( Object o )
146     {
147         if ( !( o instanceof PasswordHistory ) )
148         {
149             return false;
150         }
151 
152         PasswordHistory../../../org/apache/directory/server/core/authn/PasswordHistory.html#PasswordHistory">PasswordHistory other = ( PasswordHistory ) o;
153 
154         return this.getTime().equals( other.getTime() ) && this.data.equals( other.data );
155     }
156 
157 
158     @Override
159     public int hashCode()
160     {
161         final int prime = 31;
162         int result = 1;
163         result = prime * result + ( ( data == null ) ? 0 : data.hashCode() );
164         result = prime * result + length;
165         result = prime * result + ( ( syntaxOID == null ) ? 0 : syntaxOID.hashCode() );
166         result = prime * result + ( ( time == null ) ? 0 : time.hashCode() );
167         return result;
168     }
169 
170 
171     @Override
172     public String toString()
173     {
174         return "PasswordHistory [time=" + time + ", syntaxOID=" + syntaxOID + ", length=" + length + ", data=" + data
175             + "]";
176     }
177 }