1
2
3
4
5
6
7
8
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
27
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 {
40 this.username = username;
41 this.password = ArrayUtils.clone(password);
42 }
43
44 public MuleCredentials(String username, char[] password, Object roles)
45 {
46 this.username = username;
47 this.password = ArrayUtils.clone(password);
48 this.roles = roles;
49 }
50
51 public MuleCredentials(String header) throws EncryptionStrategyNotFoundException, CryptoFailureException
52 {
53 String scheme = null;
54
55 int i = header.indexOf(' ');
56 if (i == -1)
57 {
58 throw new IllegalArgumentException(
59 CoreMessages.headerMalformedValueIs(MuleProperties.MULE_USER_PROPERTY, header).toString());
60 }
61 else
62 {
63 scheme = header.substring(0, i);
64 }
65
66 String creds = header.substring(i + 1);
67
68 if (!scheme.equalsIgnoreCase("plain"))
69 {
70 UMOSecurityManager sm = MuleManager.getInstance().getSecurityManager();
71
72 UMOEncryptionStrategy es = sm.getEncryptionStrategy(scheme);
73 if (es == null)
74 {
75 throw new EncryptionStrategyNotFoundException(scheme);
76 }
77 else
78 {
79 creds = new String(es.decrypt(creds.getBytes(), null));
80 }
81 }
82
83 StringTokenizer st = new StringTokenizer(creds, TOKEN_DELIM);
84 username = st.nextToken();
85 password = st.nextToken().toCharArray();
86 if (st.hasMoreTokens())
87 {
88 roles = st.nextToken();
89 }
90 }
91
92 public String getToken()
93 {
94 StringBuffer buf = new StringBuffer();
95 buf.append(username).append(TOKEN_DELIM);
96 buf.append(password).append(TOKEN_DELIM);
97
98 if (roles != null)
99 {
100 buf.append(roles);
101 }
102
103 return buf.toString();
104 }
105
106 public String getUsername()
107 {
108 return username;
109 }
110
111 public char[] getPassword()
112 {
113 return ArrayUtils.clone(password);
114 }
115
116 public Object getRoles()
117 {
118 return roles;
119 }
120
121 public static String createHeader(String username, char[] password)
122 {
123 StringBuffer buf = new StringBuffer(32);
124 buf.append("Plain ");
125 buf.append(username).append(TOKEN_DELIM);
126 buf.append(password).append(TOKEN_DELIM);
127 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 StringBuffer buf = new StringBuffer();
136 buf.append(encryptionName).append(" ");
137 String creds = username + TOKEN_DELIM + password;
138 byte[] encrypted = es.encrypt(creds.getBytes(), null);
139 buf.append(new String(encrypted));
140 return buf.toString();
141 }
142 }