1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.cxf.support; |
8 | |
|
9 | |
import org.mule.RequestContext; |
10 | |
import org.mule.api.security.Authentication; |
11 | |
import org.mule.api.security.SecurityContext; |
12 | |
import org.mule.api.security.SecurityException; |
13 | |
import org.mule.api.security.SecurityProviderNotFoundException; |
14 | |
import org.mule.api.security.UnknownAuthenticationTypeException; |
15 | |
import org.mule.security.DefaultMuleAuthentication; |
16 | |
import org.mule.security.MuleCredentials; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
|
20 | |
import javax.security.auth.callback.Callback; |
21 | |
import javax.security.auth.callback.CallbackHandler; |
22 | |
import javax.security.auth.callback.UnsupportedCallbackException; |
23 | |
|
24 | |
import org.apache.commons.logging.Log; |
25 | |
import org.apache.commons.logging.LogFactory; |
26 | |
import org.apache.ws.security.WSPasswordCallback; |
27 | |
import org.apache.ws.security.WSSecurityException; |
28 | |
|
29 | 0 | public class MuleSecurityManagerCallbackHandler implements CallbackHandler |
30 | |
{ |
31 | 0 | private static Log logger = LogFactory.getLog(MuleSecurityManagerCallbackHandler.class); |
32 | |
|
33 | |
private org.mule.api.security.SecurityManager securityManager; |
34 | |
|
35 | |
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException |
36 | |
{ |
37 | 0 | WSPasswordCallback pc = (WSPasswordCallback) callbacks[0]; |
38 | |
|
39 | 0 | if (pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN |
40 | |
|| pc.getUsage() == WSPasswordCallback.USERNAME_TOKEN_UNKNOWN) |
41 | |
{ |
42 | 0 | DefaultMuleAuthentication auth = new DefaultMuleAuthentication( |
43 | |
new MuleCredentials(pc.getIdentifer(), pc.getPassword().toCharArray())); |
44 | |
|
45 | |
try |
46 | |
{ |
47 | 0 | Authentication authentication = securityManager.authenticate(auth); |
48 | 0 | pc.setPassword(pc.getPassword()); |
49 | |
|
50 | 0 | SecurityContext secContext = null; |
51 | |
try |
52 | |
{ |
53 | 0 | secContext = securityManager.createSecurityContext(authentication); |
54 | 0 | secContext.setAuthentication(authentication); |
55 | |
} |
56 | 0 | catch (UnknownAuthenticationTypeException e) |
57 | |
{ |
58 | 0 | logger.warn("Could not create security context after having successfully authenticated.", e); |
59 | 0 | } |
60 | 0 | RequestContext.getEvent().getSession().setSecurityContext(secContext); |
61 | |
} |
62 | 0 | catch (SecurityException e) |
63 | |
{ |
64 | 0 | throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION, null, null, e); |
65 | |
} |
66 | 0 | catch (SecurityProviderNotFoundException e) |
67 | |
{ |
68 | 0 | throw new WSSecurityException(WSSecurityException.FAILED_AUTHENTICATION, null, null, e); |
69 | 0 | } |
70 | |
} |
71 | 0 | } |
72 | |
|
73 | |
public void setSecurityManager(org.mule.api.security.SecurityManager securityManager) |
74 | |
{ |
75 | 0 | this.securityManager = securityManager; |
76 | 0 | } |
77 | |
|
78 | |
} |
79 | |
|
80 | |
|