1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.extras.jaas; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.security.MuleAuthentication; |
15 | |
import org.mule.umo.lifecycle.InitialisationException; |
16 | |
import org.mule.umo.security.UMOAuthentication; |
17 | |
import org.mule.umo.security.UMOSecurityContext; |
18 | |
import org.mule.umo.security.UMOSecurityContextFactory; |
19 | |
import org.mule.umo.security.UMOSecurityProvider; |
20 | |
import org.mule.umo.security.UnauthorisedException; |
21 | |
import org.mule.umo.security.UnknownAuthenticationTypeException; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.security.Security; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import javax.security.auth.login.AppConfigurationEntry; |
29 | |
import javax.security.auth.login.Configuration; |
30 | |
import javax.security.auth.login.LoginContext; |
31 | |
import javax.security.auth.login.LoginException; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | 0 | public class JaasSimpleAuthenticationProvider implements UMOSecurityProvider |
37 | |
{ |
38 | |
|
39 | |
private String loginConfig; |
40 | |
private String loginContextName; |
41 | |
private String credentials; |
42 | |
private String loginModule; |
43 | 0 | private String defaultModule = "org.mule.extras.jaas.loginmodule.DefaultLoginModule"; |
44 | |
private String name; |
45 | |
private UMOSecurityContextFactory factory; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public final void setLoginConfig(String loginConfig) |
56 | |
{ |
57 | 0 | this.loginConfig = loginConfig; |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
public final String getLoginConfig() |
66 | |
{ |
67 | 0 | return loginConfig; |
68 | |
} |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public final void setLoginContextName(String loginContextName) |
76 | |
{ |
77 | 0 | this.loginContextName = loginContextName; |
78 | 0 | } |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public final String getLoginContextName() |
86 | |
{ |
87 | 0 | return loginContextName; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
public final String getCredentials() |
96 | |
{ |
97 | 0 | return credentials; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public final void setCredentials(String credentials) |
106 | |
{ |
107 | 0 | this.credentials = credentials; |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
public final String getLoginModule() |
116 | |
{ |
117 | 0 | return loginModule; |
118 | |
} |
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public final void setLoginModule(String loginModule) |
126 | |
{ |
127 | 0 | this.loginModule = loginModule; |
128 | 0 | } |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public final String getName() |
134 | |
{ |
135 | 0 | return name; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public final void setName(String name) |
142 | |
{ |
143 | 0 | this.name = name; |
144 | 0 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
private void configureJaas() throws IOException |
154 | |
{ |
155 | |
|
156 | 0 | String loginConfigUrl = "file://" |
157 | |
+ org.mule.util.FileUtils.getResourcePath(loginConfig, |
158 | 0 | JaasSimpleAuthenticationProvider.class); |
159 | |
|
160 | 0 | boolean alreadySet = false; |
161 | |
|
162 | 0 | int n = 1; |
163 | 0 | String prefix = "login.config.url."; |
164 | 0 | String existing = null; |
165 | |
|
166 | 0 | while ((existing = Security.getProperty(prefix + n)) != null) |
167 | |
{ |
168 | 0 | alreadySet = existing.equals(loginConfigUrl); |
169 | |
|
170 | 0 | if (alreadySet) |
171 | |
{ |
172 | 0 | break; |
173 | |
} |
174 | 0 | n++; |
175 | |
} |
176 | |
|
177 | 0 | if (!alreadySet) |
178 | |
{ |
179 | 0 | String key = prefix + n; |
180 | 0 | Security.setProperty(key, loginConfigUrl); |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public final UMOAuthentication authenticate(UMOAuthentication authentication) |
194 | |
throws org.mule.umo.security.SecurityException |
195 | |
{ |
196 | |
|
197 | |
LoginContext loginContext; |
198 | 0 | MuleAuthentication auth = (MuleAuthentication)authentication; |
199 | |
|
200 | |
|
201 | 0 | MuleCallbackHandler cbh = new MuleCallbackHandler(auth); |
202 | |
|
203 | |
|
204 | |
try |
205 | |
{ |
206 | 0 | loginContext = new LoginContext(loginContextName, cbh); |
207 | |
} |
208 | 0 | catch (LoginException e) |
209 | |
{ |
210 | 0 | throw new org.mule.umo.security.UnauthorisedException( |
211 | |
CoreMessages.cannotLoadFromClasspath(loginContextName)); |
212 | 0 | } |
213 | |
|
214 | |
|
215 | |
try |
216 | |
{ |
217 | 0 | loginContext.login(); |
218 | |
} |
219 | 0 | catch (LoginException le) |
220 | |
{ |
221 | 0 | throw new UnauthorisedException(CoreMessages.authFailedForUser(auth.getPrincipal())); |
222 | 0 | } |
223 | |
|
224 | 0 | auth.setAuthenticated(true); |
225 | |
|
226 | 0 | return auth; |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public final boolean supports(Class aClass) |
236 | |
{ |
237 | 0 | return UMOAuthentication.class.isAssignableFrom(aClass); |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
public final UMOSecurityContext createSecurityContext(UMOAuthentication auth) |
246 | |
throws UnknownAuthenticationTypeException |
247 | |
{ |
248 | 0 | return factory.create(auth); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
public final void initialise() throws InitialisationException |
261 | |
{ |
262 | |
|
263 | |
|
264 | 0 | if (loginConfig == null) |
265 | |
{ |
266 | |
try |
267 | |
{ |
268 | 0 | AppConfigurationEntry entry = null; |
269 | 0 | JaasConfig.init(); |
270 | |
|
271 | 0 | HashMap options = new HashMap(); |
272 | 0 | options.put("credentials", credentials); |
273 | |
|
274 | |
|
275 | |
|
276 | 0 | if (loginModule != null) |
277 | |
{ |
278 | 0 | entry = new AppConfigurationEntry(loginModule, |
279 | |
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); |
280 | |
} |
281 | |
else |
282 | |
{ |
283 | 0 | entry = new AppConfigurationEntry(defaultModule, |
284 | |
AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options); |
285 | |
} |
286 | |
|
287 | 0 | JaasConfig.addApplicationConfigEntry(loginContextName, entry); |
288 | |
} |
289 | 0 | catch (Exception e) |
290 | |
{ |
291 | 0 | throw new InitialisationException(e, this); |
292 | 0 | } |
293 | |
} |
294 | |
else |
295 | |
{ |
296 | |
|
297 | |
try |
298 | |
{ |
299 | 0 | configureJaas(); |
300 | |
} |
301 | 0 | catch (IOException e) |
302 | |
{ |
303 | 0 | throw new InitialisationException(e, this); |
304 | 0 | } |
305 | |
} |
306 | |
|
307 | |
|
308 | |
try |
309 | |
{ |
310 | 0 | factory = new JaasSecurityContextFactory(); |
311 | |
} |
312 | 0 | catch (Exception e) |
313 | |
{ |
314 | 0 | throw new InitialisationException(CoreMessages.failedToCreate("JaasProvider"), e); |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | 0 | public static class JaasConfig extends Configuration |
323 | |
{ |
324 | |
|
325 | 0 | private static Map appConfigEntries = new HashMap(); |
326 | |
private static JaasConfig jaasConfig; |
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
public static void init() |
332 | |
{ |
333 | 0 | jaasConfig = new JaasConfig(); |
334 | 0 | Configuration.setConfiguration(jaasConfig); |
335 | 0 | } |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
public static JaasConfig getJaasConfig() |
343 | |
{ |
344 | 0 | return jaasConfig; |
345 | |
} |
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
public static void addApplicationConfigEntry(String name, AppConfigurationEntry entry) |
354 | |
{ |
355 | 0 | appConfigEntries.put(name, entry); |
356 | 0 | } |
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
public final AppConfigurationEntry[] getAppConfigurationEntry(String applicationName) |
365 | |
{ |
366 | |
|
367 | 0 | if (applicationName == null) |
368 | |
{ |
369 | 0 | throw new IllegalArgumentException("applicationName passed in was null."); |
370 | |
} |
371 | |
|
372 | 0 | AppConfigurationEntry entry = (AppConfigurationEntry)appConfigEntries.get(applicationName); |
373 | 0 | if (entry == null) |
374 | |
{ |
375 | 0 | return new AppConfigurationEntry[]{}; |
376 | |
} |
377 | |
else |
378 | |
{ |
379 | 0 | AppConfigurationEntry e[] = new AppConfigurationEntry[1]; |
380 | 0 | e[0] = entry; |
381 | 0 | return e; |
382 | |
} |
383 | |
} |
384 | |
|
385 | |
public void refresh() |
386 | |
{ |
387 | |
|
388 | 0 | } |
389 | |
} |
390 | |
} |