Search in sources :

Example 1 with SecurityRequirement

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);
}
Also used : OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) Operation(io.swagger.v3.oas.models.Operation) Components(io.swagger.v3.oas.models.Components) PathItem(io.swagger.v3.oas.models.PathItem) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) JSONObject(org.json.simple.JSONObject) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HttpMethod(io.swagger.models.HttpMethod) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 2 with SecurityRequirement

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;
}
Also used : OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Components(io.swagger.v3.oas.models.Components) Scope(org.wso2.carbon.apimgt.api.model.Scope) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 3 with SecurityRequirement

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);
}
Also used : OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Components(io.swagger.v3.oas.models.Components) Scope(org.wso2.carbon.apimgt.api.model.Scope) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 4 with SecurityRequirement

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;
}
Also used : Operation(io.swagger.v3.oas.models.Operation) SecurityRequirement(io.swagger.v3.oas.models.security.SecurityRequirement)

Example 5 with SecurityRequirement

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());
}
Also used : Workflow(io.dockstore.webservice.core.Workflow) CustomWebApplicationException(io.dockstore.webservice.CustomWebApplicationException) WorkflowVersion(io.dockstore.webservice.core.WorkflowVersion) Path(javax.ws.rs.Path) UnitOfWork(io.dropwizard.hibernate.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiOperation(io.swagger.annotations.ApiOperation) Operation(io.swagger.v3.oas.annotations.Operation)

Aggregations

Operation (io.swagger.v3.oas.annotations.Operation)237 Path (javax.ws.rs.Path)146 Timed (com.codahale.metrics.annotation.Timed)125 UnitOfWork (io.dropwizard.hibernate.UnitOfWork)120 ApiOperation (io.swagger.annotations.ApiOperation)120 GET (javax.ws.rs.GET)75 CustomWebApplicationException (io.dockstore.webservice.CustomWebApplicationException)65 SecurityRequirement (io.swagger.v3.oas.models.security.SecurityRequirement)64 SecurityRequirement (io.swagger.v3.oas.annotations.security.SecurityRequirement)56 ResponseEntity (org.springframework.http.ResponseEntity)53 User (io.dockstore.webservice.core.User)48 POST (javax.ws.rs.POST)44 RolesAllowed (javax.annotation.security.RolesAllowed)41 ArrayList (java.util.ArrayList)40 BioWorkflow (io.dockstore.webservice.core.BioWorkflow)39 Workflow (io.dockstore.webservice.core.Workflow)39 PUT (javax.ws.rs.PUT)39 OpenAPI (io.swagger.v3.oas.models.OpenAPI)38 MediaType (org.springframework.http.MediaType)38 OrganizationUser (io.dockstore.webservice.core.OrganizationUser)36