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