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 super("multi-user-test");
32 }
33
34 protected void doInitialise() throws InitialisationException
35 {
36 authentications = new ConcurrentHashMap();
37 }
38
39 public Authentication authenticate(Authentication authentication) throws SecurityException
40 {
41 String user = (String) authentication.getPrincipal();
42 logger.debug("Authenticating user: " + user);
43
44
45 Authentication oldAuth = authentications.get(user);
46 if (oldAuth != null)
47 {
48 authentication = oldAuth;
49 Map props = authentication.getProperties();
50 int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS);
51 String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR);
52 props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1);
53 authentication.setProperties(props);
54 authentications.put(user, authentication);
55 logger.info("Welcome back " + user + " (" + numberLogins+1 + " logins), we remembered your favorite color: " + favoriteColor);
56 }
57 else
58 {
59 String favoriteColor = getFavoriteColor(user);
60 logger.info("First login for user: " + user + ", favorite color is " + favoriteColor);
61 Map props = new HashMap();
62 props.put(PROPERTY_NUMBER_LOGINS, 1);
63 props.put(PROPERTY_FAVORITE_COLOR, favoriteColor);
64 authentication.setProperties(props);
65 authentications.put(user, authentication);
66 }
67 return authentication;
68 }
69 }