Coverage Report - org.mule.impl.security.MuleCredentials
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleCredentials
0%
0/48
0%
0/10
1.778
 
 1  
 /*
 2  
  * $Id: MuleCredentials.java 7963 2007-08-21 08:53:15Z 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.impl.security;
 12  
 
 13  
 import org.mule.MuleManager;
 14  
 import org.mule.config.MuleProperties;
 15  
 import org.mule.config.i18n.CoreMessages;
 16  
 import org.mule.umo.UMOEncryptionStrategy;
 17  
 import org.mule.umo.security.CryptoFailureException;
 18  
 import org.mule.umo.security.EncryptionStrategyNotFoundException;
 19  
 import org.mule.umo.security.UMOCredentials;
 20  
 import org.mule.umo.security.UMOSecurityManager;
 21  
 import org.mule.util.ArrayUtils;
 22  
 
 23  
 import java.util.StringTokenizer;
 24  
 
 25  
 /**
 26  
  * <code>MuleCredentials</code> can be used to read and set Mule user information
 27  
  * that can be stored in a message header.
 28  
  */
 29  
 
 30  
 public class MuleCredentials implements UMOCredentials
 31  
 {
 32  
     public static final String TOKEN_DELIM = "::";
 33  
 
 34  
     private final String username;
 35  
     private final char[] password;
 36  
     private Object roles;
 37  
 
 38  
     public MuleCredentials(String username, char[] password)
 39  0
     {
 40  0
         this.username = username;
 41  0
         this.password = ArrayUtils.clone(password);
 42  0
     }
 43  
 
 44  
     public MuleCredentials(String username, char[] password, Object roles)
 45  0
     {
 46  0
         this.username = username;
 47  0
         this.password = ArrayUtils.clone(password);
 48  0
         this.roles = roles;
 49  0
     }
 50  
 
 51  
     public MuleCredentials(String header) throws EncryptionStrategyNotFoundException, CryptoFailureException
 52  0
     {
 53  0
         String scheme = null;
 54  
 
 55  0
         int i = header.indexOf(' ');
 56  0
         if (i == -1)
 57  
         {
 58  0
             throw new IllegalArgumentException(
 59  
                 CoreMessages.headerMalformedValueIs(MuleProperties.MULE_USER_PROPERTY, header).toString());
 60  
         }
 61  
         else
 62  
         {
 63  0
             scheme = header.substring(0, i);
 64  
         }
 65  
 
 66  0
         String creds = header.substring(i + 1);
 67  
 
 68  0
         if (!scheme.equalsIgnoreCase("plain"))
 69  
         {
 70  0
             UMOSecurityManager sm = MuleManager.getInstance().getSecurityManager();
 71  
 
 72  0
             UMOEncryptionStrategy es = sm.getEncryptionStrategy(scheme);
 73  0
             if (es == null)
 74  
             {
 75  0
                 throw new EncryptionStrategyNotFoundException(scheme);
 76  
             }
 77  
             else
 78  
             {
 79  0
                 creds = new String(es.decrypt(creds.getBytes(), null));
 80  
             }
 81  
         }
 82  
 
 83  0
         StringTokenizer st = new StringTokenizer(creds, TOKEN_DELIM);
 84  0
         username = st.nextToken();
 85  0
         password = st.nextToken().toCharArray();
 86  0
         if (st.hasMoreTokens())
 87  
         {
 88  0
             roles = st.nextToken();
 89  
         }
 90  0
     }
 91  
 
 92  
     public String getToken()
 93  
     {
 94  0
         StringBuffer buf = new StringBuffer();
 95  0
         buf.append(username).append(TOKEN_DELIM);
 96  0
         buf.append(password).append(TOKEN_DELIM);
 97  
 
 98  0
         if (roles != null)
 99  
         {
 100  0
             buf.append(roles);
 101  
         }
 102  
 
 103  0
         return buf.toString();
 104  
     }
 105  
 
 106  
     public String getUsername()
 107  
     {
 108  0
         return username;
 109  
     }
 110  
 
 111  
     public char[] getPassword()
 112  
     {
 113  0
         return ArrayUtils.clone(password);
 114  
     }
 115  
 
 116  
     public Object getRoles()
 117  
     {
 118  0
         return roles;
 119  
     }
 120  
 
 121  
     public static String createHeader(String username, char[] password)
 122  
     {
 123  0
         StringBuffer buf = new StringBuffer(32);
 124  0
         buf.append("Plain ");
 125  0
         buf.append(username).append(TOKEN_DELIM);
 126  0
         buf.append(password).append(TOKEN_DELIM);
 127  0
         return buf.toString();
 128  
     }
 129  
 
 130  
     public static String createHeader(String username,
 131  
                                       String password,
 132  
                                       String encryptionName,
 133  
                                       UMOEncryptionStrategy es) throws CryptoFailureException
 134  
     {
 135  0
         StringBuffer buf = new StringBuffer();
 136  0
         buf.append(encryptionName).append(" ");
 137  0
         String creds = username + TOKEN_DELIM + password;
 138  0
         byte[] encrypted = es.encrypt(creds.getBytes(), null);
 139  0
         buf.append(new String(encrypted));
 140  0
         return buf.toString();
 141  
     }
 142  
 }