1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.extras.jaas.filters; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.extras.jaas.JaasAuthentication; |
15 | |
import org.mule.impl.security.AbstractEndpointSecurityFilter; |
16 | |
import org.mule.impl.security.MuleCredentials; |
17 | |
import org.mule.impl.security.MuleHeaderCredentialsAccessor; |
18 | |
import org.mule.umo.UMOEvent; |
19 | |
import org.mule.umo.lifecycle.InitialisationException; |
20 | |
import org.mule.umo.security.CredentialsNotSetException; |
21 | |
import org.mule.umo.security.CryptoFailureException; |
22 | |
import org.mule.umo.security.EncryptionStrategyNotFoundException; |
23 | |
import org.mule.umo.security.SecurityException; |
24 | |
import org.mule.umo.security.SecurityProviderNotFoundException; |
25 | |
import org.mule.umo.security.UMOAuthentication; |
26 | |
import org.mule.umo.security.UMOCredentials; |
27 | |
import org.mule.umo.security.UMOSecurityContext; |
28 | |
import org.mule.umo.security.UnauthorisedException; |
29 | |
import org.mule.umo.security.UnknownAuthenticationTypeException; |
30 | |
|
31 | |
public class JaasSecurityFilter extends AbstractEndpointSecurityFilter |
32 | |
{ |
33 | |
|
34 | |
public JaasSecurityFilter() |
35 | 4 | { |
36 | 4 | setCredentialsAccessor(new MuleHeaderCredentialsAccessor()); |
37 | 4 | } |
38 | |
|
39 | |
protected final void authenticateInbound(UMOEvent event) |
40 | |
throws SecurityException, CryptoFailureException, EncryptionStrategyNotFoundException, |
41 | |
UnknownAuthenticationTypeException |
42 | |
{ |
43 | 20 | String userHeader = (String) getCredentialsAccessor().getCredentials(event); |
44 | 20 | if (userHeader == null) |
45 | |
{ |
46 | 0 | throw new CredentialsNotSetException(event.getMessage(), event.getSession().getSecurityContext(), |
47 | |
event.getEndpoint(), this); |
48 | |
} |
49 | |
|
50 | 20 | UMOCredentials user = new MuleCredentials(userHeader); |
51 | |
UMOAuthentication authResult; |
52 | 20 | UMOAuthentication umoAuthentication = new JaasAuthentication(user); |
53 | |
try |
54 | |
{ |
55 | 20 | authResult = getSecurityManager().authenticate(umoAuthentication); |
56 | |
} |
57 | 12 | catch (SecurityException se) |
58 | |
{ |
59 | |
|
60 | 12 | if (logger.isDebugEnabled()) |
61 | |
{ |
62 | 12 | logger.debug("Security Exception raised. Authentication request for user: " + user.getUsername() |
63 | |
+ " failed: " + se.toString()); |
64 | |
} |
65 | 12 | throw se; |
66 | |
} |
67 | 0 | catch (Exception e) |
68 | |
{ |
69 | |
|
70 | 0 | if (logger.isDebugEnabled()) |
71 | |
{ |
72 | 0 | logger.debug("Authentication request for user: " + user.getUsername() |
73 | |
+ " failed: " + e.toString()); |
74 | |
} |
75 | 0 | throw new UnauthorisedException(CoreMessages.authFailedForUser(user.getUsername()), |
76 | |
event.getMessage(), e); |
77 | 8 | } |
78 | |
|
79 | |
|
80 | 8 | if (logger.isDebugEnabled()) |
81 | |
{ |
82 | 8 | logger.debug("Authentication success: " + authResult.toString()); |
83 | |
} |
84 | |
|
85 | 8 | UMOSecurityContext context = getSecurityManager().createSecurityContext(authResult); |
86 | 8 | context.setAuthentication(authResult); |
87 | 8 | event.getSession().setSecurityContext(context); |
88 | 8 | } |
89 | |
|
90 | |
protected void authenticateOutbound(UMOEvent event) |
91 | |
throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException |
92 | |
{ |
93 | 0 | if (event.getSession().getSecurityContext() == null) |
94 | |
{ |
95 | 0 | if (isAuthenticate()) |
96 | |
{ |
97 | 0 | throw new UnauthorisedException(event.getMessage(), event.getSession().getSecurityContext(), |
98 | |
event.getEndpoint(), this); |
99 | |
} |
100 | |
else |
101 | |
{ |
102 | 0 | return; |
103 | |
} |
104 | |
} |
105 | 0 | UMOAuthentication auth = event.getSession().getSecurityContext().getAuthentication(); |
106 | 0 | if (isAuthenticate()) |
107 | |
{ |
108 | 0 | auth = getSecurityManager().authenticate(auth); |
109 | 0 | if (logger.isDebugEnabled()) |
110 | |
{ |
111 | 0 | logger.debug("Authentication success: " + auth.toString()); |
112 | |
} |
113 | |
} |
114 | |
|
115 | 0 | String token = auth.getCredentials().toString(); |
116 | 0 | getCredentialsAccessor().setCredentials(event, token); |
117 | |
|
118 | 0 | } |
119 | |
|
120 | |
protected void doInitialise() throws InitialisationException |
121 | |
{ |
122 | |
|
123 | 4 | } |
124 | |
} |
125 | |
|