Search in sources :

Example 31 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class OAS2Parser method getScopes.

/**
 * This method returns the oauth scopes according to the given swagger
 *
 * @param resourceConfigsJSON resource json
 * @return scope set
 * @throws APIManagementException
 */
@Override
public Set<Scope> getScopes(String resourceConfigsJSON) throws APIManagementException {
    Swagger swagger = getSwagger(resourceConfigsJSON);
    String oauth2SchemeKey = getOAuth2SecuritySchemeKey(swagger);
    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    OAuth2Definition oAuth2Definition;
    if (securityDefinitions != null && (oAuth2Definition = (OAuth2Definition) securityDefinitions.get(oauth2SchemeKey)) != null && oAuth2Definition.getScopes() != null) {
        Set<Scope> scopeSet = new LinkedHashSet<>();
        for (Map.Entry<String, String> entry : oAuth2Definition.getScopes().entrySet()) {
            Scope scope = new Scope();
            scope.setKey(entry.getKey());
            scope.setName(entry.getKey());
            scope.setDescription(entry.getValue());
            Map<String, String> scopeBindings;
            if (oAuth2Definition.getVendorExtensions() != null && (scopeBindings = (Map<String, String>) oAuth2Definition.getVendorExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS)) != null) {
                if (scopeBindings.get(scope.getKey()) != null) {
                    scope.setRoles(scopeBindings.get(scope.getKey()));
                }
            }
            scopeSet.add(scope);
        }
        return OASParserUtil.sortScopes(scopeSet);
    } else {
        return OASParserUtil.sortScopes(getScopesFromExtensions(swagger));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Scope(org.wso2.carbon.apimgt.api.model.Scope) Swagger(io.swagger.models.Swagger) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class OAS2Parser method updateSwaggerSecurityDefinition.

/**
 * Update swagger with security definition
 *
 * @param swagger     swagger object
 * @param swaggerData Swagger related data
 */
private void updateSwaggerSecurityDefinition(Swagger swagger, SwaggerData swaggerData, String authUrl) {
    OAuth2Definition oAuth2Definition = new OAuth2Definition().implicit(authUrl);
    Set<Scope> scopes = swaggerData.getScopes();
    if (scopes != null && !scopes.isEmpty()) {
        Map<String, String> scopeBindings = new HashMap<>();
        for (Scope scope : scopes) {
            String description = scope.getDescription() != null ? scope.getDescription() : "";
            oAuth2Definition.addScope(scope.getKey(), description);
            String roles = (StringUtils.isNotBlank(scope.getRoles()) && scope.getRoles().trim().split(",").length > 0) ? scope.getRoles() : StringUtils.EMPTY;
            scopeBindings.put(scope.getKey(), roles);
        }
        oAuth2Definition.setVendorExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
    }
    swagger.addSecurityDefinition(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY, oAuth2Definition);
    if (swagger.getSecurity() == null) {
        SecurityRequirement securityRequirement = new SecurityRequirement();
        securityRequirement.setRequirements(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY, new ArrayList<String>());
        swagger.addSecurity(securityRequirement);
    }
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecurityRequirement(io.swagger.models.SecurityRequirement)

Example 33 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class OAS2Parser method generateAPIDefinition.

/**
 * This method generates API definition using the given api's URI templates and the swagger.
 * It will alter the provided swagger definition based on the URI templates. For example: if there is a new
 * URI template which is not included in the swagger, it will be added to the swagger as a basic resource. Any
 * additional resources inside the swagger will be removed from the swagger. Changes to scopes, throtting policies,
 * on the resource will be updated on the swagger
 *
 * @param swaggerData api
 * @param swaggerObj  swagger
 * @return API definition in string format
 * @throws APIManagementException if error occurred when generating API Definition
 */
private String generateAPIDefinition(SwaggerData swaggerData, Swagger swaggerObj) throws APIManagementException {
    // Generates below model using the API's URI template
    // path -> [verb1 -> template1, verb2 -> template2, ..]
    Map<String, Map<String, SwaggerData.Resource>> resourceMap = getResourceMap(swaggerData);
    Iterator<Map.Entry<String, Path>> itr = swaggerObj.getPaths().entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Path> pathEntry = itr.next();
        String pathName = pathEntry.getKey();
        Path path = pathEntry.getValue();
        Map<String, SwaggerData.Resource> resourcesForPath = resourceMap.get(pathName);
        if (resourcesForPath == null) {
            // remove paths that are not in URI Templates
            itr.remove();
        } else {
            // If path is available in the URI template, then check for operations(verbs)
            for (Map.Entry<HttpMethod, Operation> operationEntry : path.getOperationMap().entrySet()) {
                HttpMethod httpMethod = operationEntry.getKey();
                Operation operation = operationEntry.getValue();
                SwaggerData.Resource resource = resourcesForPath.get(httpMethod.toString().toUpperCase());
                if (resource == null) {
                    // if particular operation is not available in URI templates, then remove it from swagger
                    path.set(httpMethod.toString().toLowerCase(), null);
                } else {
                    // if operation is available in URI templates, update swagger operation
                    // with auth type, scope etc
                    updateOperationManagedInfo(resource, operation);
                }
            }
            // if there are any verbs (operations) not defined in swagger then add them
            for (Map.Entry<String, SwaggerData.Resource> resourcesForPathEntry : resourcesForPath.entrySet()) {
                String verb = resourcesForPathEntry.getKey();
                SwaggerData.Resource resource = resourcesForPathEntry.getValue();
                HttpMethod method = HttpMethod.valueOf(verb.toUpperCase());
                Operation operation = path.getOperationMap().get(method);
                if (operation == null) {
                    operation = createOperation(resource);
                    path.set(resource.getVerb().toLowerCase(), operation);
                }
            }
        }
    }
    // add to swagger if there are any new templates
    for (Map.Entry<String, Map<String, SwaggerData.Resource>> resourceMapEntry : resourceMap.entrySet()) {
        String path = resourceMapEntry.getKey();
        Map<String, SwaggerData.Resource> verbMap = resourceMapEntry.getValue();
        if (swaggerObj.getPath(path) == null) {
            for (Map.Entry<String, SwaggerData.Resource> verbMapEntry : verbMap.entrySet()) {
                SwaggerData.Resource resource = verbMapEntry.getValue();
                addOrUpdatePathToSwagger(swaggerObj, resource);
            }
        }
    }
    updateSwaggerSecurityDefinition(swaggerObj, swaggerData, "https://test.com");
    updateLegacyScopesFromSwagger(swaggerObj, swaggerData);
    if (StringUtils.isEmpty(swaggerObj.getInfo().getTitle())) {
        swaggerObj.getInfo().setTitle(swaggerData.getTitle());
    }
    if (StringUtils.isEmpty(swaggerObj.getInfo().getVersion())) {
        swaggerObj.getInfo().setVersion(swaggerData.getVersion());
    }
    preserveResourcePathOrderFromAPI(swaggerData, swaggerObj);
    return getSwaggerJsonString(swaggerObj);
}
Also used : RefPath(io.swagger.models.RefPath) Path(io.swagger.models.Path) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) Operation(io.swagger.models.Operation) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod)

Example 34 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class OAS2Parser method setLegacyScopeExtensionToSwagger.

/**
 * Set scopes to the swagger extension
 *
 * @param swagger     swagger object
 * @param swaggerData Swagger API data
 */
private void setLegacyScopeExtensionToSwagger(Swagger swagger, SwaggerData swaggerData) {
    Set<Scope> scopes = swaggerData.getScopes();
    if (scopes != null && !scopes.isEmpty()) {
        List<Map<String, String>> xSecurityScopesArray = new ArrayList<>();
        for (Scope scope : scopes) {
            Map<String, String> xWso2ScopesObject = new LinkedHashMap<>();
            xWso2ScopesObject.put(APIConstants.SWAGGER_SCOPE_KEY, scope.getKey());
            xWso2ScopesObject.put(APIConstants.SWAGGER_NAME, scope.getName());
            xWso2ScopesObject.put(APIConstants.SWAGGER_ROLES, scope.getRoles());
            xWso2ScopesObject.put(APIConstants.SWAGGER_DESCRIPTION, scope.getDescription());
            xSecurityScopesArray.add(xWso2ScopesObject);
        }
        Map<String, Object> xWSO2Scopes = new LinkedHashMap<>();
        xWSO2Scopes.put(APIConstants.SWAGGER_X_WSO2_SCOPES, xSecurityScopesArray);
        Map<String, Object> xWSO2SecurityDefinitionObject = new LinkedHashMap<>();
        xWSO2SecurityDefinitionObject.put(APIConstants.SWAGGER_OBJECT_NAME_APIM, xWSO2Scopes);
        swagger.setVendorExtension(APIConstants.SWAGGER_X_WSO2_SECURITY, xWSO2SecurityDefinitionObject);
    }
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 35 with Scope

use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.

the class OAS2Parser method processOtherSchemeScopes.

/**
 * This method will inject scopes of other schemes to the swagger definition
 *
 * @param swaggerContent resource json
 * @return String
 * @throws APIManagementException
 */
@Override
public String processOtherSchemeScopes(String swaggerContent) throws APIManagementException {
    Swagger swagger = getSwagger(swaggerContent);
    Set<Scope> legacyScopes = getScopesFromExtensions(swagger);
    if (!isDefaultGiven(swaggerContent) && legacyScopes.isEmpty()) {
        swagger = injectOtherScopesToDefaultScheme(swagger);
        swagger = injectOtherResourceScopesToDefaultScheme(swagger);
        return getSwaggerJsonString(swagger);
    } else if (!legacyScopes.isEmpty()) {
        swagger = processLegacyScopes(swagger);
        return getSwaggerJsonString(swagger);
    }
    return swaggerContent;
}
Also used : Scope(org.wso2.carbon.apimgt.api.model.Scope) Swagger(io.swagger.models.Swagger)

Aggregations

Scope (org.wso2.carbon.apimgt.api.model.Scope)97 HashMap (java.util.HashMap)76 ArrayList (java.util.ArrayList)58 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)50 Scope (org.wso2.carbon.apimgt.core.models.Scope)41 Map (java.util.Map)39 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)39 LinkedHashSet (java.util.LinkedHashSet)32 LinkedHashMap (java.util.LinkedHashMap)29 HashSet (java.util.HashSet)26 RestVariable (org.wso2.carbon.bpmn.rest.engine.variable.RestVariable)25 List (java.util.List)24 Test (org.testng.annotations.Test)23 JSONObject (org.json.simple.JSONObject)22 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 PreparedStatement (java.sql.PreparedStatement)17 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)17 SQLException (java.sql.SQLException)16 Gson (com.google.gson.Gson)15 Connection (java.sql.Connection)15