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