Coverage Report - org.mule.management.support.SimplePasswordJmxAuthenticator
 
Classes in this File Line Coverage Branch Coverage Complexity
SimplePasswordJmxAuthenticator
0%
0/22
0%
0/5
5.5
 
 1  
 /*
 2  
  * $Id: SimplePasswordJmxAuthenticator.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.management.support;
 12  
 
 13  
 import org.mule.management.agents.JmxAgent;
 14  
 import org.mule.util.ObjectUtils;
 15  
 import org.mule.util.StringUtils;
 16  
 
 17  
 import java.util.Collections;
 18  
 import java.util.HashMap;
 19  
 import java.util.HashSet;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 
 23  
 import javax.management.remote.JMXAuthenticator;
 24  
 import javax.management.remote.JMXPrincipal;
 25  
 import javax.security.auth.Subject;
 26  
 
 27  
 import org.apache.commons.logging.Log;
 28  
 import org.apache.commons.logging.LogFactory;
 29  
 
 30  
 /**
 31  
  * A JMX authenticator for a simple username/password scheme.
 32  
  * Passwords are neither encrypted, nor obfuscated.
 33  
  */
 34  0
 public class SimplePasswordJmxAuthenticator implements JMXAuthenticator
 35  
 {
 36  
     /**
 37  
      * Logger used by this class.
 38  
      */
 39  0
     protected static final Log logger = LogFactory.getLog(JmxAgent.class);
 40  
 
 41  
     /**
 42  
      * An in-memory credentials storage.
 43  
      */
 44  0
     private Map credentials = new HashMap();
 45  
 
 46  
     /**
 47  
      * {@inheritDoc}
 48  
      */
 49  
     public Subject authenticate (Object authToken)
 50  
     {
 51  0
         if (authToken == null)
 52  
         {
 53  0
             throw new SecurityException("No authentication token available");
 54  
         }
 55  0
         if (!(authToken instanceof String[]) || ((String[]) authToken).length != 2)
 56  
         {
 57  0
             throw new SecurityException("Unsupported credentials format");
 58  
         }
 59  
 
 60  0
         String[] authentication = (String[]) authToken;
 61  
 
 62  0
         String username = StringUtils.defaultString(authentication[0]);
 63  0
         String password = StringUtils.defaultString(authentication[1]);
 64  
 
 65  0
         if (!credentials.containsKey(username))
 66  
         {
 67  0
             throw new SecurityException("Unauthenticated user: " + username);
 68  
         }
 69  
 
 70  0
         if (!password.equals(ObjectUtils.toString(credentials.get(username))))
 71  
         {
 72  0
             throw new SecurityException("Invalid password");
 73  
         }
 74  
 
 75  0
         Set principals = new HashSet();
 76  0
         principals.add(new JMXPrincipal(username));
 77  0
         return new Subject(true, principals, Collections.EMPTY_SET, Collections.EMPTY_SET);
 78  
     }
 79  
 
 80  
 
 81  
     /**
 82  
      * Setter for property 'credentials'.
 83  
      *
 84  
      * @param newCredentials Value to set for property 'credentials'.
 85  
      */
 86  
     public void setCredentials (final Map newCredentials)
 87  
     {
 88  0
         this.credentials.clear();
 89  0
         if (newCredentials == null || newCredentials.isEmpty())
 90  
         {
 91  0
             logger.warn("Credentials cache has been purged, remote access will no longer be available");
 92  
         }
 93  
         else
 94  
         {
 95  0
             this.credentials.putAll(newCredentials);
 96  
         }
 97  0
     }
 98  
 }