io.swagger.models.SecurityRequirement Java Examples

The following examples show how to use io.swagger.models.SecurityRequirement. 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: SwaggerRouter.java    From vertx-swagger with Apache License 2.0 6 votes vote down vote up
private static AuthHandler getAuthHandler(SwaggerAuthHandlerFactory authHandlerFactory, Swagger swagger, Operation operation) {
    AuthHandler authHandler = null;
    if(authHandlerFactory != null) {
        if(operation.getSecurity() != null) {
        	if(!operation.getSecurity().isEmpty()) {
        		authHandler = authHandlerFactory.createAuthHandler(operation.getSecurity());
        	}
        } else if(swagger.getSecurity() != null && !swagger.getSecurity().isEmpty()) {
            List<Map<String, List<String>>> security = swagger.getSecurity().stream()
                    .map(SecurityRequirement::getRequirements)
                    .collect(Collectors.toList());
            authHandler = authHandlerFactory.createAuthHandler(security);
        }
    }

    return authHandler;
}
 
Example #2
Source File: AuthBuilder.java    From api-compiler with Apache License 2.0 6 votes vote down vote up
public void addSecurityRequirementForEntireService(
    Service.Builder serviceBuilder, Iterable<SecurityRequirement> securityRequirements) {
  if (securityRequirements == null) {
    requiresApiKeyAtTopLevel = false;
  } else {
    requiresApiKeyAtTopLevel =
        isApiKeyRequired(
            Iterables.transform(securityRequirements, SecurityRequirementsExtractor.INSTANCE),
            false,
            apiKeyDefinitions);
  }
  Usage.Builder usageBuilder = serviceBuilder.getUsageBuilder();
  usageBuilder.addRules(
      UsageRule.newBuilder()
          .setSelector("*")
          .setAllowUnregisteredCalls(!requiresApiKeyAtTopLevel)
          .build());
}
 
Example #3
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 #4
Source File: SwaggerFactory.java    From dorado with Apache License 2.0 4 votes vote down vote up
public static Swagger getSwagger() {
	if (!swaggerEnable)
		return new Swagger();

	if (swagger != null)
		return swagger;

	Reader reader = new Reader(new Swagger());

	String[] packages = null;
	Class<?> mainClass = Dorado.mainClass;
	EnableSwagger enableSwagger = mainClass.getAnnotation(EnableSwagger.class);

	if (enableSwagger != null) {
		packages = enableSwagger.value();
	}

	if (packages == null || packages.length == 0) {
		packages = Dorado.serverConfig.scanPackages();
	}

	if (packages == null || packages.length == 0) {
		packages = new String[] { mainClass.getPackage().getName() };
	}

	if (packages == null || packages.length == 0) {
		throw new IllegalArgumentException("缺少scanPackages设置");
	}

	Set<Class<?>> classes = new HashSet<>();
	for (String pkg : packages) {
		try {
			classes.addAll(PackageScanner.scan(pkg));
		} catch (Exception ex) {
			// ignore this ex
		}
	}

	Swagger _swagger = reader.read(classes);
	_swagger.setSchemes(Arrays.asList(Scheme.HTTP, Scheme.HTTPS));

	ApiKey apiKey = apiContext.getApiKey();
	if (apiKey != null) {
		ApiKeyAuthDefinition apiKeyAuth = new ApiKeyAuthDefinition(apiKey.getName(),
				In.forValue(apiKey.getIn() == null ? "header" : apiKey.getIn()));
		_swagger.securityDefinition("auth", apiKeyAuth);

		List<SecurityRequirement> securityRequirements = new ArrayList<>();
		SecurityRequirement sr = new SecurityRequirement();
		sr.requirement("auth");
		securityRequirements.add(sr);
		_swagger.setSecurity(securityRequirements);
	}
	if (apiContext.getInfo() != null)
		_swagger.setInfo(apiContext.getInfo());

	swagger = _swagger;
	return _swagger;
}
 
Example #5
Source File: AuthBuilder.java    From api-compiler with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, List<String>> apply(SecurityRequirement reqs) {
  return reqs.getRequirements();
}
 
Example #6
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;
}