Search in sources :

Example 6 with SecuritySchemeDefinition

use of io.swagger.models.auth.SecuritySchemeDefinition in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method addScopeToSwaggerDefinition.

@Override
public String addScopeToSwaggerDefinition(String resourceConfigJSON, Scope scope) {
    KeyMgtConfigurations keyManagerConfigs = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getKeyManagerConfigs();
    SwaggerParser swaggerParser = new SwaggerParser();
    Swagger swagger = swaggerParser.parse(resourceConfigJSON);
    Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = swagger.getSecurityDefinitions();
    if (securitySchemeDefinitionMap != null && !securitySchemeDefinitionMap.isEmpty() && securitySchemeDefinitionMap.containsKey(APIMgtConstants.OAUTH2SECURITY)) {
        OAuth2Definition oAuth2Definition = (OAuth2Definition) securitySchemeDefinitionMap.get(APIMgtConstants.OAUTH2SECURITY);
        // Removing Scope from Swagger SecurityDefinition
        Map<String, String> scopeMap = oAuth2Definition.getScopes();
        if (scopeMap != null) {
            scopeMap.put(scope.getName(), scope.getDescription());
        }
    } else {
        OAuth2Definition oAuth2Definition = new OAuth2Definition();
        oAuth2Definition.setType("oauth2");
        oAuth2Definition.setFlow("password");
        oAuth2Definition.setTokenUrl(keyManagerConfigs.getTokenEndpoint());
        Map<String, String> scopes = new HashMap<>();
        scopes.put(scope.getName(), scope.getDescription());
        oAuth2Definition.setScopes(scopes);
        if (securitySchemeDefinitionMap != null) {
            securitySchemeDefinitionMap.put(APIMgtConstants.OAUTH2SECURITY, oAuth2Definition);
        } else {
            securitySchemeDefinitionMap = new HashMap<>();
            securitySchemeDefinitionMap.put(APIMgtConstants.OAUTH2SECURITY, oAuth2Definition);
            swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
        }
    }
    return Json.pretty(swagger);
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) 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)

Example 7 with SecuritySchemeDefinition

use of io.swagger.models.auth.SecuritySchemeDefinition 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)

Aggregations

SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)7 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)6 Swagger (io.swagger.models.Swagger)5 SwaggerParser (io.swagger.parser.SwaggerParser)5 HashMap (java.util.HashMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Map (java.util.Map)3 BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Operation (io.swagger.models.Operation)1 Path (io.swagger.models.Path)1 SecurityRequirement (io.swagger.models.SecurityRequirement)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.testng.annotations.Test)1 KeyMgtConfigurations (org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations)1 Scope (org.wso2.carbon.apimgt.core.models.Scope)1