View Javadoc

1   /**
2    *
3    * Copyright 2005 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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.geronimo.security.jaas;
18  
19  import java.io.Serializable;
20  import java.util.Arrays;
21  import javax.security.auth.DestroyFailedException;
22  import javax.security.auth.Destroyable;
23  import javax.security.auth.RefreshFailedException;
24  import javax.security.auth.Refreshable;
25  
26  
27  /**
28   * A username/password credential.  Used to store the username/password in the
29   * Subject's private credentials.
30   *
31   * @version $Revision: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
32   */
33  public class UsernamePasswordCredential implements Destroyable, Refreshable, Serializable {
34  
35      private String username;
36      private char[] password;
37      private boolean destroyed;
38  
39      public UsernamePasswordCredential(String username, char[] password) {
40          assert username != null;
41          assert password != null;
42  
43          this.username = username;
44          this.password = new char[password.length];
45          System.arraycopy(password, 0, this.password, 0, password.length);
46      }
47  
48      public String getUsername() {
49          return username;
50      }
51  
52      public char[] getPassword() {
53          return password;
54      }
55  
56      public void destroy() throws DestroyFailedException {
57          Arrays.fill(password, ' ');
58          
59          username = null;
60          password = null;
61          destroyed = true;
62      }
63  
64      public boolean isDestroyed() {
65          return destroyed;
66      }
67  
68      public void refresh() throws RefreshFailedException {
69      }
70  
71      public boolean isCurrent() {
72          return !destroyed;
73      }
74  
75      public boolean equals(Object o) {
76          if (this == o) return true;
77          if (!(o instanceof UsernamePasswordCredential)) return false;
78  
79          final UsernamePasswordCredential credential = (UsernamePasswordCredential) o;
80  
81          if (destroyed != credential.destroyed) return false;
82          if (!Arrays.equals(password, credential.password)) return false;
83          if (username != null ? !username.equals(credential.username) : credential.username != null) return false;
84  
85          return true;
86      }
87  
88      public int hashCode() {
89          int result;
90          result = (username != null ? username.hashCode() : 0);
91          result = 29 * result + (destroyed ? 1 : 0);
92          return result;
93      }
94  }