Source code: org/acegisecurity/providers/rememberme/RememberMeAuthenticationToken.java
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.rememberme;
17
18 import org.acegisecurity.GrantedAuthority;
19 import org.acegisecurity.providers.AbstractAuthenticationToken;
20
21 import java.io.Serializable;
22
23 import org.springframework.util.Assert;
24
25
26 /**
27 * Represents a remembered <code>Authentication</code>.
28 *
29 * <p>
30 * A remembered <code>Authentication</code> must provide a fully valid
31 * <code>Authentication</code>, including the <code>GrantedAuthority</code>[]s
32 * that apply.
33 * </p>
34 *
35 * @author Ben Alex
36 * @version $Id: RememberMeAuthenticationToken.java,v 1.4 2005/11/17 00:55:52 benalex Exp $
37 */
38 public class RememberMeAuthenticationToken extends AbstractAuthenticationToken
39 implements Serializable {
40 //~ Instance fields ========================================================
41
42 private Object principal;
43 private GrantedAuthority[] authorities;
44 private int keyHash;
45 private boolean authenticated;
46
47 //~ Constructors ===========================================================
48
49 /**
50 * Constructor.
51 *
52 * @param key to identify if this object made by an authorised client
53 * @param principal the principal (typically a <code>UserDetails</code>)
54 * @param authorities the authorities granted to the principal
55 *
56 * @throws IllegalArgumentException if a <code>null</code> was passed
57 */
58 public RememberMeAuthenticationToken(String key, Object principal,
59 GrantedAuthority[] authorities) {
60 if ((key == null) || ("".equals(key)) || (principal == null)
61 || "".equals(principal) || (authorities == null)
62 || (authorities.length == 0)) {
63 throw new IllegalArgumentException(
64 "Cannot pass null or empty values to constructor");
65 }
66
67 for (int i = 0; i < authorities.length; i++) {
68 Assert.notNull(authorities[i], "Granted authority element "
69 + i
70 + " is null - GrantedAuthority[] cannot contain any null elements");
71 }
72
73 this.keyHash = key.hashCode();
74 this.principal = principal;
75 this.authorities = authorities;
76 this.authenticated = true;
77 }
78
79 protected RememberMeAuthenticationToken() {
80 throw new IllegalArgumentException("Cannot use default constructor");
81 }
82
83 //~ Methods ================================================================
84
85 public void setAuthenticated(boolean isAuthenticated) {
86 this.authenticated = isAuthenticated;
87 }
88
89 public boolean isAuthenticated() {
90 return this.authenticated;
91 }
92
93 public GrantedAuthority[] getAuthorities() {
94 return this.authorities;
95 }
96
97 /**
98 * Always returns an empty <code>String</code>
99 *
100 * @return an empty String
101 */
102 public Object getCredentials() {
103 return "";
104 }
105
106 public int getKeyHash() {
107 return this.keyHash;
108 }
109
110 public Object getPrincipal() {
111 return this.principal;
112 }
113
114 public boolean equals(Object obj) {
115 if (!super.equals(obj)) {
116 return false;
117 }
118
119 if (obj instanceof RememberMeAuthenticationToken) {
120 RememberMeAuthenticationToken test = (RememberMeAuthenticationToken) obj;
121
122 if (this.getKeyHash() != test.getKeyHash()) {
123 return false;
124 }
125
126 return true;
127 }
128
129 return false;
130 }
131 }