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());
}
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());
}
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());
}
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;
}
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());
}
Aggregations