1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.tck.security; |
12 | |
|
13 | |
import org.mule.api.security.Authentication; |
14 | |
import org.mule.api.security.SecurityException; |
15 | |
import org.mule.security.AbstractSecurityProvider; |
16 | |
|
17 | |
import java.util.HashMap; |
18 | |
import java.util.Map; |
19 | |
|
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public class TestSingleUserSecurityProvider extends AbstractSecurityProvider |
28 | |
{ |
29 | |
public static final String PROPERTY_FAVORITE_COLOR = "FAVORITE_COLOR"; |
30 | |
public static final String PROPERTY_NUMBER_LOGINS = "NUMBER_LOGINS"; |
31 | |
|
32 | |
private Authentication authentication; |
33 | |
|
34 | 0 | protected transient final Log logger = LogFactory.getLog(getClass()); |
35 | |
|
36 | |
public TestSingleUserSecurityProvider() |
37 | |
{ |
38 | 0 | super("single-user-test"); |
39 | 0 | } |
40 | |
|
41 | |
public TestSingleUserSecurityProvider(String name) |
42 | |
{ |
43 | 0 | super(name); |
44 | 0 | } |
45 | |
|
46 | |
public Authentication authenticate(Authentication authenticationRequest) throws SecurityException |
47 | |
{ |
48 | 0 | String user = (String) authenticationRequest.getPrincipal(); |
49 | 0 | logger.debug("Authenticating user: " + user); |
50 | |
|
51 | |
|
52 | 0 | if (authentication != null) |
53 | |
{ |
54 | 0 | Map props = authentication.getProperties(); |
55 | 0 | int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS); |
56 | 0 | String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR); |
57 | 0 | props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1); |
58 | 0 | authentication.setProperties(props); |
59 | 0 | logger.info("Welcome back " + user + " (" + numberLogins+1 + " logins), we remembered your favorite color: " + favoriteColor); |
60 | 0 | } |
61 | |
else |
62 | |
{ |
63 | 0 | String favoriteColor = getFavoriteColor(user); |
64 | 0 | logger.info("First login for user: " + user + ", favorite color is " + favoriteColor); |
65 | 0 | Map props = new HashMap(); |
66 | 0 | props.put(PROPERTY_NUMBER_LOGINS, 1); |
67 | 0 | props.put(PROPERTY_FAVORITE_COLOR, favoriteColor); |
68 | 0 | authenticationRequest.setProperties(props); |
69 | 0 | authentication = authenticationRequest; |
70 | |
} |
71 | 0 | return authentication; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
String getFavoriteColor(String user) |
76 | |
{ |
77 | 0 | if (user.equals("marie")) return "bright red"; |
78 | 0 | else if (user.equals("stan")) return "metallic blue"; |
79 | 0 | else if (user.equals("cindy")) return "dark violet"; |
80 | 0 | else return null; |
81 | |
} |
82 | |
} |