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