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.anonymous;
017:
018: import org.acegisecurity.GrantedAuthority;
019:
020: import org.acegisecurity.providers.AbstractAuthenticationToken;
021:
022: import java.io.Serializable;
023:
024: /**
025: * Represents an anonymous <code>Authentication</code>.
026: *
027: * @author Ben Alex
028: * @version $Id: AnonymousAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $
029: */
030: public class AnonymousAuthenticationToken extends
031: AbstractAuthenticationToken implements Serializable {
032: //~ Instance fields ================================================================================================
033:
034: private static final long serialVersionUID = 1L;
035: private Object principal;
036: private int keyHash;
037:
038: //~ Constructors ===================================================================================================
039:
040: /**
041: * Constructor.
042: *
043: * @param key to identify if this object made by an authorised client
044: * @param principal the principal (typically a <code>UserDetails</code>)
045: * @param authorities the authorities granted to the principal
046: *
047: * @throws IllegalArgumentException if a <code>null</code> was passed
048: */
049: public AnonymousAuthenticationToken(String key, Object principal,
050: GrantedAuthority[] authorities) {
051: super (authorities);
052:
053: if ((key == null) || ("".equals(key)) || (principal == null)
054: || "".equals(principal) || (authorities == null)
055: || (authorities.length == 0)) {
056: throw new IllegalArgumentException(
057: "Cannot pass null or empty values to constructor");
058: }
059:
060: this .keyHash = key.hashCode();
061: this .principal = principal;
062: setAuthenticated(true);
063: }
064:
065: //~ Methods ========================================================================================================
066:
067: public boolean equals(Object obj) {
068: if (!super .equals(obj)) {
069: return false;
070: }
071:
072: if (obj instanceof AnonymousAuthenticationToken) {
073: AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj;
074:
075: if (this .getKeyHash() != test.getKeyHash()) {
076: return false;
077: }
078:
079: return true;
080: }
081:
082: return false;
083: }
084:
085: /**
086: * Always returns an empty <code>String</code>
087: *
088: * @return an empty String
089: */
090: public Object getCredentials() {
091: return "";
092: }
093:
094: public int getKeyHash() {
095: return this .keyHash;
096: }
097:
098: public Object getPrincipal() {
099: return this.principal;
100: }
101: }
|