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.cas;
017:
018: import org.acegisecurity.GrantedAuthority;
019:
020: import org.acegisecurity.providers.AbstractAuthenticationToken;
021:
022: import org.acegisecurity.userdetails.UserDetails;
023:
024: import java.io.Serializable;
025:
026: import java.util.List;
027:
028: /**
029: * Represents a successful CAS <code>Authentication</code>.
030: *
031: * @author Ben Alex
032: * @version $Id: CasAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $
033: */
034: public class CasAuthenticationToken extends AbstractAuthenticationToken
035: implements Serializable {
036: //~ Instance fields ================================================================================================
037:
038: private static final long serialVersionUID = 1L;
039: private final List proxyList;
040: private final Object credentials;
041: private final Object principal;
042: private final String proxyGrantingTicketIou;
043: private final UserDetails userDetails;
044: private final int keyHash;
045:
046: //~ Constructors ===================================================================================================
047:
048: /**
049: * Constructor.
050: *
051: * @param key to identify if this object made by a given {@link
052: * CasAuthenticationProvider}
053: * @param principal typically the UserDetails object (cannot be <code>null</code>)
054: * @param credentials the service/proxy ticket ID from CAS (cannot be
055: * <code>null</code>)
056: * @param authorities the authorities granted to the user (from {@link
057: * CasAuthoritiesPopulator}) (cannot be <code>null</code>)
058: * @param userDetails the user details (from {@link
059: * CasAuthoritiesPopulator}) (cannot be <code>null</code>)
060: * @param proxyList the list of proxies from CAS (cannot be
061: * <code>null</code>)
062: * @param proxyGrantingTicketIou the PGT-IOU ID from CAS (cannot be
063: * <code>null</code>, but may be an empty <code>String</code> if no
064: * PGT-IOU ID was provided)
065: *
066: * @throws IllegalArgumentException if a <code>null</code> was passed
067: */
068: public CasAuthenticationToken(final String key,
069: final Object principal, final Object credentials,
070: final GrantedAuthority[] authorities,
071: final UserDetails userDetails, final List proxyList,
072: final String proxyGrantingTicketIou) {
073: super (authorities);
074:
075: if ((key == null) || ("".equals(key)) || (principal == null)
076: || "".equals(principal) || (credentials == null)
077: || "".equals(credentials) || (authorities == null)
078: || (userDetails == null) || (proxyList == null)
079: || (proxyGrantingTicketIou == null)) {
080: throw new IllegalArgumentException(
081: "Cannot pass null or empty values to constructor");
082: }
083:
084: this .keyHash = key.hashCode();
085: this .principal = principal;
086: this .credentials = credentials;
087: this .userDetails = userDetails;
088: this .proxyList = proxyList;
089: this .proxyGrantingTicketIou = proxyGrantingTicketIou;
090: setAuthenticated(true);
091: }
092:
093: //~ Methods ========================================================================================================
094:
095: public boolean equals(final Object obj) {
096: if (!super .equals(obj)) {
097: return false;
098: }
099:
100: if (obj instanceof CasAuthenticationToken) {
101: CasAuthenticationToken test = (CasAuthenticationToken) obj;
102:
103: // proxyGrantingTicketIou is never null due to constructor
104: if (!this .getProxyGrantingTicketIou().equals(
105: test.getProxyGrantingTicketIou())) {
106: return false;
107: }
108:
109: // proxyList is never null due to constructor
110: if (!this .getProxyList().equals(test.getProxyList())) {
111: return false;
112: }
113:
114: if (this .getKeyHash() != test.getKeyHash()) {
115: return false;
116: }
117:
118: return true;
119: }
120:
121: return false;
122: }
123:
124: public Object getCredentials() {
125: return this .credentials;
126: }
127:
128: public int getKeyHash() {
129: return this .keyHash;
130: }
131:
132: public Object getPrincipal() {
133: return this .principal;
134: }
135:
136: /**
137: * Obtains the proxy granting ticket IOU.
138: *
139: * @return the PGT IOU-ID or an empty <code>String</code> if no proxy callback was requested when validating the
140: * service ticket
141: */
142: public String getProxyGrantingTicketIou() {
143: return proxyGrantingTicketIou;
144: }
145:
146: public List getProxyList() {
147: return proxyList;
148: }
149:
150: public UserDetails getUserDetails() {
151: return userDetails;
152: }
153:
154: public String toString() {
155: StringBuffer sb = new StringBuffer();
156: sb.append(super .toString());
157: sb.append("; Credentials (Service/Proxy Ticket): ").append(
158: this .credentials);
159: sb.append("; Proxy-Granting Ticket IOU: ").append(
160: this .proxyGrantingTicketIou);
161: sb.append("; Proxy List: ").append(this.proxyList);
162:
163: return (sb.toString());
164: }
165: }
|