Coverage Report - org.acegisecurity.adapters.AbstractAdapterAuthenticationToken
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAdapterAuthenticationToken
53% 
60% 
2
 
 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.adapters;
 17  
 
 18  
 import org.acegisecurity.GrantedAuthority;
 19  
 
 20  
 import org.acegisecurity.providers.AbstractAuthenticationToken;
 21  
 
 22  
 
 23  
 /**
 24  
  * Convenience superclass for {@link AuthByAdapter} implementations.
 25  
  *
 26  
  * @author Ben Alex
 27  
  * @version $Id: AbstractAdapterAuthenticationToken.java 1496 2006-05-23 13:38:33Z benalex $
 28  
  */
 29  
 public abstract class AbstractAdapterAuthenticationToken extends AbstractAuthenticationToken implements AuthByAdapter {
 30  
     //~ Instance fields ================================================================================================
 31  
 
 32  
     private int keyHash;
 33  
 
 34  
     //~ Constructors ===================================================================================================
 35  
 
 36  
     protected AbstractAdapterAuthenticationToken() {
 37  0
         super(null);
 38  0
     }
 39  
 
 40  
 /**
 41  
      * The only way an <code>AbstractAdapterAuthentication</code> should be
 42  
      * constructed.
 43  
      *
 44  
      * @param key the key that is hashed and made available via  {@link
 45  
      *        #getKeyHash()}
 46  
      * @param authorities the authorities granted to this principal
 47  
      */
 48  
     protected AbstractAdapterAuthenticationToken(String key, GrantedAuthority[] authorities) {
 49  10
         super(authorities);
 50  10
         this.keyHash = key.hashCode();
 51  10
     }
 52  
 
 53  
     //~ Methods ========================================================================================================
 54  
 
 55  
     public boolean equals(Object obj) {
 56  9
         if (obj instanceof AbstractAdapterAuthenticationToken) {
 57  8
             if (!super.equals(obj)) {
 58  0
                 return false;
 59  
             }
 60  
 
 61  8
             AbstractAdapterAuthenticationToken test = (AbstractAdapterAuthenticationToken) obj;
 62  
 
 63  8
             return (this.getKeyHash() == test.getKeyHash());
 64  
         }
 65  
 
 66  1
         return false;
 67  
     }
 68  
 
 69  
     public int getKeyHash() {
 70  19
         return this.keyHash;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Always returns <code>true</code>.
 75  
      *
 76  
      * @return DOCUMENT ME!
 77  
      */
 78  
     public boolean isAuthenticated() {
 79  28
         return true;
 80  
     }
 81  
 
 82  
     /**
 83  
      * Iterates the granted authorities and indicates whether or not the specified role is held.<p>Comparison
 84  
      * is based on the <code>String</code> returned by {@link GrantedAuthority#getAuthority}.</p>
 85  
      *
 86  
      * @param role the role being searched for in this object's granted authorities list
 87  
      *
 88  
      * @return <code>true</code> if the granted authority is held, or <code>false</code> otherwise
 89  
      */
 90  
     public boolean isUserInRole(String role) {
 91  0
         GrantedAuthority[] authorities = super.getAuthorities();
 92  
 
 93  0
         for (int i = 0; i < authorities.length; i++) {
 94  0
             if (role.equals(authorities[i].getAuthority())) {
 95  0
                 return true;
 96  
             }
 97  
         }
 98  
 
 99  0
         return false;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Setting is ignored. Always considered authenticated.
 104  
      *
 105  
      * @param ignored DOCUMENT ME!
 106  
      */
 107  
     public void setAuthenticated(boolean ignored) {
 108  
         // ignored
 109  0
     }
 110  
 }