| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 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 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
4 |
public class AuthByAdapterProvider implements InitializingBean, AuthenticationProvider, MessageSourceAware { |
| 42 |
|
|
| 43 |
|
|
| 44 |
4 |
protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); |
| 45 |
|
private String key; |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
public void afterPropertiesSet() throws Exception { |
| 50 |
2 |
Assert.notNull(key, "A Key is required and should match that configured for the adapters"); |
| 51 |
1 |
Assert.notNull(messages, "A message source must be set"); |
| 52 |
1 |
} |
| 53 |
|
|
| 54 |
|
public Authentication authenticate(Authentication authentication) |
| 55 |
|
throws AuthenticationException { |
| 56 |
3 |
AuthByAdapter token = (AuthByAdapter) authentication; |
| 57 |
|
|
| 58 |
2 |
if (token.getKeyHash() == key.hashCode()) { |
| 59 |
1 |
return authentication; |
| 60 |
|
} else { |
| 61 |
1 |
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 |
1 |
return key; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
public void setKey(String key) { |
| 71 |
4 |
this.key = key; |
| 72 |
4 |
} |
| 73 |
|
|
| 74 |
|
public void setMessageSource(MessageSource messageSource) { |
| 75 |
0 |
this.messages = new MessageSourceAccessor(messageSource); |
| 76 |
0 |
} |
| 77 |
|
|
| 78 |
|
public boolean supports(Class authentication) { |
| 79 |
3 |
if (AuthByAdapter.class.isAssignableFrom(authentication)) { |
| 80 |
1 |
return true; |
| 81 |
|
} else { |
| 82 |
1 |
return false; |
| 83 |
|
} |
| 84 |
|
} |
| 85 |
|
} |