View Javadoc

1   /* Copyright 2004, 2005 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.acegisecurity.providers.cas;
17  
18  import org.acegisecurity.GrantedAuthority;
19  import org.acegisecurity.providers.AbstractAuthenticationToken;
20  import org.acegisecurity.userdetails.UserDetails;
21  
22  import org.springframework.util.Assert;
23  
24  import java.io.Serializable;
25  
26  import java.util.List;
27  
28  
29  /***
30   * Represents a successful CAS <code>Authentication</code>.
31   *
32   * @author Ben Alex
33   * @version $Id: CasAuthenticationToken.java,v 1.10 2005/11/29 13:10:07 benalex Exp $
34   */
35  public class CasAuthenticationToken extends AbstractAuthenticationToken
36      implements Serializable {
37      //~ Instance fields ========================================================
38  
39      private List proxyList;
40      private Object credentials;
41      private Object principal;
42      private String proxyGrantingTicketIou;
43      private UserDetails userDetails;
44      private GrantedAuthority[] authorities;
45      private boolean authenticated;
46      private int keyHash;
47  
48      //~ Constructors ===========================================================
49  
50      /***
51       * Constructor.
52       *
53       * @param key to identify if this object made by a given {@link
54       *        CasAuthenticationProvider}
55       * @param principal the username from CAS (cannot be <code>null</code>)
56       * @param credentials the service/proxy ticket ID from CAS (cannot be
57       *        <code>null</code>)
58       * @param authorities the authorities granted to the user (from {@link
59       *        CasAuthoritiesPopulator}) (cannot be <code>null</code>)
60       * @param userDetails the user details (from {@link
61       *        CasAuthoritiesPopulator}) (cannot be <code>null</code>)
62       * @param proxyList the list of proxies from CAS (cannot be
63       *        <code>null</code>)
64       * @param proxyGrantingTicketIou the PGT-IOU ID from CAS (cannot be
65       *        <code>null</code>, but may be an empty <code>String</code> if no
66       *        PGT-IOU ID was provided)
67       *
68       * @throws IllegalArgumentException if a <code>null</code> was passed
69       */
70      public CasAuthenticationToken(String key, Object principal,
71          Object credentials, GrantedAuthority[] authorities,
72          UserDetails userDetails, List proxyList, String proxyGrantingTicketIou) {
73          if ((key == null) || ("".equals(key)) || (principal == null)
74              || "".equals(principal) || (credentials == null)
75              || "".equals(credentials) || (authorities == null)
76              || (userDetails == null) || (proxyList == null)
77              || (proxyGrantingTicketIou == null)) {
78              throw new IllegalArgumentException(
79                  "Cannot pass null or empty values to constructor");
80          }
81  
82          for (int i = 0; i < authorities.length; i++) {
83              Assert.notNull(authorities[i],
84                  "Granted authority element " + i
85                  + " is null - GrantedAuthority[] cannot contain any null elements");
86          }
87  
88          this.keyHash = key.hashCode();
89          this.principal = principal;
90          this.credentials = credentials;
91          this.authorities = authorities;
92          this.userDetails = userDetails;
93          this.proxyList = proxyList;
94          this.proxyGrantingTicketIou = proxyGrantingTicketIou;
95          this.authenticated = true;
96      }
97  
98      protected CasAuthenticationToken() {
99          throw new IllegalArgumentException("Cannot use default constructor");
100     }
101 
102     //~ Methods ================================================================
103 
104     public void setAuthenticated(boolean isAuthenticated) {
105         this.authenticated = isAuthenticated;
106     }
107 
108     public boolean isAuthenticated() {
109         return this.authenticated;
110     }
111 
112     public GrantedAuthority[] getAuthorities() {
113         return this.authorities;
114     }
115 
116     public Object getCredentials() {
117         return this.credentials;
118     }
119 
120     public int getKeyHash() {
121         return this.keyHash;
122     }
123 
124     public Object getPrincipal() {
125         return this.principal;
126     }
127 
128     /***
129      * Obtains the proxy granting ticket IOU.
130      *
131      * @return the PGT IOU-ID or an empty <code>String</code> if no proxy
132      *         callback was requested when validating the service ticket
133      */
134     public String getProxyGrantingTicketIou() {
135         return proxyGrantingTicketIou;
136     }
137 
138     public List getProxyList() {
139         return proxyList;
140     }
141 
142     public UserDetails getUserDetails() {
143         return userDetails;
144     }
145 
146     public boolean equals(Object obj) {
147         if (!super.equals(obj)) {
148             return false;
149         }
150 
151         if (obj instanceof CasAuthenticationToken) {
152             CasAuthenticationToken test = (CasAuthenticationToken) obj;
153 
154             // proxyGrantingTicketIou is never null due to constructor
155             if (!this.getProxyGrantingTicketIou().equals(test
156                     .getProxyGrantingTicketIou())) {
157                 return false;
158             }
159 
160             // proxyList is never null due to constructor
161             if (!this.getProxyList().equals(test.getProxyList())) {
162                 return false;
163             }
164 
165             if (this.getKeyHash() != test.getKeyHash()) {
166                 return false;
167             }
168 
169             return true;
170         }
171 
172         return false;
173     }
174 
175     public String toString() {
176         StringBuffer sb = new StringBuffer();
177         sb.append(super.toString());
178         sb.append("; Credentials (Service/Proxy Ticket): " + this.credentials);
179         sb.append("; Proxy-Granting Ticket IOU: " + this.proxyGrantingTicketIou);
180         sb.append("; Proxy List: " + this.proxyList.toString());
181 
182         return sb.toString();
183     }
184 }