001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.providers;
017:
018: import org.acegisecurity.Authentication;
019: import org.acegisecurity.GrantedAuthority;
020:
021: import org.acegisecurity.userdetails.UserDetails;
022:
023: import org.springframework.util.Assert;
024:
025: /**
026: * Base class for <code>Authentication</code> objects.<p>Implementations which use this class should be immutable.</p>
027: *
028: * @author Ben Alex
029: * @author Luke Taylor
030: * @version $Id: AbstractAuthenticationToken.java 1496 2006-05-23 13:38:33Z benalex $
031: */
032: public abstract class AbstractAuthenticationToken implements
033: Authentication {
034: //~ Instance fields ================================================================================================
035:
036: private Object details;
037: private GrantedAuthority[] authorities;
038: private boolean authenticated = false;
039:
040: //~ Constructors ===================================================================================================
041:
042: /**
043: * Retained for compatibility with subclasses written before the
044: * <tt>AbstractAuthenticationToken(GrantedAuthority[])</tt> constructor
045: * was introduced.
046: *
047: * @deprecated in favour of the constructor which takes a
048: * <code>GrantedAuthority[]</code> argument.
049: */
050: public AbstractAuthenticationToken() {
051: }
052:
053: /**
054: * Creates a token with the supplied array of authorities.
055: *
056: * @param authorities the list of <tt>GrantedAuthority</tt>s for the
057: * principal represented by this authentication object. A
058: * <code>null</code> value indicates that no authorities have been
059: * granted (pursuant to the interface contract specified by {@link
060: * Authentication#getAuthorities()}<code>null</code> should only be
061: * presented if the principal has not been authenticated).
062: */
063: public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
064: if (authorities != null) {
065: for (int i = 0; i < authorities.length; i++) {
066: Assert
067: .notNull(
068: authorities[i],
069: "Granted authority element "
070: + i
071: + " is null - GrantedAuthority[] cannot contain any null elements");
072: }
073: }
074:
075: this .authorities = authorities;
076: }
077:
078: //~ Methods ========================================================================================================
079:
080: public boolean equals(Object obj) {
081: if (obj instanceof AbstractAuthenticationToken) {
082: AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
083:
084: if (!((this .getAuthorities() == null) && (test
085: .getAuthorities() == null))) {
086: if ((this .getAuthorities() == null)
087: || (test.getAuthorities() == null)) {
088: return false;
089: }
090:
091: if (this .getAuthorities().length != test
092: .getAuthorities().length) {
093: return false;
094: }
095:
096: for (int i = 0; i < this .getAuthorities().length; i++) {
097: if (!this .getAuthorities()[i].equals(test
098: .getAuthorities()[i])) {
099: return false;
100: }
101: }
102: }
103:
104: if ((this .details == null) && (test.getDetails() != null)) {
105: return false;
106: }
107:
108: if ((this .details != null) && (test.getDetails() == null)) {
109: return false;
110: }
111:
112: if ((this .details != null)
113: && (!this .details.equals(test.getDetails()))) {
114: return false;
115: }
116:
117: return (this .getPrincipal().equals(test.getPrincipal())
118: && this .getCredentials().equals(
119: test.getCredentials()) && (this
120: .isAuthenticated() == test.isAuthenticated()));
121: }
122:
123: return false;
124: }
125:
126: public GrantedAuthority[] getAuthorities() {
127: if (authorities == null) {
128: return null;
129: }
130:
131: GrantedAuthority[] copy = new GrantedAuthority[authorities.length];
132: System.arraycopy(authorities, 0, copy, 0, authorities.length);
133:
134: return copy;
135: }
136:
137: public Object getDetails() {
138: return details;
139: }
140:
141: public String getName() {
142: if (this .getPrincipal() instanceof UserDetails) {
143: return ((UserDetails) this .getPrincipal()).getUsername();
144: }
145:
146: return (this .getPrincipal() == null) ? "" : this .getPrincipal()
147: .toString();
148: }
149:
150: public int hashCode() {
151: int code = 31;
152:
153: // Copy authorities to local variable for performance (SEC-223)
154: GrantedAuthority[] authorities = this .getAuthorities();
155:
156: if (authorities != null) {
157: for (int i = 0; i < authorities.length; i++) {
158: code ^= authorities[i].hashCode();
159: }
160: }
161:
162: if (this .getPrincipal() != null) {
163: code ^= this .getPrincipal().hashCode();
164: }
165:
166: if (this .getCredentials() != null) {
167: code ^= this .getCredentials().hashCode();
168: }
169:
170: if (this .getDetails() != null) {
171: code ^= this .getDetails().hashCode();
172: }
173:
174: if (this .isAuthenticated()) {
175: code ^= -37;
176: }
177:
178: return code;
179: }
180:
181: public boolean isAuthenticated() {
182: return authenticated;
183: }
184:
185: public void setAuthenticated(boolean authenticated) {
186: this .authenticated = authenticated;
187: }
188:
189: public void setDetails(Object details) {
190: this .details = details;
191: }
192:
193: public String toString() {
194: StringBuffer sb = new StringBuffer();
195: sb.append(super .toString()).append(": ");
196: sb.append("Username: ").append(this .getPrincipal())
197: .append("; ");
198: sb.append("Password: [PROTECTED]; ");
199: sb.append("Authenticated: ").append(this .isAuthenticated())
200: .append("; ");
201: sb.append("Details: ").append(this .getDetails()).append("; ");
202:
203: if (this .getAuthorities() != null) {
204: sb.append("Granted Authorities: ");
205:
206: for (int i = 0; i < this .getAuthorities().length; i++) {
207: if (i > 0) {
208: sb.append(", ");
209: }
210:
211: sb.append(this .getAuthorities()[i].toString());
212: }
213: } else {
214: sb.append("Not granted any authorities");
215: }
216:
217: return sb.toString();
218: }
219: }
|