1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.jaas.loginmodule; |
12 | |
|
13 | |
import org.mule.module.jaas.MuleJaasPrincipal; |
14 | |
|
15 | |
import java.io.IOException; |
16 | |
import java.util.List; |
17 | |
import java.util.Map; |
18 | |
import java.util.Set; |
19 | |
import java.util.Vector; |
20 | |
|
21 | |
import javax.security.auth.Subject; |
22 | |
import javax.security.auth.callback.Callback; |
23 | |
import javax.security.auth.callback.CallbackHandler; |
24 | |
import javax.security.auth.callback.NameCallback; |
25 | |
import javax.security.auth.callback.PasswordCallback; |
26 | |
import javax.security.auth.callback.UnsupportedCallbackException; |
27 | |
import javax.security.auth.login.FailedLoginException; |
28 | |
import javax.security.auth.login.LoginException; |
29 | |
import javax.security.auth.spi.LoginModule; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | public class DefaultLoginModule implements LoginModule |
36 | |
{ |
37 | |
|
38 | |
|
39 | |
private CallbackHandler callbackHandler; |
40 | |
|
41 | |
|
42 | 0 | private boolean succeeded = false; |
43 | 0 | private boolean commitSucceeded = false; |
44 | |
|
45 | |
|
46 | |
private String username; |
47 | |
private String password; |
48 | |
private String credentials; |
49 | |
private List credentialList; |
50 | |
private Subject subject; |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public final boolean abort() throws LoginException |
59 | |
{ |
60 | 0 | if (!succeeded) |
61 | |
{ |
62 | 0 | return false; |
63 | |
} |
64 | 0 | else if (succeeded && !commitSucceeded) |
65 | |
{ |
66 | |
|
67 | 0 | succeeded = false; |
68 | 0 | username = null; |
69 | 0 | if (password != null) |
70 | |
{ |
71 | 0 | password = null; |
72 | |
} |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | |
|
77 | |
|
78 | 0 | logout(); |
79 | |
} |
80 | 0 | return true; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public final boolean commit() throws LoginException |
90 | |
{ |
91 | 0 | if (!succeeded) |
92 | |
{ |
93 | 0 | return false; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | |
|
98 | 0 | if (subject == null) |
99 | |
{ |
100 | 0 | return false; |
101 | |
} |
102 | 0 | MuleJaasPrincipal principal = new MuleJaasPrincipal(username); |
103 | 0 | Set entities = subject.getPrincipals(); |
104 | 0 | if (!entities.contains(principal)) |
105 | |
{ |
106 | 0 | entities.add(principal); |
107 | |
} |
108 | |
|
109 | |
|
110 | 0 | username = null; |
111 | 0 | password = null; |
112 | 0 | commitSucceeded = true; |
113 | 0 | return true; |
114 | |
} |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public final void initialize(Subject subject, |
126 | |
CallbackHandler callbackHandler, |
127 | |
Map sharedState, |
128 | |
Map options) |
129 | |
{ |
130 | 0 | this.subject = subject; |
131 | 0 | this.callbackHandler = callbackHandler; |
132 | |
|
133 | 0 | this.credentials = (String) options.get("credentials"); |
134 | 0 | this.credentialList = getCredentialList(this.credentials); |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public final boolean login() throws LoginException |
145 | |
{ |
146 | 0 | if (callbackHandler == null) |
147 | |
{ |
148 | 0 | throw new LoginException("Error: no CallbackHandler available " |
149 | |
+ "to garner authentication information from the user"); |
150 | |
} |
151 | |
|
152 | 0 | if (callbackHandler == null) |
153 | |
{ |
154 | 0 | throw new LoginException("no handler"); |
155 | |
} |
156 | |
|
157 | 0 | NameCallback nameCb = new NameCallback("user: "); |
158 | 0 | PasswordCallback passCb = new PasswordCallback("password: ", true); |
159 | |
|
160 | |
|
161 | 0 | Callback[] callbacks = new Callback[]{nameCb, passCb}; |
162 | |
|
163 | |
|
164 | |
try |
165 | |
{ |
166 | 0 | callbackHandler.handle(callbacks); |
167 | |
} |
168 | 0 | catch (IOException e) |
169 | |
{ |
170 | 0 | throw new LoginException(e.toString()); |
171 | |
} |
172 | 0 | catch (UnsupportedCallbackException e) |
173 | |
{ |
174 | 0 | throw new LoginException("Error: " + e.getCallback().toString() |
175 | |
+ " not available to garner authentication information " |
176 | |
+ "from the user"); |
177 | 0 | } |
178 | |
|
179 | 0 | username = nameCb.getName(); |
180 | 0 | password = new String(passCb.getPassword()); |
181 | |
|
182 | 0 | boolean usernameCorrect = false; |
183 | 0 | boolean passwordCorrect = false; |
184 | 0 | succeeded = false; |
185 | |
|
186 | |
|
187 | 0 | for (int i = 0; i < credentialList.size(); i = i + 2) |
188 | |
{ |
189 | 0 | if (username.equals(credentialList.get(i).toString())) |
190 | |
{ |
191 | 0 | usernameCorrect = true; |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 0 | usernameCorrect = false; |
196 | |
} |
197 | |
|
198 | 0 | if (password.equals(credentialList.get(i + 1).toString())) |
199 | |
{ |
200 | 0 | passwordCorrect = true; |
201 | |
} |
202 | |
else |
203 | |
{ |
204 | 0 | passwordCorrect = false; |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | 0 | if ((usernameCorrect) & (passwordCorrect)) |
210 | |
{ |
211 | 0 | succeeded = true; |
212 | |
} |
213 | |
} |
214 | |
|
215 | 0 | if (succeeded) |
216 | |
{ |
217 | 0 | return true; |
218 | |
} |
219 | |
else |
220 | |
{ |
221 | 0 | succeeded = false; |
222 | 0 | username = null; |
223 | 0 | password = null; |
224 | 0 | if (!usernameCorrect) |
225 | |
{ |
226 | 0 | throw new FailedLoginException("User Name Incorrect"); |
227 | |
} |
228 | |
else |
229 | |
{ |
230 | 0 | throw new FailedLoginException("Password Incorrect"); |
231 | |
} |
232 | |
} |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
public final boolean logout() |
241 | |
{ |
242 | 0 | return succeeded; |
243 | |
} |
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
public final List getCredentialList(String credentials) |
254 | |
{ |
255 | 0 | boolean semicolonIsFound = false; |
256 | 0 | boolean dividerIsFound = false; |
257 | 0 | char[] credentialArray = credentials.toCharArray(); |
258 | 0 | String username = ""; |
259 | 0 | String password = ""; |
260 | 0 | List outputList = new Vector(); |
261 | |
|
262 | 0 | for (int i = 0; i < credentials.length(); i++) |
263 | |
{ |
264 | 0 | if ((credentialArray[i] != ':') && (!dividerIsFound)) |
265 | |
{ |
266 | 0 | username = username + credentialArray[i]; |
267 | |
} |
268 | 0 | else if ((credentialArray[i] == ':') && (!dividerIsFound)) |
269 | |
{ |
270 | 0 | dividerIsFound = true; |
271 | |
} |
272 | 0 | else if ((credentialArray[i] != ';') && (!semicolonIsFound) && (dividerIsFound)) |
273 | |
{ |
274 | 0 | password = password + credentialArray[i]; |
275 | |
} |
276 | 0 | else if ((credentialArray[i] != ';') && (!semicolonIsFound) && (dividerIsFound)) |
277 | |
{ |
278 | 0 | password = password + credentialArray[i]; |
279 | |
} |
280 | 0 | else if ((credentialArray[i] == ';') && (!semicolonIsFound) && (dividerIsFound)) |
281 | |
{ |
282 | 0 | outputList.add(username); |
283 | 0 | outputList.add(password); |
284 | 0 | semicolonIsFound = false; |
285 | 0 | dividerIsFound = false; |
286 | 0 | username = ""; |
287 | 0 | password = ""; |
288 | |
} |
289 | |
} |
290 | 0 | return outputList; |
291 | |
} |
292 | |
} |