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.springframework.security.providers;
17  
18  import java.security.Principal;
19  
20  import org.springframework.security.Authentication;
21  import org.springframework.security.GrantedAuthority;
22  import org.springframework.security.userdetails.UserDetails;
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 2752 2008-03-18 18:06:56Z luke_t $
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      /**
54       * Creates a token with the supplied array of authorities.
55       *
56       * @param authorities the list of <tt>GrantedAuthority</tt>s for the
57       *                    principal represented by this authentication object. A
58       *                    <code>null</code> value indicates that no authorities have been
59       *                    granted (pursuant to the interface contract specified by {@link
60       *                    Authentication#getAuthorities()}<code>null</code> should only be
61       *                    presented if the principal has not been authenticated).
62       */
63      public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
64          if (authorities != null) {
65              for (int i = 0; i < authorities.length; i++) {
66                  Assert.notNull(authorities[i],
67                          "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
68              }
69          }
70  
71          this.authorities = authorities;
72      }
73  
74      //~ Methods ========================================================================================================
75  
76      public boolean equals(Object obj) {
77          if (obj instanceof AbstractAuthenticationToken) {
78              AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
79  
80              if (!((this.getAuthorities() == null) && (test.getAuthorities() == null))) {
81                  if ((this.getAuthorities() == null) || (test.getAuthorities() == null)) {
82                      return false;
83                  }
84  
85                  if (this.getAuthorities().length != test.getAuthorities().length) {
86                      return false;
87                  }
88  
89                  for (int i = 0; i < this.getAuthorities().length; i++) {
90                      if (!this.getAuthorities()[i].equals(test.getAuthorities()[i])) {
91                          return false;
92                      }
93                  }
94              }
95  
96              if ((this.details == null) && (test.getDetails() != null)) {
97                  return false;
98              }
99  
100             if ((this.details != null) && (test.getDetails() == null)) {
101                 return false;
102             }
103 
104             if ((this.details != null) && (!this.details.equals(test.getDetails()))) {
105                 return false;
106             }
107 
108             if ((this.getCredentials() == null) && (test.getCredentials() != null)) {
109                 return false;
110             }
111 
112             if ((this.getCredentials() != null) && !this.getCredentials().equals(test.getCredentials())) {
113                 return false;
114             }
115             
116             if (this.getPrincipal() == null && test.getPrincipal() != null) {
117                 return false;
118             }
119 
120             if (this.getPrincipal() != null && !this.getPrincipal().equals(test.getPrincipal())) {
121                 return false;
122             }            
123             
124             return this.isAuthenticated() == test.isAuthenticated();
125         }
126 
127         return false;
128     }
129 
130     public GrantedAuthority[] getAuthorities() {
131         if (authorities == null) {
132             return null;
133         }
134 
135         GrantedAuthority[] copy = new GrantedAuthority[authorities.length];
136         System.arraycopy(authorities, 0, copy, 0, authorities.length);
137 
138         return copy;
139     }
140 
141     public Object getDetails() {
142         return details;
143     }
144 
145     public String getName() {
146         if (this.getPrincipal() instanceof UserDetails) {
147             return ((UserDetails) this.getPrincipal()).getUsername();
148         }
149         
150         if (getPrincipal() instanceof Principal) {
151             return ((Principal)getPrincipal()).getName();
152         }
153 
154         return (this.getPrincipal() == null) ? "" : this.getPrincipal().toString();
155     }
156 
157     public int hashCode() {
158         int code = 31;
159 
160         // Copy authorities to local variable for performance (SEC-223)
161         GrantedAuthority[] authorities = this.getAuthorities();
162 
163         if (authorities != null) {
164             for (int i = 0; i < authorities.length; i++) {
165                 code ^= authorities[i].hashCode();
166             }
167         }
168 
169         if (this.getPrincipal() != null) {
170             code ^= this.getPrincipal().hashCode();
171         }
172 
173         if (this.getCredentials() != null) {
174             code ^= this.getCredentials().hashCode();
175         }
176 
177         if (this.getDetails() != null) {
178             code ^= this.getDetails().hashCode();
179         }
180 
181         if (this.isAuthenticated()) {
182             code ^= -37;
183         }
184 
185         return code;
186     }
187 
188     public boolean isAuthenticated() {
189         return authenticated;
190     }
191 
192     public void setAuthenticated(boolean authenticated) {
193         this.authenticated = authenticated;
194     }
195 
196     public void setDetails(Object details) {
197         this.details = details;
198     }
199 
200     public String toString() {
201         StringBuffer sb = new StringBuffer();
202         sb.append(super.toString()).append(": ");
203         sb.append("Principal: ").append(this.getPrincipal()).append("; ");
204         sb.append("Password: [PROTECTED]; ");
205         sb.append("Authenticated: ").append(this.isAuthenticated()).append("; ");
206         sb.append("Details: ").append(this.getDetails()).append("; ");
207 
208         if (this.getAuthorities() != null) {
209             sb.append("Granted Authorities: ");
210 
211             for (int i = 0; i < this.getAuthorities().length; i++) {
212                 if (i > 0) {
213                     sb.append(", ");
214                 }
215 
216                 sb.append(this.getAuthorities()[i].toString());
217             }
218         } else {
219             sb.append("Not granted any authorities");
220         }
221 
222         return sb.toString();
223     }
224 }