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.anonymous;
17
18 import org.acegisecurity.GrantedAuthority;
19
20 import org.acegisecurity.providers.AbstractAuthenticationToken;
21
22 import java.io.Serializable;
23
24
25 /**
26 * Represents an anonymous <code>Authentication</code>.
27 *
28 * @author Ben Alex
29 * @version $Id: AnonymousAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $
30 */
31 public class AnonymousAuthenticationToken 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 AnonymousAuthenticationToken(String key, Object principal, GrantedAuthority[] authorities) {
50 super(authorities);
51
52 if ((key == null) || ("".equals(key)) || (principal == null) || "".equals(principal) || (authorities == null)
53 || (authorities.length == 0)) {
54 throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
55 }
56
57 this.keyHash = key.hashCode();
58 this.principal = principal;
59 setAuthenticated(true);
60 }
61
62 //~ Methods ========================================================================================================
63
64 public boolean equals(Object obj) {
65 if (!super.equals(obj)) {
66 return false;
67 }
68
69 if (obj instanceof AnonymousAuthenticationToken) {
70 AnonymousAuthenticationToken test = (AnonymousAuthenticationToken) obj;
71
72 if (this.getKeyHash() != test.getKeyHash()) {
73 return false;
74 }
75
76 return true;
77 }
78
79 return false;
80 }
81
82 /**
83 * Always returns an empty <code>String</code>
84 *
85 * @return an empty String
86 */
87 public Object getCredentials() {
88 return "";
89 }
90
91 public int getKeyHash() {
92 return this.keyHash;
93 }
94
95 public Object getPrincipal() {
96 return this.principal;
97 }
98 }