View Javadoc

1   /* Copyright 2004, 2005, 2006 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.springframework.security.providers.cas;
17  
18  import org.jasig.cas.client.validation.Assertion;
19  import org.springframework.security.GrantedAuthority;
20  
21  import org.springframework.security.providers.AbstractAuthenticationToken;
22  
23  import org.springframework.security.userdetails.UserDetails;
24  
25  import java.io.Serializable;
26  
27  /**
28   * Represents a successful CAS <code>Authentication</code>.
29   *
30   * @author Ben Alex
31   * @author Scott Battaglia
32   * @version $Id: CasAuthenticationToken.java 2710 2008-03-10 18:33:34Z sbattaglia $
33   */
34  public class CasAuthenticationToken extends AbstractAuthenticationToken implements Serializable {
35      //~ Instance fields ================================================================================================
36  
37      private static final long serialVersionUID = 1L;
38      private final Object credentials;
39      private final Object principal;
40      private final UserDetails userDetails;
41      private final int keyHash;
42      private final Assertion assertion;
43  
44      //~ Constructors ===================================================================================================
45  
46  /**
47       * Constructor.
48       *
49       * @param key to identify if this object made by a given {@link
50       *        CasAuthenticationProvider}
51       * @param principal typically the UserDetails object (cannot  be <code>null</code>)
52       * @param credentials the service/proxy ticket ID from CAS (cannot be
53       *        <code>null</code>)
54       * @param authorities the authorities granted to the user (from the {@link
55       *        org.springframework.security.userdetails.UserDetailsService}) (cannot be <code>null</code>)
56       * @param userDetails the user details (from the {@link
57       *        org.springframework.security.userdetails.UserDetailsService}) (cannot be <code>null</code>)
58       * @param assertion the assertion returned from the CAS servers.  It contains the principal and how to obtain a
59       *        proxy ticket for the user.
60       *
61       * @throws IllegalArgumentException if a <code>null</code> was passed
62       */
63      public CasAuthenticationToken(final String key, final Object principal, final Object credentials,
64          final GrantedAuthority[] authorities, final UserDetails userDetails, final Assertion assertion) {
65          super(authorities);
66  
67          if ((key == null) || ("".equals(key)) || (principal == null) || "".equals(principal) || (credentials == null)
68              || "".equals(credentials) || (authorities == null) || (userDetails == null) || (assertion == null)) {
69              throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
70          }
71  
72          this.keyHash = key.hashCode();
73          this.principal = principal;
74          this.credentials = credentials;
75          this.userDetails = userDetails;
76          this.assertion = assertion;
77          setAuthenticated(true);
78      }
79  
80      //~ Methods ========================================================================================================
81  
82      public boolean equals(final Object obj) {
83          if (!super.equals(obj)) {
84              return false;
85          }
86  
87          if (obj instanceof CasAuthenticationToken) {
88              CasAuthenticationToken test = (CasAuthenticationToken) obj;
89              
90              if (!this.assertion.equals(test.getAssertion())) {
91                  return false;
92              }
93  
94              if (this.getKeyHash() != test.getKeyHash()) {
95                  return false;
96              }
97  
98              return true;
99          }
100 
101         return false;
102     }
103 
104     public Object getCredentials() {
105         return this.credentials;
106     }
107 
108     public int getKeyHash() {
109         return this.keyHash;
110     }
111 
112     public Object getPrincipal() {
113         return this.principal;
114     }
115 
116     public Assertion getAssertion() {
117         return this.assertion;
118     }
119 
120     public UserDetails getUserDetails() {
121         return userDetails;
122     }
123 
124     public String toString() {
125         StringBuffer sb = new StringBuffer();
126         sb.append(super.toString());
127         sb.append(" Assertion: ").append(this.assertion);
128         sb.append(" Credentials (Service/Proxy Ticket): ").append(this.credentials);
129 
130         return (sb.toString());
131     }
132 }