View Javadoc

1   /*
2    * $Id: TestMultiuserSecurityProvider.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * A security provider which holds authentications for multiple users concurrently.
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          // Check to see if user has already been authenticated
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  }