View Javadoc

    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.adapters;
    17
    18  import org.acegisecurity.AcegiMessageSource;
    19  import org.acegisecurity.Authentication;
    20  import org.acegisecurity.AuthenticationException;
    21  import org.acegisecurity.BadCredentialsException;
    22
    23  import org.acegisecurity.providers.AuthenticationProvider;
    24
    25  import org.springframework.beans.factory.InitializingBean;
    26
    27  import org.springframework.context.MessageSource;
    28  import org.springframework.context.MessageSourceAware;
    29  import org.springframework.context.support.MessageSourceAccessor;
    30
    31  import org.springframework.util.Assert;
    32
    33
    34  /***
    35   * An {@link AuthenticationProvider} implementation that can authenticate an
    36   * {@link AuthByAdapter}.
    37   *
    38   * <P>
    39   * Configured in the bean context with a key that should match the key used by
    40   * adapters to generate <code>AuthByAdapter</code> instances. It treats as
    41   * valid any such instance presenting a hash code that matches the
    42   * <code>AuthByAdapterProvider</code>-configured key.
    43   * </p>
    44   *
    45   * <P>
    46   * If the key does not match, a <code>BadCredentialsException</code> is thrown.
    47   * </p>
    48   */
    49  public class AuthByAdapterProvider implements InitializingBean,
    50      AuthenticationProvider, MessageSourceAware {
    51      //~ Instance fields ========================================================
    52
    53      protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
    54      private String key;
    55
    56      //~ Methods ================================================================
    57
    58      public void afterPropertiesSet() throws Exception {
    59          Assert.notNull(key,
    60              "A Key is required and should match that configured for the adapters");
    61          Assert.notNull(messages, "A message source must be set");
    62      }
    63
    64      public Authentication authenticate(Authentication authentication)
    65          throws AuthenticationException {
    66          AuthByAdapter token = (AuthByAdapter) authentication;
    67
    68          if (token.getKeyHash() == key.hashCode()) {
    69              return authentication;
    70          } else {
    71              throw new BadCredentialsException(messages.getMessage(
    72                      "AuthByAdapterProvider.incorrectKey",
    73                      "The presented AuthByAdapter implementation does not contain the expected key"));
    74          }
    75      }
    76
    77      public String getKey() {
    78          return key;
    79      }
    80
    81      public void setKey(String key) {
    82          this.key = key;
    83      }
    84
    85      public void setMessageSource(MessageSource messageSource) {
    86          this.messages = new MessageSourceAccessor(messageSource);
    87      }
    88
    89      public boolean supports(Class authentication) {
    90          if (AuthByAdapter.class.isAssignableFrom(authentication)) {
    91              return true;
    92          } else {
    93              return false;
    94          }
    95      }
    96  }