View Javadoc

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