View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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   * A security provider which holds authentications for multiple users concurrently.
20   */
21  public class TestMultiuserSecurityProvider extends TestSingleUserSecurityProvider
22  {
23      private Map <String, Authentication> authentications;
24      
25      public TestMultiuserSecurityProvider()
26      {
27          super("multi-user-test");
28      }
29      
30      protected void doInitialise() throws InitialisationException
31      {
32          authentications = new ConcurrentHashMap();
33      }
34      
35      public Authentication authenticate(Authentication authentication) throws SecurityException
36      {
37          String user = (String) authentication.getPrincipal();
38          logger.debug("Authenticating user: " + user);
39          
40          // Check to see if user has already been authenticated
41          Authentication oldAuth = authentications.get(user);
42          if (oldAuth != null)
43          {
44              authentication = oldAuth;
45              Map props = authentication.getProperties();
46              int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS);
47              String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR);
48              props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1);
49              authentication.setProperties(props);
50              authentications.put(user, authentication);
51              logger.info("Welcome back " + user + " (" + numberLogins+1 + " logins), we remembered your favorite color: " + favoriteColor);
52          }
53          else
54          {
55              String favoriteColor = getFavoriteColor(user);
56              logger.info("First login for user: " + user + ", favorite color is " + favoriteColor);
57              Map props = new HashMap();
58              props.put(PROPERTY_NUMBER_LOGINS, 1);
59              props.put(PROPERTY_FAVORITE_COLOR, favoriteColor);
60              authentication.setProperties(props);
61              authentications.put(user, authentication);
62          }
63          return authentication;
64      }
65  }