]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.rap/src/main/java/org/argeo/security/ui/rap/SecureEntryPoint.java
Improve Security UI
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.rap / src / main / java / org / argeo / security / ui / rap / SecureEntryPoint.java
1 package org.argeo.security.ui.rap;
2
3 import java.security.PrivilegedAction;
4
5 import javax.security.auth.Subject;
6 import javax.security.auth.login.LoginException;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.eclipse.equinox.security.auth.ILoginContext;
11 import org.eclipse.jface.dialogs.Dialog;
12 import org.eclipse.jface.dialogs.MessageDialog;
13 import org.eclipse.rwt.RWT;
14 import org.eclipse.rwt.lifecycle.IEntryPoint;
15 import org.eclipse.rwt.service.SessionStoreEvent;
16 import org.eclipse.rwt.service.SessionStoreListener;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
22 import org.eclipse.ui.application.WorkbenchAdvisor;
23 import org.eclipse.ui.application.WorkbenchWindowAdvisor;
24
25 public class SecureEntryPoint implements IEntryPoint, SessionStoreListener {
26 private final static Log log = LogFactory.getLog(SecureEntryPoint.class);
27
28 @SuppressWarnings("unchecked")
29 @Override
30 public int createUI() {
31 // 15 mins session timeout
32 RWT.getRequest().getSession().setMaxInactiveInterval(15 * 60);
33
34 if (log.isDebugEnabled())
35 log.debug("THREAD=" + Thread.currentThread().getId()
36 + ", sessionStore=" + RWT.getSessionStore().getId());
37
38 final ILoginContext loginContext = SecureRapActivator
39 .createLoginContext();
40 Integer returnCode = null;
41 Display display = PlatformUI.createDisplay();
42
43 Subject subject = null;
44 try {
45 loginContext.login();
46 subject = loginContext.getSubject();
47 } catch (LoginException e) {
48 log.error("Error when logging in.", e);
49 MessageDialog.openInformation(display.getActiveShell(),
50 "Login failed", "Login failed");
51 display.dispose();
52 RWT.getRequest().getSession().setMaxInactiveInterval(1);
53 try {
54 Thread.sleep(2000);
55 } catch (InterruptedException e1) {
56 // silent
57 }
58 // throw new RuntimeException("Login failed", e);
59 return -1;
60 }
61
62 // identify after successful login
63 if (log.isDebugEnabled())
64 log.debug("subject=" + subject);
65 final String username = subject.getPrincipals().iterator().next()
66 .getName();
67 if (log.isDebugEnabled())
68 log.debug(username + " logged in");
69 display.disposeExec(new Runnable() {
70 public void run() {
71 log.debug("Display disposed");
72 logout(loginContext, username);
73 // invalidate session
74 RWT.getRequest().getSession().setMaxInactiveInterval(1);
75 try {
76 Thread.sleep(2000);
77 } catch (InterruptedException e1) {
78 // silent
79 }
80 }
81 });
82
83 try {
84 returnCode = (Integer) Subject.doAs(subject, getRunAction(display));
85 loginContext.logout();
86 return processReturnCode(returnCode);
87 } catch (Exception e) {
88 if (subject != null)
89 logout(loginContext, username);
90 // RWT.getRequest().getSession().setMaxInactiveInterval(1);
91 log.error("Unexpected error", e);
92 // throw new ArgeoException("Cannot login", e);
93 } finally {
94 display.dispose();
95 }
96 return -1;
97 }
98
99 static void logout(ILoginContext secureContext, String username) {
100 try {
101 secureContext.logout();
102 log.info("Logged out " + (username != null ? username : "")
103 + " (THREAD=" + Thread.currentThread().getId() + ")");
104 } catch (LoginException e) {
105 log.error("Erorr when logging out", e);
106 }
107 }
108
109 // static void closeWorkbench() {
110 // final IWorkbench workbench;
111 // try {
112 // workbench = PlatformUI.getWorkbench();
113 // } catch (Exception e) {
114 // return;
115 // }
116 // if (workbench == null)
117 // return;
118 // final Display display = workbench.getDisplay();
119 // if (display != null && !display.isDisposed())
120 // display.syncExec(new Runnable() {
121 //
122 // public void run() {
123 // if (!display.isDisposed())
124 // workbench.close();
125 // }
126 // });
127 //
128 // if (log.isDebugEnabled())
129 // log.debug("Workbench closed");
130 // }
131
132 static class FailedLogin extends MessageDialog {
133
134 public FailedLogin(Shell parentShell, String dialogTitle,
135 Image dialogTitleImage, String dialogMessage,
136 int dialogImageType, String[] dialogButtonLabels,
137 int defaultIndex) {
138 super(parentShell, "Failed ", dialogTitleImage, dialogMessage,
139 dialogImageType, dialogButtonLabels, defaultIndex);
140 // TODO Auto-generated constructor stub
141 }
142
143 }
144
145 @SuppressWarnings("rawtypes")
146 private PrivilegedAction getRunAction(final Display display) {
147 return new PrivilegedAction() {
148 public Object run() {
149 int result = createAndRunWorkbench(display);
150 return new Integer(result);
151 }
152 };
153 }
154
155 protected Integer createAndRunWorkbench(Display display) {
156 return PlatformUI.createAndRunWorkbench(display,
157 createWorkbenchAdvisor());
158 }
159
160 protected Integer processReturnCode(Integer returnCode) {
161 return returnCode;
162 }
163
164 protected WorkbenchAdvisor createWorkbenchAdvisor() {
165 return new SecureWorkbenchAdvisor() {
166 public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
167 IWorkbenchWindowConfigurer configurer) {
168 return new RapSecureWorkbenchWindowAdvisor(configurer);
169 }
170
171 };
172 }
173
174 @Override
175 public void beforeDestroy(SessionStoreEvent event) {
176 if (log.isDebugEnabled())
177 log.debug("RWT session " + event.getSessionStore().getId()
178 + " about to be destroyed. THREAD="
179 + Thread.currentThread().getId());
180
181 }
182
183 }