View Javadoc

1   /* Copyright 2004, 2005, 2006 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.GrantedAuthority;
20  
21  import org.acegisecurity.userdetails.UserDetails;
22  
23  import org.springframework.util.Assert;
24  
25  
26  /**
27   * Base class for <code>Authentication</code> objects.<p>Implementations which use this class should be immutable.</p>
28   *
29   * @author Ben Alex
30   * @author Luke Taylor
31   * @version $Id: AbstractAuthenticationToken.java 1496 2006-05-23 13:38:33Z benalex $
32   */
33  public abstract class AbstractAuthenticationToken implements Authentication {
34      //~ Instance fields ================================================================================================
35  
36      private Object details;
37      private GrantedAuthority[] authorities;
38      private boolean authenticated = false;
39  
40      //~ Constructors ===================================================================================================
41  
42  /**
43       * Retained for compatibility with subclasses written before the
44       * <tt>AbstractAuthenticationToken(GrantedAuthority[])</tt> constructor
45       * was introduced.
46       *
47       * @deprecated in favour of the constructor which takes a
48       *             <code>GrantedAuthority[]</code> argument.
49       */
50      public AbstractAuthenticationToken() {}
51  
52  /**
53       * Creates a token with the supplied array of authorities.
54       *
55       * @param authorities the list of <tt>GrantedAuthority</tt>s for the
56       *        principal represented by this authentication object. A
57       *        <code>null</code> value indicates that no authorities have been
58       *        granted (pursuant to the interface contract specified by {@link
59       *        Authentication#getAuthorities()}<code>null</code> should only be
60       *        presented if the principal has not been authenticated).
61       */
62      public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
63          if (authorities != null) {
64              for (int i = 0; i < authorities.length; i++) {
65                  Assert.notNull(authorities[i],
66                      "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
67              }
68          }
69  
70          this.authorities = authorities;
71      }
72  
73      //~ Methods ========================================================================================================
74  
75      public boolean equals(Object obj) {
76          if (obj instanceof AbstractAuthenticationToken) {
77              AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
78  
79              if (!((this.getAuthorities() == null) && (test.getAuthorities() == null))) {
80                  if ((this.getAuthorities() == null) || (test.getAuthorities() == null)) {
81                      return false;
82                  }
83  
84                  if (this.getAuthorities().length != test.getAuthorities().length) {
85                      return false;
86                  }
87  
88                  for (int i = 0; i < this.getAuthorities().length; i++) {
89                      if (!this.getAuthorities()[i].equals(test.getAuthorities()[i])) {
90                          return false;
91                      }
92                  }
93              }
94  
95              if ((this.details == null) && (test.getDetails() != null)) {
96                  return false;
97              }
98  
99              if ((this.details != null) && (test.getDetails() == null)) {
100                 return false;
101             }
102 
103             if ((this.details != null) && (!this.details.equals(test.getDetails()))) {
104                 return false;
105             }
106 
107             return (this.getPrincipal().equals(test.getPrincipal())
108             && this.getCredentials().equals(test.getCredentials())
109             && (this.isAuthenticated() == test.isAuthenticated()));
110         }
111 
112         return false;
113     }
114 
115     public GrantedAuthority[] getAuthorities() {
116         if (authorities == null) {
117             return null;
118         }
119 
120         GrantedAuthority[] copy = new GrantedAuthority[authorities.length];
121         System.arraycopy(authorities, 0, copy, 0, authorities.length);
122 
123         return copy;
124     }
125 
126     public Object getDetails() {
127         return details;
128     }
129 
130     public String getName() {
131         if (this.getPrincipal() instanceof UserDetails) {
132             return ((UserDetails) this.getPrincipal()).getUsername();
133         }
134 
135         return (this.getPrincipal() == null) ? "" : this.getPrincipal().toString();
136     }
137 
138     public int hashCode() {
139         int code = 31;
140 
141         // Copy authorities to local variable for performance (SEC-223)
142         GrantedAuthority[] authorities = this.getAuthorities();
143 
144         if (authorities != null) {
145             for (int i = 0; i < authorities.length; i++) {
146                 code ^= authorities[i].hashCode();
147             }
148         }
149 
150         if (this.getPrincipal() != null) {
151             code ^= this.getPrincipal().hashCode();
152         }
153 
154         if (this.getCredentials() != null) {
155             code ^= this.getCredentials().hashCode();
156         }
157 
158         if (this.getDetails() != null) {
159             code ^= this.getDetails().hashCode();
160         }
161 
162         if (this.isAuthenticated()) {
163             code ^= -37;
164         }
165 
166         return code;
167     }
168 
169     public boolean isAuthenticated() {
170         return authenticated;
171     }
172 
173     public void setAuthenticated(boolean authenticated) {
174         this.authenticated = authenticated;
175     }
176 
177     public void setDetails(Object details) {
178         this.details = details;
179     }
180 
181     public String toString() {
182         StringBuffer sb = new StringBuffer();
183         sb.append(super.toString()).append(": ");
184         sb.append("Username: ").append(this.getPrincipal()).append("; ");
185         sb.append("Password: [PROTECTED]; ");
186         sb.append("Authenticated: ").append(this.isAuthenticated()).append("; ");
187         sb.append("Details: ").append(this.getDetails()).append("; ");
188 
189         if (this.getAuthorities() != null) {
190             sb.append("Granted Authorities: ");
191 
192             for (int i = 0; i < this.getAuthorities().length; i++) {
193                 if (i > 0) {
194                     sb.append(", ");
195                 }
196 
197                 sb.append(this.getAuthorities()[i].toString());
198             }
199         } else {
200             sb.append("Not granted any authorities");
201         }
202 
203         return sb.toString();
204     }
205 }