1
2
3
4
5
6
7
8
9
10
11
12
13
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
48
49 protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor();
50 private String key;
51
52
53
54 public void afterPropertiesSet() throws Exception {
55 Assert.notNull(key,
56 "A Key is required and should match that configured for the RunAsManagerImpl");
57 }
58
59 public Authentication authenticate(Authentication authentication)
60 throws AuthenticationException {
61 RunAsUserToken token = (RunAsUserToken) authentication;
62
63 if (token.getKeyHash() == key.hashCode()) {
64 return authentication;
65 } else {
66 throw new BadCredentialsException(messages.getMessage(
67 "RunAsImplAuthenticationProvider.incorrectKey",
68 "The presented RunAsUserToken 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 (RunAsUserToken.class.isAssignableFrom(authentication)) {
86 return true;
87 } else {
88 return false;
89 }
90 }
91 }