1 package org
.argeo
.cms
.websocket
.javax
.server
;
3 import java
.security
.AccessController
;
4 import java
.security
.PrivilegedAction
;
7 import javax
.security
.auth
.Subject
;
8 import javax
.security
.auth
.login
.LoginContext
;
9 import javax
.websocket
.Extension
;
10 import javax
.websocket
.HandshakeResponse
;
11 import javax
.websocket
.server
.HandshakeRequest
;
12 import javax
.websocket
.server
.ServerEndpointConfig
;
13 import javax
.websocket
.server
.ServerEndpointConfig
.Configurator
;
15 import org
.argeo
.api
.cms
.CmsAuth
;
16 import org
.argeo
.api
.cms
.CmsLog
;
17 import org
.argeo
.cms
.auth
.RemoteAuthCallbackHandler
;
18 import org
.argeo
.cms
.auth
.RemoteAuthSession
;
19 import org
.argeo
.cms
.servlet
.ServletHttpSession
;
20 import org
.osgi
.service
.http
.context
.ServletContextHelper
;
23 * <strong>Disabled until third party issues are solved.</strong>. Customises
24 * the initialisation of a new web socket.
26 public class CmsWebSocketConfigurator
extends Configurator
{
27 public final static String WEBSOCKET_SUBJECT
= "org.argeo.cms.websocket.subject";
29 private final static CmsLog log
= CmsLog
.getLog(CmsWebSocketConfigurator
.class);
30 final static String HEADER_WWW_AUTHENTICATE
= "WWW-Authenticate";
33 public boolean checkOrigin(String originHeaderValue
) {
38 public <T
> T
getEndpointInstance(Class
<T
> endpointClass
) throws InstantiationException
{
40 return endpointClass
.getDeclaredConstructor().newInstance();
41 } catch (Exception e
) {
42 throw new IllegalArgumentException("Cannot get endpoint instance", e
);
47 public List
<Extension
> getNegotiatedExtensions(List
<Extension
> installed
, List
<Extension
> requested
) {
52 public String
getNegotiatedSubprotocol(List
<String
> supported
, List
<String
> requested
) {
53 if ((requested
== null) || (requested
.size() == 0))
55 if ((supported
== null) || (supported
.isEmpty()))
57 for (String possible
: requested
) {
60 if (supported
.contains(possible
))
67 public void modifyHandshake(ServerEndpointConfig sec
, HandshakeRequest request
, HandshakeResponse response
) {
71 RemoteAuthSession httpSession
= new ServletHttpSession(
72 (javax
.servlet
.http
.HttpSession
) request
.getHttpSession());
73 if (log
.isDebugEnabled() && httpSession
!= null)
74 log
.debug("Web socket HTTP session id: " + httpSession
.getId());
76 if (httpSession
== null) {
77 rejectResponse(response
, null);
80 LoginContext lc
= new LoginContext(CmsAuth
.LOGIN_CONTEXT_USER
, new RemoteAuthCallbackHandler(httpSession
));
82 if (log
.isDebugEnabled())
83 log
.debug("Web socket logged-in as " + lc
.getSubject());
84 Subject
.doAs(lc
.getSubject(), new PrivilegedAction
<Void
>() {
88 sec
.getUserProperties().put(ServletContextHelper
.REMOTE_USER
, AccessController
.getContext());
93 } catch (Exception e
) {
94 rejectResponse(response
, e
);
99 * Behaviour when the web socket could not be authenticated. Throws an
100 * {@link IllegalStateException} by default.
102 * @param e can be null
104 protected void rejectResponse(HandshakeResponse response
, Exception e
) {
105 // violent implementation, as suggested in
106 // https://stackoverflow.com/questions/21763829/jsr-356-how-to-abort-a-websocket-connection-during-the-handshake
107 // throw new IllegalStateException("Web socket cannot be authenticated");