2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.ui
.rap
;
18 import java
.security
.PrivilegedAction
;
20 import javax
.security
.auth
.Subject
;
21 import javax
.security
.auth
.login
.LoginException
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.ArgeoException
;
26 import org
.eclipse
.equinox
.security
.auth
.ILoginContext
;
27 import org
.eclipse
.rap
.rwt
.RWT
;
28 import org
.eclipse
.rap
.rwt
.application
.IEntryPoint
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.ui
.PlatformUI
;
33 * RAP entry point which authenticates the subject as anonymous, for public
34 * unauthenticated access.
36 public class AnonymousEntryPoint
implements IEntryPoint
{
37 private final static Log log
= LogFactory
.getLog(AnonymousEntryPoint
.class);
40 * How many seconds to wait before invalidating the session if the user has
43 private Integer loginTimeout
= 1 * 60;
44 private Integer sessionTimeout
= 15 * 60;
47 public int createUI() {
48 // Short login timeout so that the modal dialog login doesn't hang
50 RWT
.getRequest().getSession().setMaxInactiveInterval(loginTimeout
);
52 if (log
.isDebugEnabled())
53 log
.debug("Anonymous THREAD=" + Thread
.currentThread().getId()
54 + ", sessionStore=" + RWT
.getSessionStore().getId());
57 final Display display
= PlatformUI
.createDisplay();
60 final ILoginContext loginContext
= SecureRapActivator
61 .createLoginContext(SecureRapActivator
.CONTEXT_SPRING_ANONYMOUS
);
62 Subject subject
= null;
65 subject
= loginContext
.getSubject();
66 } catch (LoginException e
) {
67 throw new ArgeoException(
68 "Unexpected exception during authentication", e
);
71 // identify after successful login
72 if (log
.isDebugEnabled())
73 log
.debug("Authenticated " + subject
);
74 final String username
= subject
.getPrincipals().iterator().next()
77 // Once the user is logged in, she can have a longer session timeout
78 RWT
.getRequest().getSession().setMaxInactiveInterval(sessionTimeout
);
80 // Logout callback when the display is disposed
81 display
.disposeExec(new Runnable() {
83 log
.debug("Display disposed");
84 logout(loginContext
, username
);
91 Integer returnCode
= null;
93 returnCode
= Subject
.doAs(subject
, new PrivilegedAction
<Integer
>() {
94 public Integer
run() {
95 RapWorkbenchAdvisor workbenchAdvisor
= new RapWorkbenchAdvisor(
97 int result
= PlatformUI
.createAndRunWorkbench(display
,
99 return new Integer(result
);
102 logout(loginContext
, username
);
109 private void logout(ILoginContext secureContext
, String username
) {
111 secureContext
.logout();
112 log
.info("Logged out " + (username
!= null ? username
: "")
113 + " (THREAD=" + Thread
.currentThread().getId() + ")");
114 } catch (LoginException e
) {
115 log
.error("Erorr when logging out", e
);