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.springframework.security.adapters; 17 18 import org.springframework.security.GrantedAuthority; 19 20 import org.springframework.security.providers.AbstractAuthenticationToken; 21 22 23 /** 24 * Convenience superclass for {@link AuthByAdapter} implementations. 25 * 26 * @author Ben Alex 27 * @version $Id: AbstractAdapterAuthenticationToken.java 2217 2007-10-27 00:45:30Z luke_t $ 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 super(null); 38 } 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 super(authorities); 50 this.keyHash = key.hashCode(); 51 } 52 53 //~ Methods ======================================================================================================== 54 55 public boolean equals(Object obj) { 56 if (obj instanceof AbstractAdapterAuthenticationToken) { 57 if (!super.equals(obj)) { 58 return false; 59 } 60 61 AbstractAdapterAuthenticationToken test = (AbstractAdapterAuthenticationToken) obj; 62 63 return (this.getKeyHash() == test.getKeyHash()); 64 } 65 66 return false; 67 } 68 69 public int getKeyHash() { 70 return this.keyHash; 71 } 72 73 /** 74 * Always returns <code>true</code>. 75 * 76 * @return DOCUMENT ME! 77 */ 78 public boolean isAuthenticated() { 79 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 GrantedAuthority[] authorities = super.getAuthorities(); 92 93 for (int i = 0; i < authorities.length; i++) { 94 if (role.equals(authorities[i].getAuthority())) { 95 return true; 96 } 97 } 98 99 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 } 110 }