1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers;
17
18 import org.acegisecurity.Authentication;
19 import org.acegisecurity.GrantedAuthority;
20
21 import org.acegisecurity.userdetails.UserDetails;
22
23 import org.springframework.util.Assert;
24
25
26
27
28
29
30
31
32
33 public abstract class AbstractAuthenticationToken implements Authentication {
34
35
36 private Object details;
37 private GrantedAuthority[] authorities;
38 private boolean authenticated = false;
39
40
41
42
43
44
45
46
47
48
49
50 public AbstractAuthenticationToken() {}
51
52
53
54
55
56
57
58
59
60
61
62 public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
63 if (authorities != null) {
64 for (int i = 0; i < authorities.length; i++) {
65 Assert.notNull(authorities[i],
66 "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
67 }
68 }
69
70 this.authorities = authorities;
71 }
72
73
74
75 public boolean equals(Object obj) {
76 if (obj instanceof AbstractAuthenticationToken) {
77 AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
78
79 if (!((this.getAuthorities() == null) && (test.getAuthorities() == null))) {
80 if ((this.getAuthorities() == null) || (test.getAuthorities() == null)) {
81 return false;
82 }
83
84 if (this.getAuthorities().length != test.getAuthorities().length) {
85 return false;
86 }
87
88 for (int i = 0; i < this.getAuthorities().length; i++) {
89 if (!this.getAuthorities()[i].equals(test.getAuthorities()[i])) {
90 return false;
91 }
92 }
93 }
94
95 if ((this.details == null) && (test.getDetails() != null)) {
96 return false;
97 }
98
99 if ((this.details != null) && (test.getDetails() == null)) {
100 return false;
101 }
102
103 if ((this.details != null) && (!this.details.equals(test.getDetails()))) {
104 return false;
105 }
106
107 return (this.getPrincipal().equals(test.getPrincipal())
108 && this.getCredentials().equals(test.getCredentials())
109 && (this.isAuthenticated() == test.isAuthenticated()));
110 }
111
112 return false;
113 }
114
115 public GrantedAuthority[] getAuthorities() {
116 if (authorities == null) {
117 return null;
118 }
119
120 GrantedAuthority[] copy = new GrantedAuthority[authorities.length];
121 System.arraycopy(authorities, 0, copy, 0, authorities.length);
122
123 return copy;
124 }
125
126 public Object getDetails() {
127 return details;
128 }
129
130 public String getName() {
131 if (this.getPrincipal() instanceof UserDetails) {
132 return ((UserDetails) this.getPrincipal()).getUsername();
133 }
134
135 return (this.getPrincipal() == null) ? "" : this.getPrincipal().toString();
136 }
137
138 public int hashCode() {
139 int code = 31;
140
141
142 GrantedAuthority[] authorities = this.getAuthorities();
143
144 if (authorities != null) {
145 for (int i = 0; i < authorities.length; i++) {
146 code ^= authorities[i].hashCode();
147 }
148 }
149
150 if (this.getPrincipal() != null) {
151 code ^= this.getPrincipal().hashCode();
152 }
153
154 if (this.getCredentials() != null) {
155 code ^= this.getCredentials().hashCode();
156 }
157
158 if (this.getDetails() != null) {
159 code ^= this.getDetails().hashCode();
160 }
161
162 if (this.isAuthenticated()) {
163 code ^= -37;
164 }
165
166 return code;
167 }
168
169 public boolean isAuthenticated() {
170 return authenticated;
171 }
172
173 public void setAuthenticated(boolean authenticated) {
174 this.authenticated = authenticated;
175 }
176
177 public void setDetails(Object details) {
178 this.details = details;
179 }
180
181 public String toString() {
182 StringBuffer sb = new StringBuffer();
183 sb.append(super.toString()).append(": ");
184 sb.append("Username: ").append(this.getPrincipal()).append("; ");
185 sb.append("Password: [PROTECTED]; ");
186 sb.append("Authenticated: ").append(this.isAuthenticated()).append("; ");
187 sb.append("Details: ").append(this.getDetails()).append("; ");
188
189 if (this.getAuthorities() != null) {
190 sb.append("Granted Authorities: ");
191
192 for (int i = 0; i < this.getAuthorities().length; i++) {
193 if (i > 0) {
194 sb.append(", ");
195 }
196
197 sb.append(this.getAuthorities()[i].toString());
198 }
199 } else {
200 sb.append("Not granted any authorities");
201 }
202
203 return sb.toString();
204 }
205 }