/
LoginController.java
551 lines (469 loc) · 20.6 KB
/
LoginController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/**
* Copyright (C) 2006, Intalio Inc.
*
* The program(s) herein may be used and/or copied only with
* the written permission of Intalio Inc. or in accordance with
* the terms and conditions stipulated in the agreement/contract
* under which the program(s) have been supplied.
*
* $Id$
* $Log$
*/
package org.intalio.tempo.web.controller;
import java.io.IOException;
import java.rmi.RemoteException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.intalio.tempo.security.Property;
import org.intalio.tempo.security.authentication.AuthenticationConstants;
import org.intalio.tempo.security.authentication.AuthenticationException;
import org.intalio.tempo.security.rbac.RBACException;
import org.intalio.tempo.security.token.TokenService;
import org.intalio.tempo.security.util.PropertyUtils;
import org.intalio.tempo.security.util.StringArrayUtils;
import org.intalio.tempo.versions.BpmsDescriptorParser;
import org.intalio.tempo.web.ApplicationState;
import org.intalio.tempo.web.Constants;
import org.intalio.tempo.web.User;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
public class LoginController extends UIController {
private static final BpmsDescriptorParser BPMS_DESCRIPTOR_PARSER = new BpmsDescriptorParser();
public static final String AUTO_LOGIN_ID = "autoLogin";
public static final String SINGLE_LOGIN_ID = "singleLogin";
public static final String REDIRECT_AFTER_LOGIN = "redirectAfterLogin";
public static final String SECURE_RANDOM = "SECURE_RANDOM";
public static final String JSESSION = "JSESSIONID";
public static final String UI_FW = "/ui-fw/";
public static final String MONITORING = "/monitoring/";
public static final String BPMS_CONSOLE = "/bpms-console/";
private static final Logger LOG = LogManager.getLogger(LoginController.class);
protected final TokenService _tokenService;
private List<String> _grantedRoles = new ArrayList<String>();
private String _defaultRedirectAfterLogin;
private String _loginPageURL;
private static SecureRandom _random;
static {
try {
_random = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
LOG.error("Cannot initialize secure random number generator", e);
throw new RuntimeException(e);
}
}
public LoginController(TokenService tokenService, String redirectSuccessfulLogin) {
super();
_tokenService = tokenService;
_defaultRedirectAfterLogin = redirectSuccessfulLogin;
}
public String getLoginPageURL() {
return _loginPageURL;
}
public void setLoginPageURL(String url) {
_loginPageURL = url;
}
public static ModelAndView redirect(String url) {
return new ModelAndView(new RedirectView(url));
}
public ModelAndView redirectAfterLogin(HttpServletRequest request, HttpServletResponse response) {
String url = _defaultRedirectAfterLogin;
Cookie cookie = getCookie(request, REDIRECT_AFTER_LOGIN);
if (cookie != null && cookie.getValue() != null) {
url = cookie.getValue();
clearCookie(REDIRECT_AFTER_LOGIN, response);
}
return redirect(url);
}
public static void clearAutoLogin(HttpServletResponse response) {
clearCookie(AUTO_LOGIN_ID, response);
}
public static void clearSingleLogin(HttpServletResponse response) {
clearCookie(SINGLE_LOGIN_ID, response);
}
public static void clearSecureRandom(HttpServletResponse response) {
clearCookie(SECURE_RANDOM, response);
}
public static void clearRootCookie(HttpServletResponse response) {
clearCookie(JSESSION, response);
}
public static String getSecureRandomCookie(HttpServletRequest request) {
Cookie cookie = getCookie(request, SECURE_RANDOM);
if (cookie == null) return null;
return cookie.getValue();
}
public static String getSecureRandomSession(HttpServletRequest request) {
return (String) request.getSession().getAttribute(SECURE_RANDOM);
}
public static void setSecureRandomSession(HttpServletRequest request, String secureRandom) {
request.getSession().setAttribute(SECURE_RANDOM, secureRandom);
}
public static void generateSecureRandom(HttpServletRequest request, HttpServletResponse response) {
String secureRandom = generateSecureRandom();
request.getSession().setAttribute(SECURE_RANDOM, secureRandom);
Cookie cookie = new Cookie(SECURE_RANDOM, secureRandom);
cookie.setMaxAge(60*60*24*365); // one year
cookie.setPath("/");
response.addCookie(cookie);
}
public static String generateSecureRandom() {
StringBuffer str = new StringBuffer();
byte[] buf = new byte[40];
_random.nextBytes(buf);
return bytesToHex(buf);
}
public static String bytesToHex(byte[] bytes) {
final char[] hex = "0123456789ABCDEF".toCharArray();
StringBuffer buf = new StringBuffer();
for (int i=0; i<bytes.length; i++) {
int n = bytes[i]+128;
buf.append(hex[n/16]);
buf.append(hex[n%16]);
}
return buf.toString();
}
public static void clearCookie(String cookieName, HttpServletResponse response) {
Cookie newCookie = new Cookie(cookieName, null);
newCookie.setMaxAge(0);
newCookie.setPath("/");
response.addCookie(newCookie);
}
public static void clearOtherCookie(HttpServletResponse response,String cookieName,String path) {
Cookie newCookie = new Cookie(cookieName, null);
newCookie.setMaxAge(0);
newCookie.setPath(path);
response.addCookie(newCookie);
}
private static String extractProperty(String propName, Property[] props) {
for (Property prop : props) {
if (propName.equals(prop.getName())) {
return (String) prop.getValue();
}
}
return null;
}
public static String extractUser(Property[] props) {
return extractProperty(AuthenticationConstants.PROPERTY_USER, props);
}
public static String[] extractRoles(Property[] props) {
String rolesCommaList = extractProperty(AuthenticationConstants.PROPERTY_ROLES, props);
if (rolesCommaList != null) {
String[] roleStrings = StringArrayUtils.parseCommaDelimited(rolesCommaList);
return roleStrings;
}
return null;
}
public User getCurrentUser(HttpServletRequest request) {
try {
User user = checkSingleLogin(request);
if (user == null) {
user = checkAutoLogin(request);
}
return user;
} catch (Exception except) {
return null;
}
}
protected User checkAutoLogin(HttpServletRequest request) {
return checkToken(request, AUTO_LOGIN_ID);
}
protected User checkSingleLogin(HttpServletRequest request) {
return checkToken(request, SINGLE_LOGIN_ID);
}
protected static Cookie getCookie(HttpServletRequest request, String cookieName) {
Cookie cookie = null;
if (request.getCookies() != null) {
for (Cookie c : request.getCookies()) {
if (cookieName.equals(c.getName())) {
cookie = c;
}
}
}
return cookie;
}
protected User checkToken(HttpServletRequest request, String cookieName) {
Cookie cookie = getCookie(request, cookieName);
User user = null;
if (cookie != null) {
String token = cookie.getValue();
if (token != null && token.trim().length() > 0) {
try {
Property[] props = _tokenService.getTokenProperties(token);
String name = extractUser(props);
String[] roles = extractRoles(props);
Property isWorkFlowAdmin = PropertyUtils.getProperty(props, "isWorkflowAdmin");
user = new User(name, roles, token, Boolean.parseBoolean(isWorkFlowAdmin.getValue().toString()));
} catch (Exception ex) {
LOG.error("Exception while verifying security token: "+ex);
}
}
}
return user;
}
public void setRedirectAfterLoginCookie(HttpServletResponse response) {
setRedirectAfterLoginCookie(response, _defaultRedirectAfterLogin);
}
public static void setRedirectAfterLoginCookie(HttpServletResponse response, String url) {
Cookie cookie = new Cookie(REDIRECT_AFTER_LOGIN, url);
cookie.setMaxAge(60*60); // one hour
cookie.setPath("/");
response.addCookie(cookie);
}
public static void setAutoLoginCookie(HttpServletResponse response, String token) {
Cookie cookie = new Cookie(AUTO_LOGIN_ID, token);
cookie.setMaxAge(60*60*24*365); // one year
cookie.setPath("/");
response.addCookie(cookie);
}
public static void setSingleLoginCookie(HttpServletResponse response, String token) {
Cookie cookie = new Cookie(SINGLE_LOGIN_ID, token);
cookie.setMaxAge(-1); // not persistent, kept until web browser exits
cookie.setPath("/");
response.addCookie(cookie);
}
public List<String> getGrantedRoles() {
return _grantedRoles;
}
public void setGrantedRoles(List<String> roles) {
_grantedRoles = roles;
}
protected User authenticate(String username, String password, String[] grantedRoles) throws SecurityException {
try {
String token = _tokenService.authenticateUser(username, password);
if (token == null) {
throw new IllegalStateException("Empty token returned from token service");
}
return authenticate(token, grantedRoles);
} catch (Exception ex) {
throw new SecurityException(ex);
}
}
protected User authenticate(String token, String[] grantedRoles) throws SecurityException {
try {
Property[] props = _tokenService.getTokenProperties(token);
if (LOG.isDebugEnabled()) {
LOG.debug("Token properties: " + PropertyUtils.toMap(props));
}
String name = extractUser(props);
String[] roles = extractRoles(props);
Property isWorkFlowAdmin = PropertyUtils.getProperty(props, "isWorkflowAdmin");
User user = new User(name, roles, token, Boolean.parseBoolean(isWorkFlowAdmin.getValue().toString()));
if (grantedRoles.length > 0 && !user.hasOneRoleOf(grantedRoles)) {
throw new SecurityException("User does not have one of the following role: "
+ StringArrayUtils.toCommaDelimited(grantedRoles));
}
LOG.debug("User: " + user);
return user;
} catch (AuthenticationException ex) {
throw new SecurityException(ex);
} catch (RemoteException ex) {
throw new SecurityException(ex);
}
}
protected User authenticate(String username, String password, Errors errors) {
try {
LOG.debug("Login user=" + username);
User currentUser = authenticate(username, password, convertRoles(_grantedRoles));
return currentUser;
} catch (SecurityException e) {
LOG.error("Error during login", e);
errors.reject("org_intalio_tempo_web_controller_login_loginError", "The user identifier or password you provided is invalid");
}
return null;
}
public String arrayToString(String[] stringArray, String delimiter) {
StringBuilder builder = new StringBuilder();
int arrayLength = stringArray.length;
for (int index = 0; index < arrayLength; index++) {
builder.append(stringArray[index]);
if (index != (arrayLength - 1)) {
builder.append(delimiter);
}
}
return builder.toString();
}
public void executePost(PostMethod post) {
HttpClient httpclient = new HttpClient();
try {
httpclient.executeMethod(post);
String responseAsString = post.getResponseBodyAsString();
LOG.debug("Response is: " + responseAsString);
} catch (Exception e) {
LOG.warn("Got exception " + e.getMessage() + "while posting request "
+ post.getPath());
} finally {
LOG.debug("Releasing Connection");
post.releaseConnection();
}
}
// @note(alex) Called by reflection - see UIController
@SuppressWarnings("unchecked")
public ModelAndView logIn(HttpServletRequest request, HttpServletResponse response, LoginCommand login,
BindException errors) throws Exception {
// Checking whether logged in
ApplicationState state = getApplicationState(request);
LOG.debug("logIn() state=" + state);
if (state.getCurrentUser() != null) {
return redirectAfterLogin(request, response);
}
if (!errors.hasErrors()) {
User user = authenticate(login.getUsername(), login.getPassword(), errors);
if (user != null) {
state.setCurrentUser(user);
if (login.isAutoLogin()) {
// set autoLogin
setAutoLoginCookie(response, user.getToken());
}
setSingleLoginCookie(response, user.getToken());
String prevAction = state.getPreviousAction();
LOG.debug("logIn() prevAction=" + prevAction);
if (prevAction == null) {
return redirectAfterLogin(request, response);
} else {
state.setPreviousAction(null);
return new ModelAndView(new RedirectView(prevAction));
}
}
}
Map model = errors.getModel();
model.put("login", login);
BPMS_DESCRIPTOR_PARSER.addBpmsBuildVersionsPropertiesToMap(model);
return new ModelAndView(Constants.LOGIN_VIEW, model);
}
// @note(alex) Called by reflection - see UIController
public ModelAndView logOut(HttpServletRequest request, HttpServletResponse response, LoginCommand login,
BindException errors) throws Exception {
ApplicationState state = getApplicationState(request);
if (state != null) {
if (state.getCurrentUser() != null){
String userName = state.getCurrentUser().getName();
LOG.debug("Logout: user=" + userName);
}
state.setCurrentUser(null);
state.setPreviousAction(null);
clearAutoLogin(response);
clearSingleLogin(response);
clearSecureRandom(response);
clearRootCookie(response);
clearOtherCookie(response,JSESSION,UI_FW);
clearOtherCookie(response,JSESSION,MONITORING);
clearOtherCookie(response,JSESSION,BPMS_CONSOLE);
}
Map model = new HashMap();
model.put("login", new LoginCommand());
BPMS_DESCRIPTOR_PARSER.addBpmsBuildVersionsPropertiesToMap(model);
return new ModelAndView(Constants.LOGIN_VIEW, model);
}
@Override
protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors)
throws Exception {
ApplicationState state = getApplicationState(request);
LOG.debug("showForm() state=" + state);
String preAction = request.getParameter("prevAction");
if(preAction != null && !preAction.equals("")) {
state.setPreviousAction(preAction);
}
// Checking whether logged in
User user = state.getCurrentUser();
if (user == null) {
user = checkSingleLogin(request);
if (user != null)
user = authenticate(user.getToken(), convertRoles(_grantedRoles));
}
if (user == null) {
user = checkAutoLogin(request);
if (user != null)
user = authenticate(user.getToken(), convertRoles(_grantedRoles));
}
if (user == null) {
// do login
// handle login from HTTP request
String username = request.getParameter(LoginValidator.USERNAME_PARAM);
String password = request.getParameter(LoginValidator.PASSWORD_PARAM);
// LOG.debug("username: " + username + " password: " + password);
if (username != null && !"".equals(username) && password != null) {
user = authenticate(username, password, errors);
}
}
if (user != null) {
LOG.debug("User authenticated: " + user.getName());
state.setCurrentUser(user);
String prevAction = state.getPreviousAction();
if (prevAction == null) {
LOG.debug("Redirect after succesful login");
return redirectAfterLogin(request, response);
} else {
LOG.debug("Redirect after succesful login: " + prevAction);
state.setPreviousAction(null);
return new ModelAndView(new RedirectView(prevAction));
}
}
LOG.debug("" + errors);
Map model = errors.getModel();
model.put("login", new LoginCommand());
BPMS_DESCRIPTOR_PARSER.addBpmsBuildVersionsPropertiesToMap(model);
return new ModelAndView(Constants.LOGIN_VIEW, model);
}
public static class LoginValidator implements org.springframework.validation.Validator {
private static final String USERNAME_PARAM = "username";
private static final String PASSWORD_PARAM = "password";
/**
* Minimal length of the component value
*/
private static final int USERNAME_MIN_LENGTH = 0;
/**
* Maximum length of the component value
*/
private static final int USERNAME_MAX_LENGTH = 100;
/**
* Minimum text length
*/
private static final int PASSWORD_MIN_LENGTH = 0;
/**
* Maximum text length
*/
private static final int PASSWORD_MAX_LENGTH = 20;
public void validate(Object obj, Errors errors) {
LOG.debug("Validate: " + obj + " " + errors);
if (obj instanceof LoginCommand) {
LoginCommand loginCommand = (LoginCommand) obj;
// validate username
if (StringUtils.isEmpty(loginCommand.getUsername())) {
errors.rejectValue(USERNAME_PARAM, "com_intalio_bpms_console_username_required");
} else if (loginCommand.getUsername().length() < USERNAME_MIN_LENGTH) {
errors.rejectValue(USERNAME_PARAM, "com_intalio_bpms_console_username_less",
new Object[] { USERNAME_MIN_LENGTH }, null);
} else if (loginCommand.getUsername().length() > USERNAME_MAX_LENGTH) {
errors.rejectValue(USERNAME_PARAM, "com_intalio_bpms_console_username_more",
new Object[] { USERNAME_MAX_LENGTH }, null);
}
// validate password
if (StringUtils.isEmpty(loginCommand.getPassword())) {
errors.rejectValue(PASSWORD_PARAM, "com_intalio_bpms_console_password_required");
} else if (loginCommand.getPassword().length() < PASSWORD_MIN_LENGTH) {
errors.rejectValue(PASSWORD_PARAM, "com_intalio_bpms_console_password_less",
new Object[] { PASSWORD_MIN_LENGTH }, null);
} else if (loginCommand.getPassword().length() > PASSWORD_MAX_LENGTH) {
errors.rejectValue(PASSWORD_PARAM, "com_intalio_bpms_console_password_more",
new Object[] { PASSWORD_MAX_LENGTH }, null);
}
}
}
public boolean supports(Class clazz) {
return LoginCommand.class.equals(clazz);
}
}
}