Coverage Report - org.mule.tck.security.TestSingleUserSecurityProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
TestSingleUserSecurityProvider
0%
0/27
0%
0/8
2.75
 
 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  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  
         // Check to see if user has already been authenticated
 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  
     // This info. would be stored in an LDAP or RDBMS
 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  
 }