Java Code Examples for io.swagger.models.Swagger#addSecurityDefinition()

The following examples show how to use io.swagger.models.Swagger#addSecurityDefinition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Update swagger with security definition
 *
 * @param swagger     swagger object
 * @param swaggerData Swagger related data
 */
private void updateSwaggerSecurityDefinition(Swagger swagger, SwaggerData swaggerData, String authUrl) {
    OAuth2Definition oAuth2Definition = new OAuth2Definition().implicit(authUrl);
    Set<Scope> scopes = swaggerData.getScopes();
    if (scopes != null && !scopes.isEmpty()) {
        Map<String, String> scopeBindings = new HashMap<>();
        for (Scope scope : scopes) {
            oAuth2Definition.addScope(scope.getKey(), scope.getDescription());
            scopeBindings.put(scope.getKey(), scope.getRoles());
        }
        oAuth2Definition.setVendorExtension(APIConstants.SWAGGER_X_SCOPES_BINDINGS, scopeBindings);
    }
    swagger.addSecurityDefinition(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY, oAuth2Definition);
    if (swagger.getSecurity() == null) {
        SecurityRequirement securityRequirement = new SecurityRequirement();
        securityRequirement.setRequirements(APIConstants.SWAGGER_APIM_DEFAULT_SECURITY, new ArrayList<String>());
        swagger.addSecurity(securityRequirement);
    }
}
 
Example 2
Source File: LogSearchDocumentationGenerator.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
private static String generateSwaggerYaml() throws Exception {
  ApiDocConfig apiDocConfig = new ApiDocConfig();
  BeanConfig beanConfig = apiDocConfig.swaggerConfig();
  Swagger swagger = beanConfig.getSwagger();
  swagger.addSecurityDefinition("basicAuth", new BasicAuthDefinition());
  beanConfig.configure(swagger);
  beanConfig.scanAndRead();
  return Yaml.mapper().writeValueAsString(swagger);
}
 
Example 3
Source File: ApiDocStorage.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@PostConstruct
private void postConstruct() {
  Thread loadApiDocThread = new Thread("load_swagger_api_doc") {
    @Override
    public void run() {
      logger.info("Start thread to scan REST API doc from endpoints.");
      Swagger swagger = beanConfig.getSwagger();
      swagger.addSecurityDefinition("basicAuth", new BasicAuthDefinition());
      beanConfig.configure(swagger);
      beanConfig.scanAndRead();
      setSwagger(swagger);
      try {
        String yaml = Yaml.mapper().writeValueAsString(swagger);
        StringBuilder b = new StringBuilder();
        String[] parts = yaml.split("\n");
        for (String part : parts) {
          b.append(part);
          b.append("\n");
        }
        setSwaggerYaml(b.toString());
      } catch (Exception e) {
        e.printStackTrace();
      }
      logger.info("Scanning REST API endpoints and generating docs has been successful.");
    }
  };
  loadApiDocThread.setDaemon(true);
  loadApiDocThread.start();
}
 
Example 4
Source File: GraviteeApiDefinition.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Override
public void afterScan(Reader reader, Swagger swagger) {
    swagger.addSecurityDefinition(TOKEN_AUTH_SCHEME, new BasicAuthDefinition());

    swagger.getPaths().values()
            .stream()
            .forEach(
                    path -> path.getOperations()
                            .stream()
                            .forEach(
                                    operation -> operation.addSecurity(GraviteeApiDefinition.TOKEN_AUTH_SCHEME, null)));
}
 
Example 5
Source File: SwaggerGenMojo.java    From herd with Apache License 2.0 4 votes vote down vote up
/**
 * Gets a new Swagger metadata.
 *
 * @return the Swagger metadata.
 * @throws MojoExecutionException if any problems were encountered.
 */
private Swagger getSwagger() throws MojoExecutionException
{
    getLog().debug("Creating Swagger Metadata");
    // Set up initial Swagger metadata.
    Swagger swagger = new Swagger();
    swagger.setInfo(new Info().title(title).version(version));
    swagger.setBasePath(basePath);

    // Set the schemes.
    if (!CollectionUtils.isEmpty(schemeParameters))
    {
        List<Scheme> schemes = new ArrayList<>();
        for (String schemeParameter : schemeParameters)
        {
            Scheme scheme = Scheme.forValue(schemeParameter);
            if (scheme == null)
            {
                throw new MojoExecutionException("Invalid scheme specified: " + schemeParameter);
            }
            schemes.add(scheme);
        }
        swagger.setSchemes(schemes);
    }

    // Add authorization support if specified.
    if (authType != null)
    {
        // Find the definition for the user specified type.
        SecuritySchemeDefinition securitySchemeDefinition = null;
        for (SecuritySchemeDefinition possibleDefinition : SECURITY_SCHEME_DEFINITIONS)
        {
            if (possibleDefinition.getType().equalsIgnoreCase(authType))
            {
                securitySchemeDefinition = possibleDefinition;
                break;
            }
        }

        // If we found a match, set it on the swagger object.
        if (securitySchemeDefinition != null)
        {
            // Come up with an authentication name for easy identification (e.g. basicAuthentication, etc.).
            String securityName = securitySchemeDefinition.getType() + "Authentication";

            // Add the security definition.
            swagger.addSecurityDefinition(securityName, securitySchemeDefinition);

            // Add the security for everything based on the name of the definition.
            SecurityRequirement securityRequirement = new SecurityRequirement();
            securityRequirement.requirement(securityName);
            swagger.addSecurity(securityRequirement);
        }
    }

    // Use default paths and definitions.
    swagger.setPaths(new TreeMap<>());
    swagger.setDefinitions(new TreeMap<>());
    return swagger;
}