@Override
  public void onHandshakeRequest(UpgradeRequest request) {
    if (configurator == null) {
      return;
    }

    Map<String, List<String>> headers = request.getHeaders();
    configurator.beforeRequest(headers);
    request.setHeaders(headers);
  }
  @Test
  public void testCredentials() throws Exception {
    MessagesManager storedMessages = mock(MessagesManager.class);
    WebSocketAccountAuthenticator webSocketAuthenticator =
        new WebSocketAccountAuthenticator(accountAuthenticator);
    AuthenticatedConnectListener connectListener =
        new AuthenticatedConnectListener(
            accountsManager,
            pushSender,
            receiptSender,
            storedMessages,
            pubSubManager,
            apnFallbackManager);
    WebSocketSessionContext sessionContext = mock(WebSocketSessionContext.class);

    when(accountAuthenticator.authenticate(eq(new BasicCredentials(VALID_USER, VALID_PASSWORD))))
        .thenReturn(Optional.of(account));

    when(accountAuthenticator.authenticate(
            eq(new BasicCredentials(INVALID_USER, INVALID_PASSWORD))))
        .thenReturn(Optional.<Account>absent());

    when(account.getAuthenticatedDevice()).thenReturn(Optional.of(device));

    when(upgradeRequest.getParameterMap())
        .thenReturn(
            new HashMap<String, String[]>() {
              {
                put("login", new String[] {VALID_USER});
                put("password", new String[] {VALID_PASSWORD});
              }
            });

    Optional<Account> account = webSocketAuthenticator.authenticate(upgradeRequest);
    when(sessionContext.getAuthenticated(Account.class)).thenReturn(account);

    connectListener.onWebSocketConnect(sessionContext);

    verify(sessionContext).addListener(any(WebSocketSessionContext.WebSocketEventListener.class));

    when(upgradeRequest.getParameterMap())
        .thenReturn(
            new HashMap<String, String[]>() {
              {
                put("login", new String[] {INVALID_USER});
                put("password", new String[] {INVALID_PASSWORD});
              }
            });

    account = webSocketAuthenticator.authenticate(upgradeRequest);
    assertFalse(account.isPresent());
  }
 public void setUpgradeRequest(UpgradeRequest request) {
   this.upgradeRequest = request;
   this.protocolVersion = request.getProtocolVersion();
   this.parameterMap.clear();
   if (request.getParameterMap() != null) {
     for (Map.Entry<String, List<String>> entry : request.getParameterMap().entrySet()) {
       List<String> values = entry.getValue();
       if (values != null) {
         this.parameterMap.put(entry.getKey(), values.toArray(new String[values.size()]));
       } else {
         this.parameterMap.put(entry.getKey(), new String[0]);
       }
     }
   }
 }
Exemplo n.º 4
0
  @Override
  public void handshake(Muxer muxer, MuxChannel channel, UpgradeRequest request)
      throws MuxException, IOException {
    StringBuilder response = new StringBuilder();
    response.append("HTTP/1.1 101 Switching Protocols\r\n");
    response.append("Connection: upgrade\r\n");
    // not meaningful (per Draft 08) hresp.append("Upgrade: websocket\r\n");
    // not meaningful (per Draft 08) hresp.append("Sec-WebSocket-Accept:
    // Kgo85/8KVE8YPONSeyhgL3GwqhI=\r\n");
    response.append("\r\n");

    EventDriver websocket = this.eventDriverFactory.wrap(echo);
    WebSocketSession session = new WebSocketSession(request.getRequestURI(), websocket, channel);
    session.setNegotiatedSubprotocol("echo");
    channel.setSession(session);
    channel.setSubProtocol("echo");
    channel.onOpen();
    session.open();

    MuxAddChannelResponse addChannelResponse = new MuxAddChannelResponse();
    addChannelResponse.setChannelId(channel.getChannelId());
    addChannelResponse.setEncoding(MuxAddChannelResponse.IDENTITY_ENCODING);
    addChannelResponse.setFailed(false);
    addChannelResponse.setHandshake(response.toString());

    muxer.output(addChannelResponse);
  }
  @Override
  public boolean isSecure() {
    if (upgradeRequest == null) {
      throw new IllegalStateException("No valid UpgradeRequest yet");
    }

    URI requestURI = upgradeRequest.getRequestURI();

    return "wss".equalsIgnoreCase(requestURI.getScheme());
  }