@Override public AuthenticationResult<Account> authenticate(UpgradeRequest request) throws AuthenticationException { try { Map<String, List<String>> parameters = request.getParameterMap(); List<String> usernames = parameters.get("login"); List<String> passwords = parameters.get("password"); if (usernames == null || usernames.size() == 0 || passwords == null || passwords.size() == 0) { return new AuthenticationResult<>(Optional.empty(), false); } BasicCredentials credentials = new BasicCredentials(usernames.get(0).replace(" ", "+"), passwords.get(0).replace(" ", "+")); return new AuthenticationResult<>(accountAuthenticator.authenticate(credentials), true); } catch (io.dropwizard.auth.AuthenticationException e) { throw new AuthenticationException(e); } }
@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; } }
@Override public AuthenticationResult<HelloAccount> authenticate(UpgradeRequest request) throws AuthenticationException { try { Map<String, List<String>> parameters = request.getParameterMap(); List<String> usernames = parameters.get("login"); List<String> passwords = parameters.get("password"); if (usernames == null || usernames.size() == 0 || passwords == null || passwords.size() == 0) { return new AuthenticationResult<>(Optional.empty(), false); } BasicCredentials credentials = new BasicCredentials(usernames.get(0), passwords.get(0)); return new AuthenticationResult<>(basicAuthenticator.authenticate(credentials), true); } catch (io.dropwizard.auth.AuthenticationException e) { throw new AuthenticationException(e); } }