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