1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tck.security; |
12 | |
|
13 | |
import org.mule.api.lifecycle.InitialisationException; |
14 | |
import org.mule.api.security.Authentication; |
15 | |
import org.mule.api.security.SecurityException; |
16 | |
|
17 | |
import java.util.HashMap; |
18 | |
import java.util.Map; |
19 | |
|
20 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
public class TestMultiuserSecurityProvider extends TestSingleUserSecurityProvider |
26 | |
{ |
27 | |
private Map <String, Authentication> authentications; |
28 | |
|
29 | |
public TestMultiuserSecurityProvider() |
30 | |
{ |
31 | 0 | super("multi-user-test"); |
32 | 0 | } |
33 | |
|
34 | |
protected void doInitialise() throws InitialisationException |
35 | |
{ |
36 | 0 | authentications = new ConcurrentHashMap(); |
37 | 0 | } |
38 | |
|
39 | |
public Authentication authenticate(Authentication authentication) throws SecurityException |
40 | |
{ |
41 | 0 | String user = (String) authentication.getPrincipal(); |
42 | 0 | logger.debug("Authenticating user: " + user); |
43 | |
|
44 | |
|
45 | 0 | Authentication oldAuth = authentications.get(user); |
46 | 0 | if (oldAuth != null) |
47 | |
{ |
48 | 0 | authentication = oldAuth; |
49 | 0 | Map props = authentication.getProperties(); |
50 | 0 | int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS); |
51 | 0 | String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR); |
52 | 0 | props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1); |
53 | 0 | authentication.setProperties(props); |
54 | 0 | authentications.put(user, authentication); |
55 | 0 | logger.info("Welcome back " + user + " (" + numberLogins+1 + " logins), we remembered your favorite color: " + favoriteColor); |
56 | 0 | } |
57 | |
else |
58 | |
{ |
59 | 0 | String favoriteColor = getFavoriteColor(user); |
60 | 0 | logger.info("First login for user: " + user + ", favorite color is " + favoriteColor); |
61 | 0 | Map props = new HashMap(); |
62 | 0 | props.put(PROPERTY_NUMBER_LOGINS, 1); |
63 | 0 | props.put(PROPERTY_FAVORITE_COLOR, favoriteColor); |
64 | 0 | authentication.setProperties(props); |
65 | 0 | authentications.put(user, authentication); |
66 | |
} |
67 | 0 | return authentication; |
68 | |
} |
69 | |
} |