01: package com.technoetic.xplanner.security;
02:
03: import javax.servlet.http.HttpServletRequest;
04:
05: import org.apache.log4j.Logger;
06:
07: import com.technoetic.xplanner.util.LogUtil;
08:
09: public class AuthenticatorImpl implements Authenticator {
10: private static Logger LOG = LogUtil.getLogger();
11: public static final String LOGIN_CONTEXT_SESSION_KEY = "LOGIN_CONTEXT";
12: static final String GUESTS_KEY = "xplanner.security.guests";
13:
14: private LoginContext loginContext;
15: public int NO_PARENT = 0;
16:
17: public AuthenticatorImpl(LoginContext loginContext) {
18: this .loginContext = loginContext;
19: }
20:
21: public AuthenticatorImpl() {
22: }
23:
24: public void authenticate(HttpServletRequest request, String userId,
25: String password) throws AuthenticationException {
26: LoginContext loginContext = getLoginContext();
27: if (SecurityHelper.isUserAuthenticated(request)) {
28: loginContext.logout(request);
29: }
30: loginContext.authenticate(userId, password);
31: SecurityHelper.setSubject(request, loginContext.getSubject());
32:
33: setLoginContext(request, loginContext);
34: }
35:
36: public LoginContext getLoginContext() {
37: return loginContext;
38: }
39:
40: public static LoginContext getLoginContext(
41: HttpServletRequest request) {
42: return (LoginContext) request.getSession().getAttribute(
43: LOGIN_CONTEXT_SESSION_KEY);
44: }
45:
46: public static void setLoginContext(HttpServletRequest request,
47: LoginContext context) {
48: request.getSession().setAttribute(LOGIN_CONTEXT_SESSION_KEY,
49: context);
50: }
51:
52: public static LoginModule getLoginModule(HttpServletRequest request) {
53: LoginContext context = getLoginContext(request);
54: if (context == null)
55: return null;
56: LoginModule loginModule = null;
57: try {
58: loginModule = context.getLoginModule();
59: } catch (RuntimeException e) {
60: LOG.error(e);
61: }
62: return loginModule;
63: }
64:
65: public void logout(HttpServletRequest request, int principalId)
66: throws AuthenticationException {
67: LoginModule loginModule = getLoginModule(request);
68: if (loginModule != null) {
69: loginModule.logout(request);
70: }
71:
72: }
73:
74: }
|