@Test public void testUnauthorized() throws AuthenticationException, IOException { ResourceConfig jerseyEnvironment = new DropwizardResourceConfig(); WebSocketEnvironment environment = mock(WebSocketEnvironment.class ); WebSocketAuthenticator authenticator = mock(WebSocketAuthenticator.class); ServletUpgradeRequest request = mock(ServletUpgradeRequest.class ); ServletUpgradeResponse response = mock(ServletUpgradeResponse.class); when(environment.getAuthenticator()).thenReturn(authenticator); when(authenticator.authenticate(eq(request))).thenReturn(new WebSocketAuthenticator.AuthenticationResult<>(Optional.empty(), true)); when(environment.jersey()).thenReturn(jerseyEnvironment); WebSocketResourceProviderFactory factory = new WebSocketResourceProviderFactory(environment, Account.class); Object connection = factory.createWebSocket(request, response); assertNull(connection); verify(response).sendForbidden(eq("Unauthorized")); verify(authenticator).authenticate(eq(request)); }
@Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator<T>> authenticator = Optional.ofNullable(environment.getAuthenticator()); T authenticated = null; if (authenticator.isPresent()) { AuthenticationResult<T> authenticationResult = authenticator.get().authenticate(request); if (authenticationResult.getUser().isEmpty() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider<T>(getRemoteAddress(request), this.jerseyApplicationHandler, this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); try { response.sendError(500, "Failure"); } catch (IOException ex) {} return null; } }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator<T>> authenticator = Optional.ofNullable(environment.getAuthenticator()); T authenticated = null; if (authenticator.isPresent()) { AuthenticationResult<T> authenticationResult = authenticator.get().authenticate(request); if (authenticationResult.getUser().isEmpty() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider<T>(getRemoteAddress(request), this.jerseyApplicationHandler, this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); try { response.sendError(500, "Failure"); } catch (IOException ex) {} return null; } } }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator<T>> authenticator = Optional.ofNullable(environment.getAuthenticator()); T authenticated = null; if (authenticator.isPresent()) { AuthenticationResult<T> authenticationResult = authenticator.get().authenticate(request); if (authenticationResult.getUser().isEmpty() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider<T>(getRemoteAddress(request), this.jerseyApplicationHandler, this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); try { response.sendError(500, "Failure"); } catch (IOException ex) {} return null; } } WebSocketResourceProviderFactory(WebSocketEnvironment<T> environment, Class<T> principalClass); }
WebSocketResourceProviderFactory extends WebSocketServlet implements WebSocketCreator { @Override public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) { try { Optional<WebSocketAuthenticator<T>> authenticator = Optional.ofNullable(environment.getAuthenticator()); T authenticated = null; if (authenticator.isPresent()) { AuthenticationResult<T> authenticationResult = authenticator.get().authenticate(request); if (authenticationResult.getUser().isEmpty() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider<T>(getRemoteAddress(request), this.jerseyApplicationHandler, this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); try { response.sendError(500, "Failure"); } catch (IOException ex) {} return null; } } WebSocketResourceProviderFactory(WebSocketEnvironment<T> environment, Class<T> principalClass); @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<T>> authenticator = Optional.ofNullable(environment.getAuthenticator()); T authenticated = null; if (authenticator.isPresent()) { AuthenticationResult<T> authenticationResult = authenticator.get().authenticate(request); if (authenticationResult.getUser().isEmpty() && authenticationResult.isRequired()) { response.sendForbidden("Unauthorized"); return null; } else { authenticated = authenticationResult.getUser().orElse(null); } } return new WebSocketResourceProvider<T>(getRemoteAddress(request), this.jerseyApplicationHandler, this.environment.getRequestLog(), authenticated, this.environment.getMessageFactory(), ofNullable(this.environment.getConnectListener()), this.environment.getIdleTimeoutMillis()); } catch (AuthenticationException | IOException e) { logger.warn("Authentication failure", e); try { response.sendError(500, "Failure"); } catch (IOException ex) {} return null; } } WebSocketResourceProviderFactory(WebSocketEnvironment<T> environment, Class<T> principalClass); @Override Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response); @Override void configure(WebSocketServletFactory factory);  }