]> git.argeo.org Git - lgpl/argeo-commons.git/blob - AnonymousEntryPoint.java
99536faa11827574aa64ba2bd791a8fec786ac16
[lgpl/argeo-commons.git] / AnonymousEntryPoint.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.rap;
17
18 import java.security.PrivilegedAction;
19
20 import javax.security.auth.Subject;
21 import javax.security.auth.login.LoginContext;
22 import javax.security.auth.login.LoginException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.ArgeoException;
27 import org.argeo.cms.KernelHeader;
28 import org.argeo.cms.auth.ArgeoLoginContext;
29 import org.eclipse.rap.rwt.RWT;
30 import org.eclipse.rap.rwt.application.EntryPoint;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.ui.PlatformUI;
33
34 /**
35 * RAP entry point which authenticates the subject as anonymous, for public
36 * unauthenticated access.
37 */
38 public class AnonymousEntryPoint implements EntryPoint {
39 private final static Log log = LogFactory.getLog(AnonymousEntryPoint.class);
40
41 /**
42 * How many seconds to wait before invalidating the session if the user has
43 * not yet logged in.
44 */
45 private Integer sessionTimeout = 5 * 60;
46
47 @Override
48 public int createUI() {
49 RWT.getRequest().getSession().setMaxInactiveInterval(sessionTimeout);
50
51 // if (log.isDebugEnabled())
52 // log.debug("Anonymous THREAD=" + Thread.currentThread().getId()
53 // + ", sessionStore=" + RWT.getSessionStore().getId());
54
55 final Display display = PlatformUI.createDisplay();
56 Subject subject = new Subject();
57
58 final LoginContext loginContext;
59 try {
60 loginContext = new ArgeoLoginContext(
61 KernelHeader.LOGIN_CONTEXT_ANONYMOUS, subject);
62 loginContext.login();
63 } catch (LoginException e1) {
64 throw new ArgeoException("Cannot initialize login context", e1);
65 }
66
67 // identify after successful login
68 if (log.isDebugEnabled())
69 log.debug("Authenticated " + subject);
70 final String username = subject.getPrincipals().iterator().next()
71 .getName();
72
73 // Logout callback when the display is disposed
74 display.disposeExec(new Runnable() {
75 public void run() {
76 log.debug("Display disposed");
77 logout(loginContext, username);
78 }
79 });
80
81 //
82 // RUN THE WORKBENCH
83 //
84 Integer returnCode = null;
85 try {
86 returnCode = Subject.doAs(subject, new PrivilegedAction<Integer>() {
87 public Integer run() {
88 RapWorkbenchAdvisor workbenchAdvisor = new RapWorkbenchAdvisor(
89 null);
90 int result = PlatformUI.createAndRunWorkbench(display,
91 workbenchAdvisor);
92 return new Integer(result);
93 }
94 });
95 logout(loginContext, username);
96 if (log.isTraceEnabled())
97 log.trace("Return code " + returnCode);
98 } finally {
99 display.dispose();
100 }
101 return 1;
102 }
103
104 private void logout(LoginContext loginContext, String username) {
105 try {
106 loginContext.logout();
107 log.info("Logged out " + (username != null ? username : "")
108 + " (THREAD=" + Thread.currentThread().getId() + ")");
109 } catch (LoginException e) {
110 log.error("Erorr when logging out", e);
111 }
112 }
113 }