View Javadoc

1   /*
2    * $Id: TestSingleUserSecurityProvider.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.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   * A security provider which only authenticates a single user at a time 
25   * (i.e., authentication of a new user overwrites the previous authentication).  
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      protected transient final Log logger = LogFactory.getLog(getClass());
35      
36      public TestSingleUserSecurityProvider()
37      {
38          super("single-user-test");
39      }
40      
41      public TestSingleUserSecurityProvider(String name)
42      {
43          super(name);
44      }
45  
46      public Authentication authenticate(Authentication authenticationRequest) throws SecurityException
47      {
48          String user = (String) authenticationRequest.getPrincipal();
49          logger.debug("Authenticating user: " + user);
50          
51          // Check to see if user has already been authenticated
52          if (authentication != null)
53          {
54              Map props = authentication.getProperties();
55              int numberLogins = (Integer) props.get(PROPERTY_NUMBER_LOGINS);
56              String favoriteColor = (String) props.get(PROPERTY_FAVORITE_COLOR);
57              props.put(PROPERTY_NUMBER_LOGINS, numberLogins + 1);
58              authentication.setProperties(props);
59              logger.info("Welcome back " + user + " (" + numberLogins+1 + " logins), we remembered your favorite color: " + favoriteColor);
60          }
61          else
62          {
63              String favoriteColor = getFavoriteColor(user);
64              logger.info("First login for user: " + user + ", favorite color is " + favoriteColor);
65              Map props = new HashMap();
66              props.put(PROPERTY_NUMBER_LOGINS, 1);
67              props.put(PROPERTY_FAVORITE_COLOR, favoriteColor);
68              authenticationRequest.setProperties(props);
69              authentication = authenticationRequest;
70          }
71          return authentication;
72      }
73      
74      // This info. would be stored in an LDAP or RDBMS
75      String getFavoriteColor(String user)
76      {
77          if (user.equals("marie")) return "bright red";
78          else if (user.equals("stan")) return "metallic blue";
79          else if (user.equals("cindy")) return "dark violet";
80          else return null;
81      }
82  }