use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.
the class OAS3Parser method setLegacyScopeExtensionToSwagger.
/**
* Set scopes to the openAPI extension
*
* @param openAPI OpenAPI object
* @param swaggerData Swagger API data
*/
private void setLegacyScopeExtensionToSwagger(OpenAPI openAPI, 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);
openAPI.addExtension(APIConstants.SWAGGER_X_WSO2_SECURITY, xWSO2SecurityDefinitionObject);
}
}
use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.
the class OAS3Parser 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 {
OpenAPI openAPI = getOpenAPI(swaggerContent);
Set<Scope> legacyScopes = getScopesFromExtensions(openAPI);
// This is to fix https://github.com/wso2/product-apim/issues/8724
if (isDefaultGiven(swaggerContent) && !legacyScopes.isEmpty()) {
SecurityScheme defaultScheme = openAPI.getComponents().getSecuritySchemes().get(OPENAPI_SECURITY_SCHEMA_KEY);
OAuthFlows oAuthFlows = defaultScheme.getFlows();
if (oAuthFlows != null) {
OAuthFlow oAuthFlow = oAuthFlows.getImplicit();
if (oAuthFlow != null) {
Scopes defaultScopes = oAuthFlow.getScopes();
if (defaultScopes != null) {
for (Scope legacyScope : legacyScopes) {
if (!defaultScopes.containsKey(legacyScope.getKey())) {
openAPI = processLegacyScopes(openAPI);
return Json.pretty(openAPI);
}
}
}
}
}
}
if (!isDefaultGiven(swaggerContent)) {
openAPI = processLegacyScopes(openAPI);
openAPI = injectOtherScopesToDefaultScheme(openAPI);
openAPI = injectOtherResourceScopesToDefaultScheme(openAPI);
return Json.pretty(openAPI);
}
return swaggerContent;
}
use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.
the class OAS3Parser method processLegacyScopes.
/**
* This method will extract scopes from legacy x-wso2-security and add them to default scheme
* @param openAPI openAPI definition
* @return
* @throws APIManagementException
*/
private OpenAPI processLegacyScopes(OpenAPI openAPI) throws APIManagementException {
Set<Scope> scopes = getScopesFromExtensions(openAPI);
if (!scopes.isEmpty()) {
if (openAPI.getComponents() == null) {
openAPI.setComponents(new Components());
}
Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
if (securitySchemes == null) {
securitySchemes = new HashMap<>();
openAPI.getComponents().setSecuritySchemes(securitySchemes);
}
SecurityScheme securityScheme = securitySchemes.get(OPENAPI_SECURITY_SCHEMA_KEY);
if (securityScheme == null) {
securityScheme = new SecurityScheme();
securityScheme.setType(SecurityScheme.Type.OAUTH2);
securitySchemes.put(OPENAPI_SECURITY_SCHEMA_KEY, securityScheme);
List<SecurityRequirement> security = new ArrayList<SecurityRequirement>();
SecurityRequirement secReq = new SecurityRequirement();
secReq.addList(OPENAPI_SECURITY_SCHEMA_KEY, new ArrayList<String>());
security.add(secReq);
openAPI.setSecurity(security);
}
if (securityScheme.getFlows() == null) {
securityScheme.setFlows(new OAuthFlows());
}
OAuthFlow oAuthFlow = securityScheme.getFlows().getImplicit();
if (oAuthFlow == null) {
oAuthFlow = new OAuthFlow();
securityScheme.getFlows().setImplicit(oAuthFlow);
}
oAuthFlow.setAuthorizationUrl(OPENAPI_DEFAULT_AUTHORIZATION_URL);
Scopes oas3Scopes = oAuthFlow.getScopes() != null ? oAuthFlow.getScopes() : new Scopes();
if (scopes != null && !scopes.isEmpty()) {
Map<String, String> scopeBindings = new HashMap<>();
if (oAuthFlow.getExtensions() != null) {
scopeBindings = (Map<String, String>) oAuthFlow.getExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS) != null ? (Map<String, String>) oAuthFlow.getExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS) : new HashMap<>();
}
for (Scope scope : scopes) {
oas3Scopes.put(scope.getKey(), scope.getDescription());
String roles = (StringUtils.isNotBlank(scope.getRoles()) && scope.getRoles().trim().split(",").length > 0) ? scope.getRoles() : StringUtils.EMPTY;
scopeBindings.put(scope.getKey(), roles);
}
oAuthFlow.addExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
}
oAuthFlow.setScopes(oas3Scopes);
}
return openAPI;
}
use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.
the class OAS3Parser method getScopesFromExtensions.
/**
* Get scope information from the extensions
*
* @param openAPI openAPI object
* @return Scope set
* @throws APIManagementException if an error occurred
*/
private Set<Scope> getScopesFromExtensions(OpenAPI openAPI) throws APIManagementException {
Set<Scope> scopeList = new LinkedHashSet<>();
Map<String, Object> extensions = openAPI.getExtensions();
if (extensions != null && extensions.containsKey(APIConstants.SWAGGER_X_WSO2_SECURITY)) {
Map<String, Object> securityDefinitions = (Map<String, Object>) extensions.get(APIConstants.SWAGGER_X_WSO2_SECURITY);
for (Map.Entry<String, Object> entry : securityDefinitions.entrySet()) {
Map<String, Object> securityDefinition = (Map<String, Object>) entry.getValue();
if (securityDefinition.containsKey(APIConstants.SWAGGER_X_WSO2_SCOPES)) {
List<Map<String, String>> oauthScope = (List<Map<String, String>>) securityDefinition.get(APIConstants.SWAGGER_X_WSO2_SCOPES);
for (Map<String, String> anOauthScope : oauthScope) {
Scope scope = new Scope();
scope.setKey(anOauthScope.get(APIConstants.SWAGGER_SCOPE_KEY));
scope.setName(anOauthScope.get(APIConstants.SWAGGER_NAME));
scope.setDescription(anOauthScope.get(APIConstants.SWAGGER_DESCRIPTION));
scope.setRoles(anOauthScope.get(APIConstants.SWAGGER_ROLES));
scopeList.add(scope);
}
}
}
}
return scopeList;
}
use of org.wso2.carbon.apimgt.api.model.Scope in project carbon-apimgt by wso2.
the class OASParserUtil method setScopesToTemplate.
/**
* Sets the scopes to the URL template object using the given list of scopes
*
* @param template URL template
* @param resourceScopes list of scopes of the resource
* @param apiScopes set of scopes defined for the API
* @return URL template after setting the scopes
*/
public static URITemplate setScopesToTemplate(URITemplate template, List<String> resourceScopes, Set<Scope> apiScopes) throws APIManagementException {
for (String scopeName : resourceScopes) {
if (StringUtils.isNotBlank(scopeName)) {
Scope scope = APIUtil.findScopeByKey(apiScopes, scopeName);
if (scope == null) {
throw new APIManagementException("Resource Scope '" + scopeName + "' not found.");
}
template.setScopes(scope);
}
}
return template;
}
Aggregations