Coverage Report - org.mule.security.MuleCredentials
 
Classes in this File Line Coverage Branch Coverage Complexity
MuleCredentials
0%
0/46
0%
0/10
1.778
 
 1  
 /*
 2  
  * $Id: MuleCredentials.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.security;
 12  
 
 13  
 import org.mule.api.EncryptionStrategy;
 14  
 import org.mule.api.config.MuleProperties;
 15  
 import org.mule.api.security.Credentials;
 16  
 import org.mule.api.security.CryptoFailureException;
 17  
 import org.mule.api.security.EncryptionStrategyNotFoundException;
 18  
 import org.mule.api.security.SecurityManager;
 19  
 import org.mule.config.i18n.CoreMessages;
 20  
 import org.mule.util.ArrayUtils;
 21  
 
 22  
 import java.io.Serializable;
 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 Credentials, Serializable
 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, SecurityManager sm) throws EncryptionStrategyNotFoundException, CryptoFailureException
 52  0
     {
 53  
 
 54  0
         int i = header.indexOf(' ');
 55  0
         if (i == -1)
 56  
         {
 57  0
             throw new IllegalArgumentException(
 58  
                 CoreMessages.headerMalformedValueIs(MuleProperties.MULE_USER_PROPERTY, header).toString());
 59  
         }
 60  
 
 61  0
         String scheme = header.substring(0, i);
 62  0
         String creds = header.substring(i + 1);
 63  
 
 64  0
         if (!scheme.equalsIgnoreCase("plain"))
 65  
         {
 66  0
             EncryptionStrategy es = sm.getEncryptionStrategy(scheme);
 67  0
             if (es == null)
 68  
             {
 69  0
                 throw new EncryptionStrategyNotFoundException(scheme);
 70  
             }
 71  
             else
 72  
             {
 73  0
                 creds = new String(es.decrypt(creds.getBytes(), null));
 74  
             }
 75  
         }
 76  
 
 77  0
         StringTokenizer st = new StringTokenizer(creds, TOKEN_DELIM);
 78  0
         username = st.nextToken();
 79  0
         password = st.nextToken().toCharArray();
 80  0
         if (st.hasMoreTokens())
 81  
         {
 82  0
             roles = st.nextToken();
 83  
         }
 84  0
     }
 85  
 
 86  
     public String getToken()
 87  
     {
 88  0
         StringBuffer buf = new StringBuffer();
 89  0
         buf.append(username).append(TOKEN_DELIM);
 90  0
         buf.append(password).append(TOKEN_DELIM);
 91  
 
 92  0
         if (roles != null)
 93  
         {
 94  0
             buf.append(roles);
 95  
         }
 96  
 
 97  0
         return buf.toString();
 98  
     }
 99  
 
 100  
     public String getUsername()
 101  
     {
 102  0
         return username;
 103  
     }
 104  
 
 105  
     public char[] getPassword()
 106  
     {
 107  0
         return ArrayUtils.clone(password);
 108  
     }
 109  
 
 110  
     public Object getRoles()
 111  
     {
 112  0
         return roles;
 113  
     }
 114  
 
 115  
     public static String createHeader(String username, char[] password)
 116  
     {
 117  0
         StringBuffer buf = new StringBuffer(32);
 118  0
         buf.append("Plain ");
 119  0
         buf.append(username).append(TOKEN_DELIM);
 120  0
         buf.append(password).append(TOKEN_DELIM);
 121  0
         return buf.toString();
 122  
     }
 123  
 
 124  
     public static String createHeader(String username,
 125  
                                       String password,
 126  
                                       String encryptionName,
 127  
                                       EncryptionStrategy es) throws CryptoFailureException
 128  
     {
 129  0
         StringBuffer buf = new StringBuffer();
 130  0
         buf.append(encryptionName).append(" ");
 131  0
         String creds = username + TOKEN_DELIM + password;
 132  0
         byte[] encrypted = es.encrypt(creds.getBytes(), null);
 133  0
         buf.append(new String(encrypted));
 134  0
         return buf.toString();
 135  
     }
 136  
 }