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