use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class InternalAPIKeyAuthenticatorTest method testAuthenticateWithExpiredToken.
@Test
public void testAuthenticateWithExpiredToken() throws Exception {
PowerMockito.when(GatewayUtils.isInternalKey(Mockito.any(JWTClaimsSet.class))).thenReturn(true);
InternalAPIKeyAuthenticator internalAPIKeyAuthenticator = new InternalAPIKeyAuthenticator(APIMgtGatewayConstants.INTERNAL_KEY);
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1/1.0.0");
Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
API api = new API();
PowerMockito.when(GatewayUtils.getAPI(messageContext)).thenReturn(api);
TreeMap transportHeaders = new TreeMap();
transportHeaders.put(APIMgtGatewayConstants.INTERNAL_KEY, internalKey);
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Mockito.when(messageContext.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/resource");
OpenAPI openAPI = Mockito.mock(OpenAPI.class);
Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.OPEN_API_OBJECT)).thenReturn(openAPI);
PowerMockito.when(OpenAPIUtils.getResourceThrottlingTier(openAPI, messageContext)).thenReturn("GOLD");
PowerMockito.when(GatewayUtils.getTenantDomain()).thenReturn("carbon.super");
Cache internalKeyCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getGatewayInternalKeyCache()).thenReturn(internalKeyCache);
Mockito.when(internalKeyCache.get("28f8d7b0-9e62-4341-bf17-094453d5ffa4")).thenReturn("carbon.super");
Cache internalKeyDataCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getGatewayInternalKeyDataCache()).thenReturn(internalKeyDataCache);
Cache invalidCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getInvalidGatewayInternalKeyCache()).thenReturn(invalidCache);
JWTTokenPayloadInfo jwtTokenPayloadInfo = new JWTTokenPayloadInfo();
jwtTokenPayloadInfo.setPayload(signedJWT.getJWTClaimsSet());
jwtTokenPayloadInfo.setAccessToken(internalKey);
String cacheKey = GatewayUtils.getAccessTokenCacheKey("28f8d7b0-9e62-4341-bf17-094453d5ffa4", "/api1/1.0.0", "1.0.0", "/resource", "GET");
JSONObject subscribedAPI = Mockito.mock(JSONObject.class);
Mockito.when(internalKeyDataCache.get(cacheKey)).thenReturn(jwtTokenPayloadInfo);
PowerMockito.when(GatewayUtils.isJwtTokenExpired(signedJWT.getJWTClaimsSet())).thenReturn(true);
PowerMockito.when(GatewayUtils.validateAPISubscription("/api1/1.0.0", "1.0.0", signedJWT.getJWTClaimsSet(), internalKey.split("\\."), false)).thenReturn(subscribedAPI);
AuthenticationContext authenticationContext = Mockito.mock(AuthenticationContext.class);
PowerMockito.when(GatewayUtils.generateAuthenticationContext("28f8d7b0-9e62-4341-bf17-094453d5ffa4", signedJWT.getJWTClaimsSet(), subscribedAPI, api.getApiTier())).thenReturn(authenticationContext);
PowerMockito.doNothing().when(APISecurityUtils.class, "setAuthenticationContext", messageContext, authenticationContext);
AuthenticationResponse authenticate = internalAPIKeyAuthenticator.authenticate(messageContext);
Assert.assertNotNull(authenticate);
Assert.assertTrue(authenticate.isMandatoryAuthentication());
Assert.assertFalse(authenticate.isAuthenticated());
Assert.assertFalse(authenticate.isContinueToNextAuthenticator());
Assert.assertEquals(authenticate.getErrorCode(), APISecurityConstants.API_AUTH_INVALID_CREDENTIALS);
Assert.assertEquals(authenticate.getErrorMessage(), APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class InternalAPIKeyAuthenticatorTest method testAuthenticateNoCache.
@Test
public void testAuthenticateNoCache() throws Exception {
PowerMockito.when(GatewayUtils.isInternalKey(Mockito.any(JWTClaimsSet.class))).thenReturn(true);
InternalAPIKeyAuthenticator internalAPIKeyAuthenticator = new InternalAPIKeyAuthenticator(APIMgtGatewayConstants.INTERNAL_KEY);
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1/1.0.0");
Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
API api = new API();
PowerMockito.when(GatewayUtils.getAPI(messageContext)).thenReturn(api);
TreeMap transportHeaders = new TreeMap();
transportHeaders.put(APIMgtGatewayConstants.INTERNAL_KEY, internalKey);
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Mockito.when(messageContext.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/resource");
OpenAPI openAPI = Mockito.mock(OpenAPI.class);
Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.OPEN_API_OBJECT)).thenReturn(openAPI);
PowerMockito.when(OpenAPIUtils.getResourceThrottlingTier(openAPI, messageContext)).thenReturn("GOLD");
PowerMockito.when(GatewayUtils.getTenantDomain()).thenReturn("carbon.super");
Cache internalKeyCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getGatewayInternalKeyCache()).thenReturn(internalKeyCache);
Mockito.when(internalKeyCache.get("28f8d7b0-9e62-4341-bf17-094453d5ffa4")).thenReturn(null);
Cache internalKeyDataCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getGatewayInternalKeyDataCache()).thenReturn(internalKeyDataCache);
Cache invalidCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getInvalidGatewayInternalKeyCache()).thenReturn(invalidCache);
Mockito.when(invalidCache.get("28f8d7b0-9e62-4341-bf17-094453d5ffa4")).thenReturn(null);
String cacheKey = GatewayUtils.getAccessTokenCacheKey("28f8d7b0-9e62-4341-bf17-094453d5ffa4", "/api1/1.0.0", "1.0.0", "/resource", "GET");
JSONObject subscribedAPI = Mockito.mock(JSONObject.class);
PowerMockito.when(GatewayUtils.verifyTokenSignature(Mockito.any(SignedJWT.class), Mockito.anyString())).thenReturn(true);
PowerMockito.when(GatewayUtils.isJwtTokenExpired(signedJWT.getJWTClaimsSet())).thenReturn(false);
PowerMockito.when(GatewayUtils.validateAPISubscription("/api1/1.0.0", "1.0.0", signedJWT.getJWTClaimsSet(), internalKey.split("\\."), false)).thenReturn(subscribedAPI);
AuthenticationContext authenticationContext = Mockito.mock(AuthenticationContext.class);
PowerMockito.when(GatewayUtils.generateAuthenticationContext("28f8d7b0-9e62-4341-bf17-094453d5ffa4", signedJWT.getJWTClaimsSet(), subscribedAPI, api.getApiTier())).thenReturn(authenticationContext);
PowerMockito.doNothing().when(APISecurityUtils.class, "setAuthenticationContext", messageContext, authenticationContext);
AuthenticationResponse authenticate = internalAPIKeyAuthenticator.authenticate(messageContext);
Assert.assertNotNull(authenticate);
Assert.assertTrue(authenticate.isMandatoryAuthentication());
Assert.assertTrue(authenticate.isAuthenticated());
Assert.assertFalse(authenticate.isContinueToNextAuthenticator());
Assert.assertEquals(authenticate.getErrorCode(), 0);
Assert.assertNull(authenticate.getErrorMessage());
Mockito.verify(internalKeyCache, Mockito.times(1)).get("28f8d7b0-9e62-4341-bf17-094453d5ffa4");
Mockito.verify(invalidCache, Mockito.times(1)).get("28f8d7b0-9e62-4341-bf17-094453d5ffa4");
Mockito.verify(internalKeyCache, Mockito.times(1)).put("28f8d7b0-9e62-4341-bf17-094453d5ffa4", "carbon.super");
Mockito.verify(internalKeyDataCache, Mockito.times(1)).put(Mockito.anyString(), Mockito.any());
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class InternalAPIKeyAuthenticatorTest method testAuthenticateNoCacheExpiredTokenTenant.
@Test
public void testAuthenticateNoCacheExpiredTokenTenant() throws Exception {
PowerMockito.when(GatewayUtils.isInternalKey(Mockito.any(JWTClaimsSet.class))).thenReturn(true);
InternalAPIKeyAuthenticator internalAPIKeyAuthenticator = new InternalAPIKeyAuthenticator(APIMgtGatewayConstants.INTERNAL_KEY);
MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
Mockito.when(messageContext.getProperty(RESTConstants.REST_API_CONTEXT)).thenReturn("/api1/1.0.0");
Mockito.when(messageContext.getProperty(RESTConstants.SYNAPSE_REST_API_VERSION)).thenReturn("1.0.0");
API api = new API();
PowerMockito.when(GatewayUtils.getAPI(messageContext)).thenReturn(api);
TreeMap transportHeaders = new TreeMap();
transportHeaders.put(APIMgtGatewayConstants.INTERNAL_KEY, internalKey);
org.apache.axis2.context.MessageContext axis2MsgCntxt = Mockito.mock(org.apache.axis2.context.MessageContext.class);
Mockito.when(axis2MsgCntxt.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS)).thenReturn(transportHeaders);
Mockito.when(((Axis2MessageContext) messageContext).getAxis2MessageContext()).thenReturn(axis2MsgCntxt);
Mockito.when(axis2MsgCntxt.getProperty(Constants.Configuration.HTTP_METHOD)).thenReturn("GET");
Mockito.when(messageContext.getProperty(APIConstants.API_ELECTED_RESOURCE)).thenReturn("/resource");
OpenAPI openAPI = Mockito.mock(OpenAPI.class);
Mockito.when(messageContext.getProperty(APIMgtGatewayConstants.OPEN_API_OBJECT)).thenReturn(openAPI);
PowerMockito.when(OpenAPIUtils.getResourceThrottlingTier(openAPI, messageContext)).thenReturn("GOLD");
PowerMockito.when(GatewayUtils.getTenantDomain()).thenReturn("abc.com");
Cache internalKeyCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getGatewayInternalKeyCache()).thenReturn(internalKeyCache);
Mockito.when(internalKeyCache.get("28f8d7b0-9e62-4341-bf17-094453d5ffa4")).thenReturn(null);
Cache internalKeyDataCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getGatewayInternalKeyDataCache()).thenReturn(internalKeyDataCache);
Cache invalidCache = Mockito.mock(Cache.class);
PowerMockito.when(CacheProvider.getInvalidGatewayInternalKeyCache()).thenReturn(invalidCache);
Mockito.when(invalidCache.get("28f8d7b0-9e62-4341-bf17-094453d5ffa4")).thenReturn(null);
JSONObject subscribedAPI = Mockito.mock(JSONObject.class);
PowerMockito.when(GatewayUtils.verifyTokenSignature(Mockito.any(SignedJWT.class), Mockito.anyString())).thenReturn(true);
PowerMockito.when(GatewayUtils.isJwtTokenExpired(signedJWT.getJWTClaimsSet())).thenReturn(true);
PowerMockito.when(GatewayUtils.validateAPISubscription("/api1/1.0.0", "1.0.0", signedJWT.getJWTClaimsSet(), internalKey.split("\\."), false)).thenReturn(subscribedAPI);
AuthenticationContext authenticationContext = Mockito.mock(AuthenticationContext.class);
PowerMockito.when(GatewayUtils.generateAuthenticationContext("28f8d7b0-9e62-4341-bf17-094453d5ffa4", signedJWT.getJWTClaimsSet(), subscribedAPI, api.getApiTier())).thenReturn(authenticationContext);
PowerMockito.doNothing().when(APISecurityUtils.class, "setAuthenticationContext", messageContext, authenticationContext);
PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
PowerMockito.doNothing().when(PrivilegedCarbonContext.class, "startTenantFlow");
PowerMockito.doNothing().when(privilegedCarbonContext).setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true);
AuthenticationResponse authenticate = internalAPIKeyAuthenticator.authenticate(messageContext);
Assert.assertNotNull(authenticate);
Assert.assertTrue(authenticate.isMandatoryAuthentication());
Assert.assertFalse(authenticate.isAuthenticated());
Assert.assertFalse(authenticate.isContinueToNextAuthenticator());
Assert.assertEquals(authenticate.getErrorCode(), APISecurityConstants.API_AUTH_INVALID_CREDENTIALS);
Assert.assertEquals(authenticate.getErrorMessage(), APISecurityConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
Mockito.verify(internalKeyCache, Mockito.times(1)).get("28f8d7b0-9e62-4341-bf17-094453d5ffa4");
Mockito.verify(invalidCache, Mockito.times(1)).get("28f8d7b0-9e62-4341-bf17-094453d5ffa4");
Mockito.verify(internalKeyCache, Mockito.times(0)).put("28f8d7b0-9e62-4341-bf17-094453d5ffa4", "abc.com");
Mockito.verify(invalidCache, Mockito.times(1)).put("28f8d7b0-9e62-4341-bf17-094453d5ffa4", "abc.com");
Mockito.verify(internalKeyDataCache, Mockito.times(0)).put(Mockito.anyString(), Mockito.any(AuthenticationContext.class));
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class APIThrottleHandlerTest method testMsgThrottleOutWhenApplicationLevelQuotaExceeded.
@Test
public void testMsgThrottleOutWhenApplicationLevelQuotaExceeded() throws XMLStreamException, ThrottleException {
concurrentAccessController = new ConcurrentAccessController(100);
configurationContext.setProperty(throttleKey, concurrentAccessController);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setApplicationTier("Silver");
((Axis2MessageContext) messageContext).getAxis2MessageContext().setConfigurationContext(configurationContext);
TestUtils.loadAPIThrottlingPolicyEntry(String.format(THROTTLING_POLICY_DEFINITION, "ROLE", "Silver", 0, 60000, "true"), THROTTLE_POLICY_KEY, true, 0, messageContext);
TestUtils.loadAPIThrottlingPolicyEntry(String.format(THROTTLING_POLICY_DEFINITION, "ROLE", "Silver", 0, 60000, "true"), THROTTLE_POLICY_RESOURCE_KEY, true, 0, messageContext);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
messageContext.setProperty(RESPONSE, "false");
messageContext.setProperty(APIConstants.VERB_INFO_DTO, verbInfoDTO);
apiThrottleHandler.setPolicyKey(THROTTLE_POLICY_KEY);
apiThrottleHandler.setPolicyKeyResource(THROTTLE_POLICY_RESOURCE_KEY);
apiThrottleHandler.setId(throttleID);
Mockito.when(throttleContext.getThrottleConfiguration()).thenReturn(throttleConfiguration);
Mockito.when(throttleConfiguration.getCallerConfiguration(Mockito.anyString())).thenReturn(callerConfiguration);
// Set application level access state to be ACCESS_DENIED
Mockito.when(callerConfiguration.getAccessState()).thenReturn(1);
Assert.assertFalse(apiThrottleHandler.handleRequest(messageContext));
}
use of org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext in project carbon-apimgt by wso2.
the class APIThrottleHandlerTest method testMsgThrottleOutWhenHardLevelQuotaExceededForProductionEndpoint.
@Test
public void testMsgThrottleOutWhenHardLevelQuotaExceededForProductionEndpoint() throws XMLStreamException, ThrottleException {
concurrentAccessController = new ConcurrentAccessController(100);
configurationContext.setProperty(throttleKey, concurrentAccessController);
AuthenticationContext authenticationContext = (AuthenticationContext) messageContext.getProperty(API_AUTH_CONTEXT);
authenticationContext.setKeyType(APIConstants.API_KEY_TYPE_PRODUCTION);
messageContext.setProperty(API_AUTH_CONTEXT, authenticationContext);
messageContext.setProperty(APIConstants.VERB_INFO_DTO, new VerbInfoDTO());
((Axis2MessageContext) messageContext).getAxis2MessageContext().setConfigurationContext(configurationContext);
TestUtils.loadAPIThrottlingPolicyEntry(String.format(THROTTLING_POLICY_DEFINITION, "ROLE", "Silver", 1, 60000, "true"), THROTTLE_POLICY_KEY, true, 0, messageContext);
TestUtils.loadAPIThrottlingPolicyEntry(String.format(THROTTLING_POLICY_DEFINITION, "ROLE", "Silver", 1, 60000, "true"), THROTTLE_POLICY_RESOURCE_KEY, true, 0, messageContext);
messageContext.setProperty(RESPONSE, "false");
apiThrottleHandler.setPolicyKey(THROTTLE_POLICY_KEY);
apiThrottleHandler.setPolicyKeyResource(THROTTLE_POLICY_RESOURCE_KEY);
apiThrottleHandler.setId(throttleID);
apiThrottleHandler.setProductionMaxCount(PRODUCTION_MAX_COUNT);
Mockito.when(throttleContext.getThrottleConfiguration()).thenReturn(throttleConfiguration);
Mockito.when(throttleConfiguration.getCallerConfiguration(Mockito.anyString())).thenReturn(callerConfiguration);
Mockito.when(callerConfiguration.getAccessState()).thenReturn(1);
Assert.assertFalse(apiThrottleHandler.handleRequest(messageContext));
}
Aggregations