01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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;
17:
18: import org.acegisecurity.GrantedAuthority;
19:
20: /**
21: * An {@link org.acegisecurity.Authentication} implementation that is designed for simple presentation of a
22: * username and password.<p>The <code>principal</code> and <code>credentials</code> should be set with an
23: * <code>Object</code> that provides the respective property via its <code>Object.toString()</code> method. The
24: * simplest such <code>Object</code> to use is <code>String</code>.</p>
25: *
26: * @author Ben Alex
27: * @version $Id: UsernamePasswordAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $
28: */
29: public class UsernamePasswordAuthenticationToken extends
30: AbstractAuthenticationToken {
31: //~ Instance fields ================================================================================================
32:
33: private static final long serialVersionUID = 1L;
34: private Object credentials;
35: private Object principal;
36:
37: //~ Constructors ===================================================================================================
38:
39: /**
40: * This constructor can be safely used by any code that wishes to create a
41: * <code>UsernamePasswordAuthenticationToken</code>, as the {@link
42: * #isAuthenticated()} will return <code>false</code>.
43: *
44: * @param principal DOCUMENT ME!
45: * @param credentials DOCUMENT ME!
46: */
47: public UsernamePasswordAuthenticationToken(Object principal,
48: Object credentials) {
49: super (null);
50: this .principal = principal;
51: this .credentials = credentials;
52: setAuthenticated(false);
53: }
54:
55: /**
56: * This constructor should only be used by
57: * <code>AuthenticationManager</code> or
58: * <code>AuthenticationProvider</code> implementations that are satisfied
59: * with producing a trusted (ie {@link #isAuthenticated()} =
60: * <code>true</code>) authentication token.
61: *
62: * @param principal
63: * @param credentials
64: * @param authorities
65: */
66: public UsernamePasswordAuthenticationToken(Object principal,
67: Object credentials, GrantedAuthority[] authorities) {
68: super (authorities);
69: this .principal = principal;
70: this .credentials = credentials;
71: super .setAuthenticated(true); // must use super, as we override
72: }
73:
74: //~ Methods ========================================================================================================
75:
76: public Object getCredentials() {
77: return this .credentials;
78: }
79:
80: public Object getPrincipal() {
81: return this .principal;
82: }
83:
84: public void setAuthenticated(boolean isAuthenticated)
85: throws IllegalArgumentException {
86: if (isAuthenticated) {
87: throw new IllegalArgumentException(
88: "Cannot set this token to trusted - use constructor containing GrantedAuthority[]s instead");
89: }
90:
91: super .setAuthenticated(false);
92: }
93: }
|