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