View Javadoc

    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;
    17
    18  import org.acegisecurity.GrantedAuthority;
    19
    20
    21  /***
    22   * An {@link org.acegisecurity.Authentication} implementation that is
    23   * designed for simple presentation of a username and password.
    24   *
    25   * <p>
    26   * The <code>principal</code> and <code>credentials</code> should be set with
    27   * an <code>Object</code> that provides the respective property via its
    28   * <code>Object.toString()</code> method. The simplest such
    29   * <code>Object</code> to use is <code>String</code>.
    30   * </p>
    31   *
    32   * @author Ben Alex
    33   * @version $Id: UsernamePasswordAuthenticationToken.java,v 1.9 2005/11/17 00:55:49 benalex Exp $
    34   */
    35  public class UsernamePasswordAuthenticationToken
    36      extends AbstractAuthenticationToken {
    37      //~ Instance fields ========================================================
    38
    39      private Object credentials;
    40      private Object details = null;
    41      private Object principal;
    42      private GrantedAuthority[] authorities;
    43      private boolean authenticated;
    44
    45      //~ Constructors ===========================================================
    46
    47      /***
    48       * This constructor can be safely used by any code that wishes to create a
    49       * <code>UsernamePasswordAuthenticationToken</code>, as the {@link
    50       * #isAuthenticated()} will return <code>false</code>.
    51       *
    52       * @param principal DOCUMENT ME!
    53       * @param credentials DOCUMENT ME!
    54       */
    55      public UsernamePasswordAuthenticationToken(Object principal,
    56          Object credentials) {
    57          this.principal = principal;
    58          this.credentials = credentials;
    59          this.authenticated = false;
    60      }
    61
    62      /***
    63       * This constructor should only be used by
    64       * <code>AuthenticationManager</code> or
    65       * <code>AuthenticationProvider</code> implementations that are satisfied
    66       * with producing a trusted (ie {@link #isAuthenticated()} =
    67       * <code>true</code>) authentication token.
    68       *
    69       * @param principal
    70       * @param credentials
    71       * @param authorities
    72       */
    73      public UsernamePasswordAuthenticationToken(Object principal,
    74          Object credentials, GrantedAuthority[] authorities) {
    75          this.principal = principal;
    76          this.credentials = credentials;
    77          this.authorities = authorities;
    78          this.authenticated = true;
    79      }
    80
    81      protected UsernamePasswordAuthenticationToken() {
    82          throw new IllegalArgumentException("Cannot use default constructor");
    83      }
    84
    85      //~ Methods ================================================================
    86
    87      public void setAuthenticated(boolean isAuthenticated)
    88          throws IllegalArgumentException {
    89          if (isAuthenticated) {
    90              throw new IllegalArgumentException(
    91                  "Cannot set this token to trusted - use constructor containing GrantedAuthority[]s instead");
    92          }
    93
    94          this.authenticated = isAuthenticated;
    95      }
    96
    97      public boolean isAuthenticated() {
    98          return this.authenticated;
    99      }
    100
    101     public GrantedAuthority[] getAuthorities() {
    102         return this.authorities;
    103     }
    104
    105     public Object getCredentials() {
    106         return this.credentials;
    107     }
    108
    109     public void setDetails(Object details) {
    110         this.details = details;
    111     }
    112
    113     /***
    114      * Usually a {@link org.acegisecurity.ui.WebAuthenticationDetails}.
    115      *
    116      * @return the authentication request details, or <code>null</code>
    117      */
    118     public Object getDetails() {
    119         return details;
    120     }
    121
    122     public Object getPrincipal() {
    123         return this.principal;
    124     }
    125 }