Search in sources :

Example 1 with HandshakeProcessor

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor in project carbon-apimgt by wso2.

the class HandshakeProcessorTest method handleSuccessfulHandshake.

@Test
public void handleSuccessfulHandshake() throws Exception {
    InboundMessageContext inboundMessageContext = new InboundMessageContext();
    URLMapping urlMapping = new URLMapping();
    urlMapping.setHttpMethod("SUBSCRIPTION");
    urlMapping.setThrottlingPolicy("Unlimited");
    urlMapping.setUrlPattern("liftStatusChange");
    org.wso2.carbon.apimgt.keymgt.model.entity.API api = new API();
    api.addResource(urlMapping);
    inboundMessageContext.setElectedAPI(api);
    PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
    PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenReturn(true);
    HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
    InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
    Assert.assertFalse(inboundProcessorResponseDTO.isError());
    Assert.assertNull(inboundProcessorResponseDTO.getErrorMessage());
    Assert.assertFalse(inboundProcessorResponseDTO.isCloseConnection());
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) InboundProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO) InboundMessageContext(org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with HandshakeProcessor

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor in project carbon-apimgt by wso2.

the class HandshakeProcessorTest method handleAuthenticationException.

@Test
public void handleAuthenticationException() throws Exception {
    InboundMessageContext inboundMessageContext = new InboundMessageContext();
    PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
    PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenThrow(new APIManagementException("Error while accessing truststore"));
    HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
    InboundProcessorResponseDTO errorResponseDTO = new InboundProcessorResponseDTO();
    errorResponseDTO.setError(true);
    errorResponseDTO.setErrorCode(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR);
    errorResponseDTO.setErrorMessage(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_GENERAL_MESSAGE);
    errorResponseDTO.setCloseConnection(true);
    PowerMockito.when(InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_GENERAL_MESSAGE)).thenReturn(errorResponseDTO);
    InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
    Assert.assertTrue(inboundProcessorResponseDTO.isError());
    Assert.assertNotNull(inboundProcessorResponseDTO.getErrorMessage());
    Assert.assertTrue(inboundProcessorResponseDTO.isCloseConnection());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorResponseString(), errorResponseDTO.getErrorResponseString());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), errorResponseDTO.getErrorCode());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), errorResponseDTO.getErrorMessage());
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) InboundProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO) InboundMessageContext(org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with HandshakeProcessor

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor in project carbon-apimgt by wso2.

the class HandshakeProcessorTest method handleAPISecurityException.

@Test
public void handleAPISecurityException() throws Exception {
    InboundMessageContext inboundMessageContext = new InboundMessageContext();
    PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
    PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenThrow(new APISecurityException(APISecurityConstants.API_AUTH_INVALID_CREDENTIALS, APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE));
    HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
    InboundProcessorResponseDTO errorResponseDTO = new InboundProcessorResponseDTO();
    errorResponseDTO.setError(true);
    errorResponseDTO.setErrorCode(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR);
    errorResponseDTO.setErrorMessage(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
    errorResponseDTO.setCloseConnection(true);
    PowerMockito.when(InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE)).thenReturn(errorResponseDTO);
    InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
    Assert.assertTrue(inboundProcessorResponseDTO.isError());
    Assert.assertNotNull(inboundProcessorResponseDTO.getErrorMessage());
    Assert.assertTrue(inboundProcessorResponseDTO.isCloseConnection());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorResponseString(), errorResponseDTO.getErrorResponseString());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), errorResponseDTO.getErrorCode());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), errorResponseDTO.getErrorMessage());
}
Also used : APISecurityException(org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException) InboundProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO) InboundMessageContext(org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with HandshakeProcessor

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor in project carbon-apimgt by wso2.

the class InboundWebSocketProcessor method handleHandshake.

/**
 * This method process websocket handshake and extract necessary API information from the channel context and
 * request. Finally, hand over the processing to relevant handshake processor for authentication etc.
 *
 * @param req                   Handshake request
 * @param ctx                   Channel pipeline context
 * @param inboundMessageContext InboundMessageContext
 * @return InboundProcessorResponseDTO with handshake processing response
 */
public InboundProcessorResponseDTO handleHandshake(FullHttpRequest req, ChannelHandlerContext ctx, InboundMessageContext inboundMessageContext) {
    InboundProcessorResponseDTO inboundProcessorResponseDTO;
    try {
        HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
        setUris(req, inboundMessageContext);
        InboundWebsocketProcessorUtil.setTenantDomainToContext(inboundMessageContext);
        setMatchingResource(ctx, req, inboundMessageContext);
        String userAgent = req.headers().get(HttpHeaders.USER_AGENT);
        // '-' is used for empty values to avoid possible errors in DAS side.
        // Required headers are stored one by one as validateOAuthHeader()
        // removes some headers from the request
        userAgent = userAgent != null ? userAgent : "-";
        inboundMessageContext.getRequestHeaders().put(HttpHeaders.USER_AGENT, userAgent);
        PrivilegedCarbonContext.startTenantFlow();
        PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(inboundMessageContext.getTenantDomain(), true);
        if (validateOAuthHeader(req, inboundMessageContext)) {
            setRequestHeaders(req, inboundMessageContext);
            inboundMessageContext.getRequestHeaders().put(HttpHeaders.AUTHORIZATION, req.headers().get(HttpHeaders.AUTHORIZATION));
            inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
        } else {
            String errorMessage = "No Authorization Header or access_token query parameter present";
            log.error(errorMessage + " in request for the websocket context " + inboundMessageContext.getApiContext());
            inboundProcessorResponseDTO = InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, errorMessage);
        }
        publishHandshakeAuthErrorEvent(ctx, inboundProcessorResponseDTO.getErrorMessage());
        return inboundProcessorResponseDTO;
    } catch (APISecurityException e) {
        log.error("Authentication Failure for the websocket context: " + inboundMessageContext.getApiContext() + e.getMessage());
        inboundProcessorResponseDTO = InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, e.getMessage());
        publishHandshakeAuthErrorEvent(ctx, e.getMessage());
    } catch (WebSocketApiException e) {
        log.error(e.getMessage());
        inboundProcessorResponseDTO = InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.INTERNAL_SERVER_ERROR, e.getMessage());
    } catch (ResourceNotFoundException e) {
        log.error(e.getMessage());
        inboundProcessorResponseDTO = InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.RESOURCE_NOT_FOUND_ERROR, e.getMessage());
        publishResourceNotFoundEvent(ctx);
    }
    return inboundProcessorResponseDTO;
}
Also used : APISecurityException(org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException) WebSocketApiException(org.wso2.carbon.apimgt.gateway.handlers.streaming.websocket.WebSocketApiException) ResourceNotFoundException(org.wso2.carbon.apimgt.gateway.handlers.security.ResourceNotFoundException) HandshakeProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor)

Example 5 with HandshakeProcessor

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor in project carbon-apimgt by wso2.

the class HandshakeProcessorTest method handleFailedAuthentication.

@Test
public void handleFailedAuthentication() throws Exception {
    InboundMessageContext inboundMessageContext = new InboundMessageContext();
    PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
    PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenReturn(false);
    HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
    InboundProcessorResponseDTO errorResponseDTO = new InboundProcessorResponseDTO();
    errorResponseDTO.setError(true);
    errorResponseDTO.setErrorCode(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR);
    errorResponseDTO.setErrorMessage(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
    errorResponseDTO.setCloseConnection(true);
    PowerMockito.when(InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE)).thenReturn(errorResponseDTO);
    InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
    Assert.assertTrue(inboundProcessorResponseDTO.isError());
    Assert.assertNotNull(inboundProcessorResponseDTO.getErrorMessage());
    Assert.assertTrue(inboundProcessorResponseDTO.isCloseConnection());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorResponseString(), errorResponseDTO.getErrorResponseString());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), errorResponseDTO.getErrorCode());
    Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), errorResponseDTO.getErrorMessage());
}
Also used : InboundProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO) InboundMessageContext(org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 InboundMessageContext (org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext)4 InboundProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO)4 APISecurityException (org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 URLMapping (org.wso2.carbon.apimgt.api.model.subscription.URLMapping)1 ResourceNotFoundException (org.wso2.carbon.apimgt.gateway.handlers.security.ResourceNotFoundException)1 WebSocketApiException (org.wso2.carbon.apimgt.gateway.handlers.streaming.websocket.WebSocketApiException)1 HandshakeProcessor (org.wso2.carbon.apimgt.gateway.inbound.websocket.handshake.HandshakeProcessor)1 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)1