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