Search in sources :

Example 76 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method getScopes.

@Override
public Map<String, Scope> getScopes(String resourceConfigsJSON) throws APIManagementException {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigsJSON);
    if (swagger.getVendorExtensions() != null) {
        String basePath = swagger.getBasePath();
        String nameSpace = getNamespaceFromBasePath(basePath);
        if (nameSpace == null) {
            return new HashMap<>();
        }
        String securityHeaderScopes = null;
        // read security header from deployment.yaml
        if (localConfigMap.containsKey(nameSpace)) {
            if (localConfigMap.get(nameSpace).containsKey(APIMgtConstants.SWAGGER_X_WSO2_SCOPES)) {
                securityHeaderScopes = localConfigMap.get(nameSpace).get(APIMgtConstants.SWAGGER_X_WSO2_SCOPES).toString();
            }
        } else {
            // rest api resource to scope mapping configurations have not been loaded.hence, populating
            populateConfigMapForScopes(swagger, nameSpace);
        }
        if (securityHeaderScopes == null || StringUtils.isEmpty(securityHeaderScopes)) {
            // security header is not found in deployment.yaml.hence, reading from swagger
            securityHeaderScopes = new Gson().toJson(swagger.getVendorExtensions().get(APIMgtConstants.SWAGGER_X_WSO2_SECURITY));
            localConfigMap.get(nameSpace).put(APIMgtConstants.SWAGGER_X_WSO2_SCOPES, securityHeaderScopes);
        }
        try {
            JSONObject scopesJson = (JSONObject) new JSONParser().parse(securityHeaderScopes);
            return extractScopesFromJson(scopesJson);
        } catch (ParseException e) {
            String msg = "invalid json : " + securityHeaderScopes;
            log.error(msg, e);
            throw new APIManagementException(msg, ExceptionCodes.SWAGGER_PARSE_EXCEPTION);
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("vendor extensions are not found in provided swagger json. resourceConfigsJSON = " + resourceConfigsJSON);
        }
        return new HashMap<>();
    }
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) Gson(com.google.gson.Gson) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Example 77 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method getScopesFromSecurityDefinitionForWebApps.

@Override
public Map<String, Scope> getScopesFromSecurityDefinitionForWebApps(String resourceConfigJSON) throws APIManagementException {
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJSON);
    String basePath = swagger.getBasePath();
    String nameSpace = getNamespaceFromBasePath(basePath);
    Map<String, String> scopes;
    if (nameSpace == null) {
        return new HashMap<>();
    }
    if (localConfigMap.containsKey(nameSpace)) {
        if (localConfigMap.get(nameSpace).containsKey(APIMgtConstants.SCOPES)) {
            return (Map<String, Scope>) localConfigMap.get(nameSpace).get(APIMgtConstants.SCOPES);
        }
    } else {
        populateConfigMapForScope(swagger, nameSpace);
    }
    // security header is not found in deployment.yaml.hence, reading from swagger
    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    if (securityDefinitions != null) {
        OAuth2Definition securityDefinition = (OAuth2Definition) securityDefinitions.get(APIMgtConstants.OAUTH2SECURITY);
        if (securityDefinition != null) {
            scopes = securityDefinition.getScopes();
            // populate Scope object map using oAuth2securityDefinitions
            Map<String, Scope> scopeMap = populateScopeMap(scopes);
            localConfigMap.get(nameSpace).put(APIMgtConstants.SCOPES, scopeMap);
            log.debug("Scopes of extracted from Swagger: {}", scopeMap);
            return scopeMap;
        }
    }
    return new HashMap<>();
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) Scope(org.wso2.carbon.apimgt.core.models.Scope) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 78 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testSaveSwagger20Definition.

@Test(description = "Save swagger definition for API")
public void testSaveSwagger20Definition() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, gatewaySourceGenerator, gateway, keyManager);
    Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
    String oldSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
    Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
    Mockito.when(keyManager.retrieveScope("apim:api_create")).thenReturn(new Scope("apim:api_create", "Create " + "API"));
    Mockito.when(keyManager.retrieveScope("apim:api_delete")).thenReturn(new Scope("apim:api_delete", "Create " + "API"));
    apiPublisher.saveSwagger20Definition(uuid, SampleTestObjectCreator.apiDefinition);
    Mockito.verify(apiDAO, Mockito.times(1)).updateApiDefinition(uuid, SampleTestObjectCreator.apiDefinition, USER);
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Example 79 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testGetScopeInformationFromApi.

@Test
public void testGetScopeInformationFromApi() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, keyManager);
    String newSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Mockito.when(apiDAO.getApiSwaggerDefinition("abcd")).thenReturn(newSwagger);
    Scope scope = new Scope("apim:api_create", "apim:api_create");
    Mockito.when(keyManager.retrieveScope("apim:api_create")).thenReturn(scope);
    Scope retrievedScope = apiPublisher.getScopeInformationOfApi("abcd", "apim:api_create");
    Assert.assertEquals(scope, retrievedScope);
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) Test(org.testng.annotations.Test)

Example 80 with Scope

use of org.wso2.ballerinalang.compiler.semantics.model.Scope in project carbon-apimgt by wso2.

the class APIPublisherImplTestCase method testUpdateScopeOfNonExistingScope.

@Test(description = "update existing Scope to API")
public void testUpdateScopeOfNonExistingScope() throws APIManagementException, IOException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    String uuid = api.getId();
    Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
    GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
    APIGateway gateway = Mockito.mock(APIGateway.class);
    IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, gatewaySourceGenerator, gateway, keyManager);
    String oldSwagger = IOUtils.toString(new FileInputStream("src" + File.separator + "test" + File.separator + "resources" + File.separator + "swagger" + File.separator + "swaggerWithAuthorization" + ".yaml"));
    Scope scope = new Scope("apim:api_delete", "apim:api_delete");
    Mockito.when(apiDAO.getApiSwaggerDefinition(uuid)).thenReturn(oldSwagger);
    try {
        apiPublisher.updateScopeOfTheApi(api.getId(), scope);
        Assert.fail();
    } catch (APIManagementException e) {
        Assert.assertTrue(e.getMessage().contains("Scope couldn't found by name: "));
    }
}
Also used : Scope(org.wso2.carbon.apimgt.core.models.Scope) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) API(org.wso2.carbon.apimgt.core.models.API) IdentityProvider(org.wso2.carbon.apimgt.core.api.IdentityProvider) APIGateway(org.wso2.carbon.apimgt.core.api.APIGateway) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) FileInputStream(java.io.FileInputStream) GatewaySourceGenerator(org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator) Test(org.testng.annotations.Test)

Aggregations

Scope (org.wso2.carbon.apimgt.core.models.Scope)41 HashMap (java.util.HashMap)25 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)25 Test (org.testng.annotations.Test)23 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 Response (javax.ws.rs.core.Response)16 ScopeInfo (org.wso2.carbon.apimgt.core.auth.dto.ScopeInfo)15 FileInputStream (java.io.FileInputStream)14 API (org.wso2.carbon.apimgt.core.models.API)14 ArrayList (java.util.ArrayList)13 KeyManager (org.wso2.carbon.apimgt.core.api.KeyManager)13 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)13 Map (java.util.Map)12 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)12 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)12 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)12 IdentityProvider (org.wso2.carbon.apimgt.core.api.IdentityProvider)11 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)11 ActivitiObjectNotFoundException (org.activiti.engine.ActivitiObjectNotFoundException)10 Scope (org.wso2.ballerinalang.compiler.semantics.model.Scope)10