Clover coverage report - Acegi Security System for Spring - 1.0.0-RC1
Coverage timestamp: Mon Dec 5 2005 09:05:15 EST
file stats: LOC: 91   Methods: 6
NCLOC: 47   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RunAsImplAuthenticationProvider.java 100% 90.9% 83.3% 90.5%
coverage coverage
 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.runas;
 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.springframework.beans.factory.InitializingBean;
 24    import org.springframework.context.MessageSource;
 25    import org.springframework.context.MessageSourceAware;
 26    import org.springframework.context.support.MessageSourceAccessor;
 27    import org.springframework.util.Assert;
 28   
 29   
 30    /**
 31    * An {@link AuthenticationProvider} implementation that can authenticate a
 32    * {@link RunAsUserToken}.
 33    *
 34    * <P>
 35    * Configured in the bean context with a key that should match the key used by
 36    * adapters to generate the <code>RunAsUserToken</code>. It treats as valid
 37    * any <code>RunAsUserToken</code> instance presenting a hash code that
 38    * matches the <code>RunAsImplAuthenticationProvider</code>-configured key.
 39    * </p>
 40    *
 41    * <P>
 42    * If the key does not match, a <code>BadCredentialsException</code> is thrown.
 43    * </p>
 44    */
 45    public class RunAsImplAuthenticationProvider implements InitializingBean,
 46    AuthenticationProvider, MessageSourceAware {
 47    //~ Instance fields ========================================================
 48   
 49    protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
 50    private String key;
 51   
 52    //~ Methods ================================================================
 53   
 54  2 public void afterPropertiesSet() throws Exception {
 55  2 Assert.notNull(key,
 56    "A Key is required and should match that configured for the RunAsManagerImpl");
 57    }
 58   
 59  2 public Authentication authenticate(Authentication authentication)
 60    throws AuthenticationException {
 61  2 RunAsUserToken token = (RunAsUserToken) authentication;
 62   
 63  2 if (token.getKeyHash() == key.hashCode()) {
 64  1 return authentication;
 65    } else {
 66  1 throw new BadCredentialsException(messages.getMessage(
 67    "RunAsImplAuthenticationProvider.incorrectKey",
 68    "The presented RunAsUserToken does not contain the expected key"));
 69    }
 70    }
 71   
 72  1 public String getKey() {
 73  1 return key;
 74    }
 75   
 76  3 public void setKey(String key) {
 77  3 this.key = key;
 78    }
 79   
 80  0 public void setMessageSource(MessageSource messageSource) {
 81  0 this.messages = new MessageSourceAccessor(messageSource);
 82    }
 83   
 84  2 public boolean supports(Class authentication) {
 85  2 if (RunAsUserToken.class.isAssignableFrom(authentication)) {
 86  1 return true;
 87    } else {
 88  1 return false;
 89    }
 90    }
 91    }