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