View Javadoc

1   /* Copyright 2004, 2005 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.providers;
17  
18  import org.acegisecurity.Authentication;
19  import org.acegisecurity.userdetails.UserDetails;
20  
21  
22  /***
23   * Provides a <code>String</code> representation of the Authentication token.
24   *
25   * @author Ben Alex
26   * @version $Id: AbstractAuthenticationToken.java,v 1.10 2005/11/29 13:10:09 benalex Exp $
27   */
28  public abstract class AbstractAuthenticationToken implements Authentication {
29      //~ Methods ================================================================
30  
31      /***
32       * Subclasses should override if they wish to provide additional details
33       * about the authentication event.
34       *
35       * @return always <code>null</code>
36       */
37      public Object getDetails() {
38          return null;
39      }
40  
41      public String getName() {
42          if (this.getPrincipal() instanceof UserDetails) {
43              return ((UserDetails) this.getPrincipal()).getUsername();
44          }
45  
46          return this.getPrincipal().toString();
47      }
48  
49      public boolean equals(Object obj) {
50          if (obj instanceof AbstractAuthenticationToken) {
51              AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
52  
53              if (!((this.getAuthorities() == null)
54                  && (test.getAuthorities() == null))) {
55                  if ((this.getAuthorities() == null)
56                      || (test.getAuthorities() == null)) {
57                      return false;
58                  }
59  
60                  if (this.getAuthorities().length != test.getAuthorities().length) {
61                      return false;
62                  }
63  
64                  for (int i = 0; i < this.getAuthorities().length; i++) {
65                      if (!this.getAuthorities()[i].equals(
66                              test.getAuthorities()[i])) {
67                          return false;
68                      }
69                  }
70              }
71  
72              return (this.getPrincipal().equals(test.getPrincipal())
73              && this.getCredentials().equals(test.getCredentials())
74              && (this.isAuthenticated() == test.isAuthenticated()));
75          }
76  
77          return false;
78      }
79  
80      public String toString() {
81          StringBuffer sb = new StringBuffer();
82          sb.append(super.toString()).append(": ");
83          sb.append("Username: ").append(this.getPrincipal()).append("; ");
84          sb.append("Password: [PROTECTED]; ");
85          sb.append("Authenticated: ").append(this.isAuthenticated()).append("; ");
86          sb.append("Details: ").append(this.getDetails()).append("; ");
87  
88          if (this.getAuthorities() != null) {
89              sb.append("Granted Authorities: ");
90  
91              for (int i = 0; i < this.getAuthorities().length; i++) {
92                  if (i > 0) {
93                      sb.append(", ");
94                  }
95  
96                  sb.append(this.getAuthorities()[i].toString());
97              }
98          } else {
99              sb.append("Not granted any authorities");
100         }
101 
102         return sb.toString();
103     }
104 }