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.acegisecurity.providers.rememberme;
17
18 import java.io.Serializable;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.providers.AbstractAuthenticationToken;
22
23
24 /**
25 * Represents a remembered <code>Authentication</code>.<p>A remembered <code>Authentication</code> must provide a
26 * fully valid <code>Authentication</code>, including the <code>GrantedAuthority</code>[]s that apply.</p>
27 *
28 * @author Ben Alex
29 * @version $Id: RememberMeAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $
30 */
31 public class RememberMeAuthenticationToken extends AbstractAuthenticationToken implements Serializable {
32 //~ Instance fields ================================================================================================
33
34 private static final long serialVersionUID = 1L;
35 private Object principal;
36 private int keyHash;
37
38 //~ Constructors ===================================================================================================
39
40 /**
41 * Constructor.
42 *
43 * @param key to identify if this object made by an authorised client
44 * @param principal the principal (typically a <code>UserDetails</code>)
45 * @param authorities the authorities granted to the principal
46 *
47 * @throws IllegalArgumentException if a <code>null</code> was passed
48 */
49 public RememberMeAuthenticationToken(String key, Object principal, GrantedAuthority[] authorities) {
50 super(authorities);
51
52 if ((key == null) || ("".equals(key)) || (principal == null) || "".equals(principal)) {
53 throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
54 }
55
56 this.keyHash = key.hashCode();
57 this.principal = principal;
58 setAuthenticated(true);
59 }
60
61 //~ Methods ========================================================================================================
62
63 public boolean equals(Object obj) {
64 if (!super.equals(obj)) {
65 return false;
66 }
67
68 if (obj instanceof RememberMeAuthenticationToken) {
69 RememberMeAuthenticationToken test = (RememberMeAuthenticationToken) obj;
70
71 if (this.getKeyHash() != test.getKeyHash()) {
72 return false;
73 }
74
75 return true;
76 }
77
78 return false;
79 }
80
81 /**
82 * Always returns an empty <code>String</code>
83 *
84 * @return an empty String
85 */
86 public Object getCredentials() {
87 return "";
88 }
89
90 public int getKeyHash() {
91 return this.keyHash;
92 }
93
94 public Object getPrincipal() {
95 return this.principal;
96 }
97 }