@Test public void testValidAuthorization() throws AuthenticationException, ServletException { JerseyEnvironment jerseyEnvironment = mock(JerseyEnvironment.class ); WebSocketEnvironment environment = mock(WebSocketEnvironment.class ); WebSocketAuthenticator authenticator = mock(WebSocketAuthenticator.class); ServletUpgradeRequest request = mock(ServletUpgradeRequest.class ); ServletUpgradeResponse response = mock(ServletUpgradeResponse.class); Session session = mock(Session.class ); Account account = new Account(); when(environment.getAuthenticator()).thenReturn(authenticator); when(authenticator.authenticate(eq(request))).thenReturn(new WebSocketAuthenticator.AuthenticationResult<>(Optional.of(account), true)); when(environment.jersey()).thenReturn(jerseyEnvironment); WebSocketResourceProviderFactory factory = new WebSocketResourceProviderFactory(environment); Object connection = factory.createWebSocket(request, response); assertNotNull(connection); verifyNoMoreInteractions(response); verify(authenticator).authenticate(eq(request)); ((WebSocketResourceProvider)connection).onWebSocketConnect(session); assertNotNull(((WebSocketResourceProvider) connection).getContext().getAuthenticated()); assertEquals(((WebSocketResourceProvider)connection).getContext().getAuthenticated(), account); }
@Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator> authenticator = Optional.ofNullable(environment.getAuthenticator()); Object authenticated = null; if (authenticator.isPresent()) { AuthenticationResult authenticationResult = authenticator.get().authenticate(request); if (!authenticationResult.getUser().isPresent() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider(this.environment.getJerseyServletContainer(), this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), Optional.ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); return null; } }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator> authenticator = Optional.ofNullable(environment.getAuthenticator()); Object authenticated = null; if (authenticator.isPresent()) { AuthenticationResult authenticationResult = authenticator.get().authenticate(request); if (!authenticationResult.getUser().isPresent() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider(this.environment.getJerseyServletContainer(), this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), Optional.ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); return null; } } }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator> authenticator = Optional.ofNullable(environment.getAuthenticator()); Object authenticated = null; if (authenticator.isPresent()) { AuthenticationResult authenticationResult = authenticator.get().authenticate(request); if (!authenticationResult.getUser().isPresent() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider(this.environment.getJerseyServletContainer(), this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), Optional.ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); return null; } } WebSocketResourceProviderFactory(WebSocketEnvironment environment); }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator> authenticator = Optional.ofNullable(environment.getAuthenticator()); Object authenticated = null; if (authenticator.isPresent()) { AuthenticationResult authenticationResult = authenticator.get().authenticate(request); if (!authenticationResult.getUser().isPresent() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider(this.environment.getJerseyServletContainer(), this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), Optional.ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); return null; } } WebSocketResourceProviderFactory(WebSocketEnvironment environment); void start(); @Override Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response); @Override void configure(WebSocketServletFactory factory); }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator> authenticator = Optional.ofNullable(environment.getAuthenticator()); Object authenticated = null; if (authenticator.isPresent()) { AuthenticationResult authenticationResult = authenticator.get().authenticate(request); if (!authenticationResult.getUser().isPresent() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider(this.environment.getJerseyServletContainer(), this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), Optional.ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); return null; } } WebSocketResourceProviderFactory(WebSocketEnvironment environment); void start(); @Override Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response); @Override void configure(WebSocketServletFactory factory);  }