01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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: * An {@link AuthenticationProvider} implementation that can authenticate an {@link AuthByAdapter}.<P>Configured in
35: * the bean context with a key that should match the key used by adapters to generate <code>AuthByAdapter</code>
36: * instances. It treats as valid any such instance presenting a hash code that matches the
37: * <code>AuthByAdapterProvider</code>-configured key.</p>
38: * <P>If the key does not match, a <code>BadCredentialsException</code> is thrown.</p>
39: */
40: public class AuthByAdapterProvider implements InitializingBean,
41: AuthenticationProvider, MessageSourceAware {
42: //~ Instance fields ================================================================================================
43:
44: protected MessageSourceAccessor messages = AcegiMessageSource
45: .getAccessor();
46: private String key;
47:
48: //~ Methods ========================================================================================================
49:
50: public void afterPropertiesSet() throws Exception {
51: Assert
52: .notNull(key,
53: "A Key is required and should match that configured for the adapters");
54: Assert.notNull(messages, "A message source must be set");
55: }
56:
57: public Authentication authenticate(Authentication authentication)
58: throws AuthenticationException {
59: AuthByAdapter token = (AuthByAdapter) authentication;
60:
61: if (token.getKeyHash() == key.hashCode()) {
62: return authentication;
63: } else {
64: throw new BadCredentialsException(
65: messages
66: .getMessage(
67: "AuthByAdapterProvider.incorrectKey",
68: "The presented AuthByAdapter implementation does not contain the expected key"));
69: }
70: }
71:
72: public String getKey() {
73: return key;
74: }
75:
76: public void setKey(String key) {
77: this .key = key;
78: }
79:
80: public void setMessageSource(MessageSource messageSource) {
81: this .messages = new MessageSourceAccessor(messageSource);
82: }
83:
84: public boolean supports(Class authentication) {
85: if (AuthByAdapter.class.isAssignableFrom(authentication)) {
86: return true;
87: } else {
88: return false;
89: }
90: }
91: }
|