use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class APISecurityUtilsTestCase method testSetAuthenticationContext.
public void testSetAuthenticationContext() {
PowerMockito.mockStatic(ServiceReferenceHolder.class);
ServiceReferenceHolder serviceReferenceHolder = Mockito.mock(ServiceReferenceHolder.class);
PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
APIManagerConfiguration apiMgtConfig = Mockito.mock(APIManagerConfiguration.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfiguration()).thenReturn(apiMgtConfig);
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
AuthenticationContext authenticationContext = Mockito.mock(AuthenticationContext.class);
Mockito.when(authenticationContext.getKeyType()).thenReturn("keyType");
APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
// test when caller token is not null
Mockito.when(authenticationContext.getCallerToken()).thenReturn("callertoken");
Mockito.when(messageContext.getProperty(APIConstants.API_KEY_TYPE)).thenReturn("keyType");
// Axis2MessageContext axis2MessageContext = Mockito.mock(Axis2MessageContext.class);
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
Map transportHeaders = new HashMap();
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
Assert.assertEquals(APISecurityUtils.getAuthenticationContext(messageContext).getCallerToken(), "callertoken");
Assert.assertEquals("keyType", messageContext.getProperty(APIConstants.API_KEY_TYPE));
// test for IllegalStateException
String API_AUTH_CONTEXT = "__API_AUTH_CONTEXT";
Mockito.when(authenticationContext.getCallerToken()).thenReturn("newCallerToken");
Mockito.when(messageContext.getProperty(API_AUTH_CONTEXT)).thenReturn("abc");
APISecurityUtils.setAuthenticationContext(messageContext, authenticationContext, "abc");
Assert.assertEquals(APISecurityUtils.getAuthenticationContext(messageContext).getCallerToken(), "newCallerToken");
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testGetThrottledInConditionWithJWTCondition.
@Test
public void testGetThrottledInConditionWithJWTCondition() {
ThrottleProperties throttleProperties = new ThrottleProperties();
throttleProperties.setEnableJwtConditions(true);
String jwt = "eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3c28yLm9yZy9wcm9kdWN0cy9hbSIsImV4cCI6MTM0NTE4MzQ5MjE4MSwiaHR0cDov" + "L3dzbzIub3JnL2NsYWltcy9hYmMiOiJjZGUiLCJodHRwOi8vd3NvMi5vcmcvY2xhaW1zL2JjZCI6Inh5eiJ9.9zGU062DJ5mQ5hne" + "41h4IRpLbaY_b5thRxb3feebOcA";
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setCallerToken(jwt);
ServiceReferenceHolder.getInstance().setThrottleProperties(throttleProperties);
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
Map<String, List<ConditionDto>> conditionMap = new HashMap<>();
conditionMap.put("condition1", Arrays.asList(new ConditionDto[] { getJWTCondition(false) }));
conditionMap.put("default", Arrays.asList(new ConditionDto[] { getJWTCondition(false) }));
String condition = throttleConditionEvaluator.getThrottledInCondition(messageContext, authenticationContext, conditionMap);
Assert.assertEquals(condition, "condition1");
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testRetrievingDefaultThrottlingConditionGroupWhenConditionGroupsAreNotAvailable.
@Test
public void testRetrievingDefaultThrottlingConditionGroupWhenConditionGroupsAreNotAvailable() {
ConditionGroupDTO[] conditionGroupDTOS = { defaultConditionGroupDTO };
List<ConditionGroupDTO> conditionGroupDTOList = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
Assert.assertEquals(conditionGroupDTOList.size(), 1);
Assert.assertEquals(conditionGroupDTOList.get(0).getConditionGroupId(), THROTTLE_POLICY_DEFAULT);
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testRetrievingEmptyApplicableConditionsWhenDefaultAndConditionGroupsAreNotAvailable.
@Test
public void testRetrievingEmptyApplicableConditionsWhenDefaultAndConditionGroupsAreNotAvailable() {
ConditionGroupDTO[] conditionGroupDTOS = new ConditionGroupDTO[0];
List<ConditionGroupDTO> conditionGroupDTOList = throttleConditionEvaluator.getApplicableConditions(TestUtils.getMessageContext(apiContext, apiVersion), new AuthenticationContext(), conditionGroupDTOS);
// Should return empty Condition group in the Condition group array
Assert.assertNull(conditionGroupDTOList.get(0));
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluatorTest method testApplicabilityOfNotMatchingHeaderCondition.
@Test
public void testApplicabilityOfNotMatchingHeaderCondition() {
ConditionGroupDTO conditionGroupDTO = new ConditionGroupDTO();
conditionGroupDTO.setConditionGroupId("HeaderConditionGroup");
ConditionDTO notMatchingCondition = new ConditionDTO();
notMatchingCondition.setConditionType("Header");
notMatchingCondition.setConditionName("host");
notMatchingCondition.setConditionValue("org.ibm.com");
ConditionDTO[] conditionDTOS = { notMatchingCondition };
conditionGroupDTO.setConditions(conditionDTOS);
ConditionGroupDTO[] conditionGroupDTOS = { conditionGroupDTO };
AuthenticationContext authenticationContext = new AuthenticationContext();
authenticationContext.setCallerToken(JWTToken);
MessageContext messageContext = TestUtils.getMessageContext(apiContext, apiVersion);
Map map = new TreeMap();
map.put("host", "org.wso2.com");
((Axis2MessageContext) messageContext).getAxis2MessageContext().setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, map);
List<ConditionGroupDTO> matchingConditionGroups = throttleConditionEvaluator.getApplicableConditions(messageContext, authenticationContext, conditionGroupDTOS);
Assert.assertNull(matchingConditionGroups.get(0));
}
Aggregations