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