Coverage Report - org.acegisecurity.providers.AbstractAuthenticationToken
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAuthenticationToken
87% 
100% 
3.818
 
 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.Authentication;
 19  
 import org.acegisecurity.GrantedAuthority;
 20  
 
 21  
 import org.acegisecurity.userdetails.UserDetails;
 22  
 
 23  
 import org.springframework.util.Assert;
 24  
 
 25  
 
 26  
 /**
 27  
  * Base class for <code>Authentication</code> objects.<p>Implementations which use this class should be immutable.</p>
 28  
  *
 29  
  * @author Ben Alex
 30  
  * @author Luke Taylor
 31  
  * @version $Id: AbstractAuthenticationToken.java 1496 2006-05-23 13:38:33Z benalex $
 32  
  */
 33  
 public abstract class AbstractAuthenticationToken implements Authentication {
 34  
     //~ Instance fields ================================================================================================
 35  
 
 36  
     private Object details;
 37  
     private GrantedAuthority[] authorities;
 38  460
     private boolean authenticated = false;
 39  
 
 40  
     //~ Constructors ===================================================================================================
 41  
 
 42  
 /**
 43  
      * Retained for compatibility with subclasses written before the
 44  
      * <tt>AbstractAuthenticationToken(GrantedAuthority[])</tt> constructor
 45  
      * was introduced.
 46  
      *
 47  
      * @deprecated in favour of the constructor which takes a
 48  
      *             <code>GrantedAuthority[]</code> argument.
 49  
      */
 50  0
     public AbstractAuthenticationToken() {}
 51  
 
 52  
 /**
 53  
      * Creates a token with the supplied array of authorities.
 54  
      *
 55  
      * @param authorities the list of <tt>GrantedAuthority</tt>s for the
 56  
      *        principal represented by this authentication object. A
 57  
      *        <code>null</code> value indicates that no authorities have been
 58  
      *        granted (pursuant to the interface contract specified by {@link
 59  
      *        Authentication#getAuthorities()}<code>null</code> should only be
 60  
      *        presented if the principal has not been authenticated).
 61  
      */
 62  460
     public AbstractAuthenticationToken(GrantedAuthority[] authorities) {
 63  460
         if (authorities != null) {
 64  711
             for (int i = 0; i < authorities.length; i++) {
 65  448
                 Assert.notNull(authorities[i],
 66  
                     "Granted authority element " + i + " is null - GrantedAuthority[] cannot contain any null elements");
 67  
             }
 68  
         }
 69  
 
 70  457
         this.authorities = authorities;
 71  457
     }
 72  
 
 73  
     //~ Methods ========================================================================================================
 74  
 
 75  
     public boolean equals(Object obj) {
 76  40
         if (obj instanceof AbstractAuthenticationToken) {
 77  40
             AbstractAuthenticationToken test = (AbstractAuthenticationToken) obj;
 78  
 
 79  40
             if (!((this.getAuthorities() == null) && (test.getAuthorities() == null))) {
 80  34
                 if ((this.getAuthorities() == null) || (test.getAuthorities() == null)) {
 81  0
                     return false;
 82  
                 }
 83  
 
 84  34
                 if (this.getAuthorities().length != test.getAuthorities().length) {
 85  0
                     return false;
 86  
                 }
 87  
 
 88  93
                 for (int i = 0; i < this.getAuthorities().length; i++) {
 89  59
                     if (!this.getAuthorities()[i].equals(test.getAuthorities()[i])) {
 90  0
                         return false;
 91  
                     }
 92  
                 }
 93  
             }
 94  
 
 95  40
             if ((this.details == null) && (test.getDetails() != null)) {
 96  0
                 return false;
 97  
             }
 98  
 
 99  40
             if ((this.details != null) && (test.getDetails() == null)) {
 100  0
                 return false;
 101  
             }
 102  
 
 103  40
             if ((this.details != null) && (!this.details.equals(test.getDetails()))) {
 104  0
                 return false;
 105  
             }
 106  
 
 107  40
             return (this.getPrincipal().equals(test.getPrincipal())
 108  
             && this.getCredentials().equals(test.getCredentials())
 109  
             && (this.isAuthenticated() == test.isAuthenticated()));
 110  
         }
 111  
 
 112  0
         return false;
 113  
     }
 114  
 
 115  
     public GrantedAuthority[] getAuthorities() {
 116  927
         if (authorities == null) {
 117  27
             return null;
 118  
         }
 119  
 
 120  900
         GrantedAuthority[] copy = new GrantedAuthority[authorities.length];
 121  900
         System.arraycopy(authorities, 0, copy, 0, authorities.length);
 122  
 
 123  900
         return copy;
 124  
     }
 125  
 
 126  
     public Object getDetails() {
 127  141
         return details;
 128  
     }
 129  
 
 130  
     public String getName() {
 131  2699
         if (this.getPrincipal() instanceof UserDetails) {
 132  28
             return ((UserDetails) this.getPrincipal()).getUsername();
 133  
         }
 134  
 
 135  2671
         return (this.getPrincipal() == null) ? "" : this.getPrincipal().toString();
 136  
     }
 137  
 
 138  
     public int hashCode() {
 139  20
         int code = 31;
 140  
 
 141  
         // Copy authorities to local variable for performance (SEC-223)
 142  20
         GrantedAuthority[] authorities = this.getAuthorities();
 143  
 
 144  20
         if (authorities != null) {
 145  34
             for (int i = 0; i < authorities.length; i++) {
 146  18
                 code ^= authorities[i].hashCode();
 147  
             }
 148  
         }
 149  
 
 150  20
         if (this.getPrincipal() != null) {
 151  20
             code ^= this.getPrincipal().hashCode();
 152  
         }
 153  
 
 154  20
         if (this.getCredentials() != null) {
 155  20
             code ^= this.getCredentials().hashCode();
 156  
         }
 157  
 
 158  20
         if (this.getDetails() != null) {
 159  0
             code ^= this.getDetails().hashCode();
 160  
         }
 161  
 
 162  20
         if (this.isAuthenticated()) {
 163  15
             code ^= -37;
 164  
         }
 165  
 
 166  20
         return code;
 167  
     }
 168  
 
 169  
     public boolean isAuthenticated() {
 170  140
         return authenticated;
 171  
     }
 172  
 
 173  
     public void setAuthenticated(boolean authenticated) {
 174  360
         this.authenticated = authenticated;
 175  360
     }
 176  
 
 177  
     public void setDetails(Object details) {
 178  64
         this.details = details;
 179  64
     }
 180  
 
 181  
     public String toString() {
 182  8
         StringBuffer sb = new StringBuffer();
 183  8
         sb.append(super.toString()).append(": ");
 184  8
         sb.append("Username: ").append(this.getPrincipal()).append("; ");
 185  8
         sb.append("Password: [PROTECTED]; ");
 186  8
         sb.append("Authenticated: ").append(this.isAuthenticated()).append("; ");
 187  8
         sb.append("Details: ").append(this.getDetails()).append("; ");
 188  
 
 189  8
         if (this.getAuthorities() != null) {
 190  6
             sb.append("Granted Authorities: ");
 191  
 
 192  14
             for (int i = 0; i < this.getAuthorities().length; i++) {
 193  8
                 if (i > 0) {
 194  3
                     sb.append(", ");
 195  
                 }
 196  
 
 197  8
                 sb.append(this.getAuthorities()[i].toString());
 198  
             }
 199  
         } else {
 200  2
             sb.append("Not granted any authorities");
 201  
         }
 202  
 
 203  8
         return sb.toString();
 204  
     }
 205  
 }