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.runas;
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 a {@link RunAsUserToken}.<P>Configured in
35: * the bean context with a key that should match the key used by adapters to generate the <code>RunAsUserToken</code>.
36: * It treats as valid any <code>RunAsUserToken</code> instance presenting a hash code that matches the
37: * <code>RunAsImplAuthenticationProvider</code>-configured key.</p>
38: * <P>If the key does not match, a <code>BadCredentialsException</code> is thrown.</p>
39: */
40: public class RunAsImplAuthenticationProvider implements
41: InitializingBean, 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 RunAsManagerImpl");
54: }
55:
56: public Authentication authenticate(Authentication authentication)
57: throws AuthenticationException {
58: RunAsUserToken token = (RunAsUserToken) authentication;
59:
60: if (token.getKeyHash() == key.hashCode()) {
61: return authentication;
62: } else {
63: throw new BadCredentialsException(
64: messages
65: .getMessage(
66: "RunAsImplAuthenticationProvider.incorrectKey",
67: "The presented RunAsUserToken does not contain the expected key"));
68: }
69: }
70:
71: public String getKey() {
72: return key;
73: }
74:
75: public void setKey(String key) {
76: this .key = key;
77: }
78:
79: public void setMessageSource(MessageSource messageSource) {
80: this .messages = new MessageSourceAccessor(messageSource);
81: }
82:
83: public boolean supports(Class authentication) {
84: if (RunAsUserToken.class.isAssignableFrom(authentication)) {
85: return true;
86: } else {
87: return false;
88: }
89: }
90: }
|