Coverage Report - org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
AnonymousAuthenticationProvider
88% 
100% 
1.667
 
 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.anonymous;
 17  
 
 18  
 import org.acegisecurity.AcegiMessageSource;
 19  
 import org.acegisecurity.Authentication;
 20  
 import org.acegisecurity.AuthenticationException;
 21  
 import org.acegisecurity.BadCredentialsException;
 22  
 import org.acegisecurity.providers.AuthenticationProvider;
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 import org.springframework.beans.factory.InitializingBean;
 26  
 import org.springframework.context.MessageSource;
 27  
 import org.springframework.context.MessageSourceAware;
 28  
 import org.springframework.context.support.MessageSourceAccessor;
 29  
 import org.springframework.util.Assert;
 30  
 
 31  
 
 32  
 /**
 33  
  * An {@link AuthenticationProvider} implementation that validates {@link
 34  
  * org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken}s.<p>To be successfully validated, the
 35  
  * {@link org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken#getKeyHash()} must match this class'
 36  
  * {@link #getKey()}.</p>
 37  
  */
 38  6
 public class AnonymousAuthenticationProvider implements AuthenticationProvider, InitializingBean, MessageSourceAware {
 39  
     //~ Static fields/initializers =====================================================================================
 40  
 
 41  3
     private static final Log logger = LogFactory.getLog(AnonymousAuthenticationProvider.class);
 42  
 
 43  
     //~ Instance fields ================================================================================================
 44  
 
 45  6
     protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
 46  
     private String key;
 47  
 
 48  
     //~ Methods ========================================================================================================
 49  
 
 50  
         public void afterPropertiesSet() throws Exception {
 51  2
         Assert.hasLength(key, "A Key is required");
 52  1
         Assert.notNull(this.messages, "A message source must be set");
 53  1
     }
 54  
 
 55  
     public Authentication authenticate(Authentication authentication)
 56  
         throws AuthenticationException {
 57  3
         if (!supports(authentication.getClass())) {
 58  1
             return null;
 59  
         }
 60  
 
 61  2
         if (this.key.hashCode() != ((AnonymousAuthenticationToken) authentication).getKeyHash()) {
 62  1
             throw new BadCredentialsException(messages.getMessage("AnonymousAuthenticationProvider.incorrectKey",
 63  
                     "The presented AnonymousAuthenticationToken does not contain the expected key"));
 64  
         }
 65  
 
 66  1
         return authentication;
 67  
     }
 68  
 
 69  
     public String getKey() {
 70  1
         return key;
 71  
     }
 72  
 
 73  
     public void setKey(String key) {
 74  4
         this.key = key;
 75  4
     }
 76  
 
 77  
     public void setMessageSource(MessageSource messageSource) {
 78  0
         this.messages = new MessageSourceAccessor(messageSource);
 79  0
     }
 80  
 
 81  
     public boolean supports(Class authentication) {
 82  6
         return (AnonymousAuthenticationToken.class.isAssignableFrom(authentication));
 83  
     }
 84  
 }