]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rap/AnonymousEntryPoint.java
Prepare next development cycle
[lgpl/argeo-commons.git] / rap / AnonymousEntryPoint.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.argeo.ArgeoException;
11 import org.eclipse.equinox.security.auth.ILoginContext;
12 import org.eclipse.rwt.RWT;
13 import org.eclipse.rwt.lifecycle.IEntryPoint;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.ui.PlatformUI;
16
17 /**
18 * RAP entry point which authenticates the subject as anonymous, for public
19 * unauthenticated access.
20 */
21 public class AnonymousEntryPoint implements IEntryPoint {
22 private final static Log log = LogFactory.getLog(AnonymousEntryPoint.class);
23
24 /**
25 * How many seconds to wait before invalidating the session if the user has
26 * not yet logged in.
27 */
28 private Integer loginTimeout = 1 * 60;
29 private Integer sessionTimeout = 15 * 60;
30
31 @Override
32 public int createUI() {
33 // Short login timeout so that the modal dialog login doesn't hang
34 // around too long
35 RWT.getRequest().getSession().setMaxInactiveInterval(loginTimeout);
36
37 if (log.isDebugEnabled())
38 log.debug("Anonymous THREAD=" + Thread.currentThread().getId()
39 + ", sessionStore=" + RWT.getSessionStore().getId());
40
41 // create display
42 final Display display = PlatformUI.createDisplay();
43
44 // log in
45 final ILoginContext loginContext = SecureRapActivator
46 .createLoginContext(SecureRapActivator.CONTEXT_SPRING_ANONYMOUS);
47 Subject subject = null;
48 try {
49 loginContext.login();
50 subject = loginContext.getSubject();
51 } catch (LoginException e) {
52 throw new ArgeoException(
53 "Unexpected exception during authentication", e);
54 }
55
56 // identify after successful login
57 if (log.isDebugEnabled())
58 log.debug("Authenticated " + subject);
59 final String username = subject.getPrincipals().iterator().next()
60 .getName();
61
62 // Once the user is logged in, she can have a longer session timeout
63 RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
64
65 // Logout callback when the display is disposed
66 display.disposeExec(new Runnable() {
67 public void run() {
68 log.debug("Display disposed");
69 logout(loginContext, username);
70 }
71 });
72
73 //
74 // RUN THE WORKBENCH
75 //
76 Integer returnCode = null;
77 try {
78 returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
79 public Integer run() {
80 RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
81 null);
82 int result = PlatformUI.createAndRunWorkbench(display,
83 workbenchAdvisor);
84 return new Integer(result);
85 }
86 });
87 logout(loginContext, username);
88 } finally {
89 display.dispose();
90 }
91 return returnCode;
92 }
93
94 private void logout(ILoginContext secureContext, String username) {
95 try {
96 secureContext.logout();
97 log.info("Logged out " + (username != null ? username : "")
98 + " (THREAD=" + Thread.currentThread().getId() + ")");
99 } catch (LoginException e) {
100 log.error("Erorr when logging out", e);
101 }
102 }
103 }