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.security.Authentication;
10  import org.mule.api.security.SecurityException;
11  import org.mule.security.AbstractSecurityProvider;
12  
13  import java.util.HashMap;
14  import java.util.Map;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /**
20   * A security provider which only authenticates a single user at a time 
21   * (i.e., authentication of a new user overwrites the previous authentication).  
22   */
23  public class TestSingleUserSecurityProvider extends AbstractSecurityProvider
24  {
25      public static final String PROPERTY_FAVORITE_COLOR = "FAVORITE_COLOR";
26      public static final String PROPERTY_NUMBER_LOGINS = "NUMBER_LOGINS";
27      
28      private Authentication authentication;
29      
30      protected transient final Log logger = LogFactory.getLog(getClass());
31      
32      public TestSingleUserSecurityProvider()
33      {
34          super("single-user-test");
35      }
36      
37      public TestSingleUserSecurityProvider(String name)
38      {
39          super(name);
40      }
41  
42      public Authentication authenticate(Authentication authenticationRequest) throws SecurityException
43      {
44          String user = (String) authenticationRequest.getPrincipal();
45          logger.debug("Authenticating user: " + user);
46          
47          // Check to see if user has already been authenticated
48          if (authentication != null)
49          {
50              Map props = authentication.getProperties();
51              int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS);
52              String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR);
53              props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1);
54              authentication.setProperties(props);
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              authenticationRequest.setProperties(props);
65              authentication = authenticationRequest;
66          }
67          return authentication;
68      }
69      
70      // This info. would be stored in an LDAP or RDBMS
71      String getFavoriteColor(String user)
72      {
73          if (user.equals("marie")) return "bright red";
74          else if (user.equals("stan")) return "metallic blue";
75          else if (user.equals("cindy")) return "dark violet";
76          else return null;
77      }
78  }