Search in sources :

Example 1 with DefaultKeyValidationHandler

use of org.wso2.carbon.apimgt.keymgt.handlers.DefaultKeyValidationHandler in project carbon-apimgt by wso2.

the class DefaultKeyValidationHandlerTest method testValidateScopes.

@Test
public void testValidateScopes() throws APIKeyMgtException {
    API api = new API();
    api.setApiId(1);
    api.setApiProvider(USER_NAME);
    api.setApiName(API_NAME);
    api.setApiVersion(API_VERSION);
    api.setContext(API_CONTEXT);
    URLMapping urlMapping = new URLMapping();
    urlMapping.addScope(SCOPES);
    urlMapping.setHttpMethod(HTTP_VERB);
    urlMapping.setUrlPattern(RESOURCE);
    api.addResource(urlMapping);
    Map<String, API> apiMap = new HashMap<>();
    String key = API_CONTEXT + ":" + API_VERSION;
    apiMap.put(key, api);
    APIKeyValidationInfoDTO dto = new APIKeyValidationInfoDTO();
    dto.setSubscriber(SUBSCRIBER);
    dto.setApplicationName(APPLICATION_NAME);
    dto.setApplicationId(APPLICATION_ID);
    dto.setApplicationTier(TIER);
    Set<String> scopeSet = new HashSet<>();
    scopeSet.add(SCOPES);
    dto.setScopes(scopeSet);
    dto.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    dto.setUserType(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION);
    // TokenValidationContext for non default API
    TokenValidationContext param1 = new TokenValidationContext();
    param1.setValidationInfoDTO(dto);
    param1.setContext(API_CONTEXT);
    param1.setVersion(API_VERSION);
    param1.setAccessToken(ACCESS_TOKEN);
    param1.setMatchingResource(RESOURCE);
    param1.setHttpVerb(HTTP_VERB);
    // TokenValidationContext for default API version
    TokenValidationContext param2 = new TokenValidationContext();
    param2.setValidationInfoDTO(dto);
    param2.setContext(API_CONTEXT);
    param2.setVersion(DEFAULT_API_VERSION);
    param2.setAccessToken(ACCESS_TOKEN);
    param2.setMatchingResource(RESOURCE);
    param2.setHttpVerb(HTTP_VERB);
    Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore(eq(TENANT_DOMAIN))).thenReturn(tenantSubscriptionStore);
    Mockito.when(tenantSubscriptionStore.getApiByContextAndVersion(eq(API_CONTEXT), eq(API_VERSION))).thenReturn(api);
    DefaultKeyValidationHandler defaultKeyValidationHandler = new DefaultKeyValidationHandler();
    boolean isScopeValidated = defaultKeyValidationHandler.validateScopes(param1);
    boolean isScopeValidated_default = defaultKeyValidationHandler.validateScopes(param2);
    Assert.assertTrue("Scope validation fails for API " + API_NAME, isScopeValidated);
    Assert.assertTrue("Scope validation fails for default API " + API_NAME, isScopeValidated_default);
}
Also used : URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) TokenValidationContext(org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext) HashMap(java.util.HashMap) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with DefaultKeyValidationHandler

use of org.wso2.carbon.apimgt.keymgt.handlers.DefaultKeyValidationHandler in project carbon-apimgt by wso2.

the class DefaultKeyValidationHandlerTest method testInvalidSubscription.

@Test
public void testInvalidSubscription() throws APIKeyMgtException {
    DefaultKeyValidationHandler defaultKeyValidationHandler = new DefaultKeyValidationHandler();
    API api = new API();
    api.setApiId(1);
    api.setApiProvider(USER_NAME);
    api.setApiName(API_NAME);
    api.setApiVersion(API_VERSION);
    api.setContext(API_CONTEXT);
    URLMapping urlMapping = new URLMapping();
    urlMapping.addScope(SCOPES);
    urlMapping.setHttpMethod(HTTP_VERB);
    urlMapping.setUrlPattern(RESOURCE);
    api.addResource(urlMapping);
    Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore(eq(TENANT_DOMAIN))).thenReturn(tenantSubscriptionStore);
    Mockito.when(tenantSubscriptionStore.getApiByContextAndVersion(eq(API_CONTEXT), eq(API_VERSION))).thenReturn(api);
    APIKeyValidationInfoDTO info = defaultKeyValidationHandler.validateSubscription(API_CONTEXT, API_VERSION, "xxxxxx", "default");
    Assert.assertEquals("Invalid error message status code ", APIConstants.KeyValidationStatus.API_AUTH_RESOURCE_FORBIDDEN, info.getValidationStatus());
}
Also used : URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with DefaultKeyValidationHandler

use of org.wso2.carbon.apimgt.keymgt.handlers.DefaultKeyValidationHandler in project carbon-apimgt by wso2.

the class ServiceReferenceHolder method getKeyValidationHandler.

public KeyValidationHandler getKeyValidationHandler(String tenantDomain) {
    if (keyValidationHandlerMap.containsKey(tenantDomain)) {
        return keyValidationHandlerMap.get(tenantDomain);
    }
    KeyValidationHandler keyValidationHandler = null;
    String className = amConfigurationService.getAPIManagerConfiguration().getFirstProperty(APIConstants.KEY_VALIDATION_HANDLER_CLASSNAME);
    try {
        if (StringUtils.isNotEmpty(className)) {
            keyValidationHandler = (KeyValidationHandler) APIUtil.getClassInstance(className);
        } else {
            keyValidationHandler = new DefaultKeyValidationHandler();
        }
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
        log.error("Key validation handler object creation error", e);
    }
    keyValidationHandlerMap.put(tenantDomain, keyValidationHandler);
    return keyValidationHandler;
}
Also used : KeyValidationHandler(org.wso2.carbon.apimgt.keymgt.handlers.KeyValidationHandler) DefaultKeyValidationHandler(org.wso2.carbon.apimgt.keymgt.handlers.DefaultKeyValidationHandler) DefaultKeyValidationHandler(org.wso2.carbon.apimgt.keymgt.handlers.DefaultKeyValidationHandler)

Aggregations

Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 URLMapping (org.wso2.carbon.apimgt.api.model.subscription.URLMapping)2 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)2 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 DefaultKeyValidationHandler (org.wso2.carbon.apimgt.keymgt.handlers.DefaultKeyValidationHandler)1 KeyValidationHandler (org.wso2.carbon.apimgt.keymgt.handlers.KeyValidationHandler)1 TokenValidationContext (org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext)1