| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| UsernamePasswordAuthenticationToken |
|
| 1.4;1.4 |
| 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; |
|
| 17 | ||
| 18 | import org.acegisecurity.GrantedAuthority; |
|
| 19 | ||
| 20 | ||
| 21 | /** |
|
| 22 | * An {@link org.acegisecurity.Authentication} implementation that is designed for simple presentation of a |
|
| 23 | * username and password.<p>The <code>principal</code> and <code>credentials</code> should be set with an |
|
| 24 | * <code>Object</code> that provides the respective property via its <code>Object.toString()</code> method. The |
|
| 25 | * simplest such <code>Object</code> to use is <code>String</code>.</p> |
|
| 26 | * |
|
| 27 | * @author Ben Alex |
|
| 28 | * @version $Id: UsernamePasswordAuthenticationToken.java 1784 2007-02-24 21:00:24Z luke_t $ |
|
| 29 | */ |
|
| 30 | public class UsernamePasswordAuthenticationToken extends 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, Object credentials) { |
|
| 48 | 179 | super(null); |
| 49 | 179 | this.principal = principal; |
| 50 | 179 | this.credentials = credentials; |
| 51 | 179 | setAuthenticated(false); |
| 52 | 179 | } |
| 53 | ||
| 54 | /** |
|
| 55 | * This constructor should only be used by |
|
| 56 | * <code>AuthenticationManager</code> or |
|
| 57 | * <code>AuthenticationProvider</code> implementations that are satisfied |
|
| 58 | * with producing a trusted (ie {@link #isAuthenticated()} = |
|
| 59 | * <code>true</code>) authentication token. |
|
| 60 | * |
|
| 61 | * @param principal |
|
| 62 | * @param credentials |
|
| 63 | * @param authorities |
|
| 64 | */ |
|
| 65 | public UsernamePasswordAuthenticationToken(Object principal, Object credentials, GrantedAuthority[] authorities) { |
|
| 66 | 104 | super(authorities); |
| 67 | 104 | this.principal = principal; |
| 68 | 104 | this.credentials = credentials; |
| 69 | 104 | super.setAuthenticated(true); // must use super, as we override |
| 70 | 104 | } |
| 71 | ||
| 72 | //~ Methods ======================================================================================================== |
|
| 73 | ||
| 74 | public Object getCredentials() { |
|
| 75 | 155 | return this.credentials; |
| 76 | } |
|
| 77 | ||
| 78 | public Object getPrincipal() { |
|
| 79 | 8428 | return this.principal; |
| 80 | } |
|
| 81 | ||
| 82 | public void setAuthenticated(boolean isAuthenticated) |
|
| 83 | throws IllegalArgumentException { |
|
| 84 | 184 | if (isAuthenticated) { |
| 85 | 1 | throw new IllegalArgumentException( |
| 86 | "Cannot set this token to trusted - use constructor containing GrantedAuthority[]s instead"); |
|
| 87 | } |
|
| 88 | ||
| 89 | 183 | super.setAuthenticated(false); |
| 90 | 183 | } |
| 91 | } |