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.rememberme;
017:
018: import java.io.Serializable;
019:
020: import org.acegisecurity.GrantedAuthority;
021: import org.acegisecurity.providers.AbstractAuthenticationToken;
022:
023: /**
024: * Represents a remembered <code>Authentication</code>.<p>A remembered <code>Authentication</code> must provide a
025: * fully valid <code>Authentication</code>, including the <code>GrantedAuthority</code>[]s that apply.</p>
026: *
027: * @author Ben Alex
028: * @version $Id: RememberMeAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $
029: */
030: public class RememberMeAuthenticationToken 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 RememberMeAuthenticationToken(String key, Object principal,
050: GrantedAuthority[] authorities) {
051: super (authorities);
052:
053: if ((key == null) || ("".equals(key)) || (principal == null)
054: || "".equals(principal)) {
055: throw new IllegalArgumentException(
056: "Cannot pass null or empty values to constructor");
057: }
058:
059: this .keyHash = key.hashCode();
060: this .principal = principal;
061: setAuthenticated(true);
062: }
063:
064: //~ Methods ========================================================================================================
065:
066: public boolean equals(Object obj) {
067: if (!super .equals(obj)) {
068: return false;
069: }
070:
071: if (obj instanceof RememberMeAuthenticationToken) {
072: RememberMeAuthenticationToken test = (RememberMeAuthenticationToken) obj;
073:
074: if (this .getKeyHash() != test.getKeyHash()) {
075: return false;
076: }
077:
078: return true;
079: }
080:
081: return false;
082: }
083:
084: /**
085: * Always returns an empty <code>String</code>
086: *
087: * @return an empty String
088: */
089: public Object getCredentials() {
090: return "";
091: }
092:
093: public int getKeyHash() {
094: return this .keyHash;
095: }
096:
097: public Object getPrincipal() {
098: return this.principal;
099: }
100: }
|