use of io.swagger.v3.oas.annotations.security.SecurityRequirement in project carbon-apimgt by wso2.
the class OAS3Parser method getOASDefinitionForPublisher.
/**
* Update OAS definition for API Publisher
*
* @param api API
* @param oasDefinition
* @return OAS definition
* @throws APIManagementException throws if an error occurred
*/
@Override
public String getOASDefinitionForPublisher(API api, String oasDefinition) throws APIManagementException {
OpenAPI openAPI = getOpenAPI(oasDefinition);
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());
}
// setting scopes id if it is null
// https://github.com/swagger-api/swagger-parser/issues/1202
OAuthFlow oAuthFlow = securityScheme.getFlows().getImplicit();
if (oAuthFlow == null) {
oAuthFlow = new OAuthFlow();
securityScheme.getFlows().setImplicit(oAuthFlow);
}
if (oAuthFlow.getScopes() == null) {
oAuthFlow.setScopes(new Scopes());
}
oAuthFlow.setAuthorizationUrl(OPENAPI_DEFAULT_AUTHORIZATION_URL);
if (api.getAuthorizationHeader() != null) {
openAPI.addExtension(APIConstants.X_WSO2_AUTH_HEADER, api.getAuthorizationHeader());
}
if (api.getApiLevelPolicy() != null) {
openAPI.addExtension(APIConstants.X_THROTTLING_TIER, api.getApiLevelPolicy());
}
openAPI.addExtension(APIConstants.X_WSO2_CORS, api.getCorsConfiguration());
Object prodEndpointObj = OASParserUtil.generateOASConfigForEndpoints(api, true);
if (prodEndpointObj != null) {
openAPI.addExtension(APIConstants.X_WSO2_PRODUCTION_ENDPOINTS, prodEndpointObj);
}
Object sandEndpointObj = OASParserUtil.generateOASConfigForEndpoints(api, false);
if (sandEndpointObj != null) {
openAPI.addExtension(APIConstants.X_WSO2_SANDBOX_ENDPOINTS, sandEndpointObj);
}
openAPI.addExtension(APIConstants.X_WSO2_BASEPATH, api.getContext());
if (api.getTransports() != null) {
openAPI.addExtension(APIConstants.X_WSO2_TRANSPORTS, api.getTransports().split(","));
}
String apiSecurity = api.getApiSecurity();
// set mutual ssl extension if enabled
if (apiSecurity != null) {
List<String> securityList = Arrays.asList(apiSecurity.split(","));
if (securityList.contains(APIConstants.API_SECURITY_MUTUAL_SSL)) {
String mutualSSLOptional = !securityList.contains(APIConstants.API_SECURITY_MUTUAL_SSL_MANDATORY) ? APIConstants.OPTIONAL : APIConstants.MANDATORY;
openAPI.addExtension(APIConstants.X_WSO2_MUTUAL_SSL, mutualSSLOptional);
}
}
// This app security is should given in resource level,
// otherwise the default oauth2 scheme defined at each resouce level will override application securities
JsonNode appSecurityExtension = OASParserUtil.getAppSecurity(apiSecurity);
for (String pathKey : openAPI.getPaths().keySet()) {
PathItem pathItem = openAPI.getPaths().get(pathKey);
for (Map.Entry<PathItem.HttpMethod, Operation> entry : pathItem.readOperationsMap().entrySet()) {
Operation operation = entry.getValue();
operation.addExtension(APIConstants.X_WSO2_APP_SECURITY, appSecurityExtension);
}
}
openAPI.addExtension(APIConstants.X_WSO2_RESPONSE_CACHE, OASParserUtil.getResponseCacheConfig(api.getResponseCache(), api.getCacheTimeout()));
return Json.pretty(openAPI);
}
use of io.swagger.v3.oas.annotations.security.SecurityRequirement 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 io.swagger.v3.oas.annotations.security.SecurityRequirement in project carbon-apimgt by wso2.
the class OAS3Parser method updateSwaggerSecurityDefinition.
/**
* Include Scope details to the definition
*
* @param openAPI openapi definition
* @param swaggerData Swagger related API data
*/
private void updateSwaggerSecurityDefinition(OpenAPI openAPI, SwaggerData swaggerData, String authUrl) {
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(authUrl);
Scopes oas3Scopes = new Scopes();
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() : "";
oas3Scopes.put(scope.getKey(), description);
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);
}
use of io.swagger.v3.oas.annotations.security.SecurityRequirement in project flow by vaadin.
the class OpenAPIObjectGenerator method createPostOperation.
private Operation createPostOperation(MethodDeclaration methodDeclaration) {
Operation post = new Operation();
SecurityRequirement securityItem = new SecurityRequirement();
securityItem.addList(VAADIN_CONNECT_OAUTH2_SECURITY_SCHEME);
post.addSecurityItem(securityItem);
methodDeclaration.getJavadoc().ifPresent(javadoc -> post.setDescription(javadoc.getDescription().toText()));
return post;
}
use of io.swagger.v3.oas.annotations.security.SecurityRequirement in project dockstore by dockstore.
the class AliasResource method getWorkflowVersionPathInfoByAlias.
@GET
@Timed
@UnitOfWork(readOnly = true)
@Path("workflow-versions/{alias}")
@Operation(operationId = "getWorkflowVersionPathInfoByAlias", description = "Retrieves workflow version path information by alias.", security = @SecurityRequirement(name = OPENAPI_JWT_SECURITY_DEFINITION_NAME))
@ApiOperation(value = "Retrieves workflow version path information by alias.", notes = OPTIONAL_AUTH_MESSAGE, response = WorkflowVersion.WorkflowVersionPathInfo.class, authorizations = { @Authorization(value = JWT_SECURITY_DEFINITION_NAME) })
public WorkflowVersion.WorkflowVersionPathInfo getWorkflowVersionPathInfoByAlias(@ApiParam(hidden = true) @Parameter(hidden = true, name = "user") @Auth Optional<User> user, @ApiParam(value = "Alias", required = true) @PathParam("alias") String alias) {
final WorkflowVersion workflowVersion = this.workflowVersionDAO.findByAlias(alias);
if (workflowVersion == null) {
LOG.error("Could not find workflow version using the alias: " + alias);
throw new CustomWebApplicationException("Workflow version not found when searching with alias: " + alias, HttpStatus.SC_BAD_REQUEST);
}
long workflowVersionId = workflowVersion.getId();
Workflow workflow = AliasHelper.getWorkflow(workflowDAO, workflowVersionId);
workflowResource.optionalUserCheckEntry(user, workflow);
return new WorkflowVersion.WorkflowVersionPathInfo(workflow.getWorkflowPath(), workflowVersion.getName());
}
Aggregations