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

The following examples show how to use io.swagger.models.Swagger#setSecurityDefinitions() . 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: SwaggerGenerator.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private static Map<String, SecuritySchemeDefinition> getOrCreateSecurityDefinitionMap(
    Swagger swagger) {
  Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
  if (securityDefinitions == null) {
    securityDefinitions = new LinkedHashMap<>();
    swagger.setSecurityDefinitions(securityDefinitions);
  }
  return securityDefinitions;
}
 
Example 2
Source File: SecurityDefinitionConfigurator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void afterScan(Reader reader, Swagger swagger) {
    OAuth2Definition tokenScheme = new OAuth2Definition();
    tokenScheme.setType("oauth2");
    tokenScheme.setFlow("application");
    tokenScheme.setTokenUrl("https://" + swagger.getHost() + "/oauth2/token");
    tokenScheme.setAuthorizationUrl("https://" + swagger.getHost() + "/oauth2/authorize");
    tokenScheme.addScope("write:everything", "Full access");

    Map<String, SecuritySchemeDefinition> schemes = new HashMap<>();
    schemes.put(TOKEN_AUTH_SCHEME, tokenScheme);

    swagger.setSecurityDefinitions(schemes);
}
 
Example 3
Source File: SecurityDefinitionConfigurator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void afterScan(Reader reader, Swagger swagger) {
    OAuth2Definition tokenScheme = new OAuth2Definition();
    tokenScheme.setType("oauth2");
    tokenScheme.setFlow("application");
    tokenScheme.setTokenUrl("https://" + swagger.getHost() + "/oauth2/token");
    tokenScheme.setAuthorizationUrl("https://" + swagger.getHost() + "/oauth2/authorize");
    tokenScheme.addScope("write:everything", "Full access");

    Map<String, SecuritySchemeDefinition> schemes = new HashMap<>();
    schemes.put(TOKEN_AUTH_SCHEME, tokenScheme);

    swagger.setSecurityDefinitions(schemes);
}
 
Example 4
Source File: SecurityDefinitionConfigurator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void afterScan(Reader reader, Swagger swagger) {
    OAuth2Definition tokenScheme = new OAuth2Definition();
    tokenScheme.setType("oauth2");
    tokenScheme.setFlow("application");
    tokenScheme.setTokenUrl("https://" + swagger.getHost() + "/oauth2/token");
    tokenScheme.setAuthorizationUrl("https://" + swagger.getHost() + "/oauth2/authorize");
    tokenScheme.addScope("write:everything", "Full access");

    Map<String, SecuritySchemeDefinition> schemes = new HashMap<>();
    schemes.put(TOKEN_AUTH_SCHEME, tokenScheme);

    swagger.setSecurityDefinitions(schemes);
   //TODO: Have to add wso2-scopes to swagger definition from here
}
 
Example 5
Source File: SecurityDefinitionConfigurator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public void afterScan(Reader reader, Swagger swagger) {
    OAuth2Definition tokenScheme = new OAuth2Definition();
    tokenScheme.setType("oauth2");
    tokenScheme.setFlow("application");
    tokenScheme.setTokenUrl("https://" + swagger.getHost() + "/oauth2/token");
    tokenScheme.setAuthorizationUrl("https://" + swagger.getHost() + "/oauth2/authorize");
    tokenScheme.addScope("write:everything", "Full access");

    Map<String, SecuritySchemeDefinition> schemes = new HashMap<>();
    schemes.put(TOKEN_AUTH_SCHEME, tokenScheme);

    swagger.setSecurityDefinitions(schemes);
   //TODO: Have to add wso2-scopes to swagger definition from here
}
 
Example 6
Source File: AbstractDocumentSource.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
private Swagger addSecurityDefinitions(final Swagger swagger, ApiSource apiSource) throws GenerateException {
    Swagger result = swagger;
    if (apiSource.getSecurityDefinitions() == null) {
        return result;
    }
    Map<String, SecuritySchemeDefinition> definitions = new TreeMap<String, SecuritySchemeDefinition>();
    for (SecurityDefinition sd : apiSource.getSecurityDefinitions()) {
        for (Map.Entry<String, SecuritySchemeDefinition> entry : sd.generateSecuritySchemeDefinitions().entrySet()) {
            definitions.put(entry.getKey(), entry.getValue());
        }
    }
    result.setSecurityDefinitions(definitions);
    return result;
}
 
Example 7
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * This method returns the oauth scopes according to the given swagger(version 2)
 *
 * @param swagger resource json
 * @return Swagger
 * @throws APIManagementException
 */
private Swagger injectOtherScopesToDefaultScheme(Swagger swagger) throws APIManagementException {
    //Get security definitions from swagger
    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    List<String> otherSetOfSchemes = new ArrayList<>();
    Map<String, String> defaultScopeBindings = null;
    if (securityDefinitions != null) {
        //If there is no default type schemes set a one
        OAuth2Definition newDefault = new OAuth2Definition();
        securityDefinitions.put(SWAGGER_SECURITY_SCHEMA_KEY, newDefault);
        //Check all the security definitions
        for (Map.Entry<String, SecuritySchemeDefinition> definition : securityDefinitions.entrySet()) {
            String checkType = definition.getValue().getType();
            //Inject other scheme scopes into default scope
            if (!SWAGGER_SECURITY_SCHEMA_KEY.equals(definition.getKey()) && "oauth2".equals(checkType)) {
                //Add non default scopes to other scopes list
                otherSetOfSchemes.add(definition.getKey());
                //Check for default one
                OAuth2Definition noneDefaultFlowType = (OAuth2Definition) definition.getValue();
                OAuth2Definition defaultTypeFlow = (OAuth2Definition) securityDefinitions.get(SWAGGER_SECURITY_SCHEMA_KEY);
                Map<String, String> noneDefaultFlowScopes = noneDefaultFlowType.getScopes();
                Map<String, String> defaultTypeScopes = defaultTypeFlow.getScopes();
                if (defaultTypeScopes == null) {
                    defaultTypeScopes = new HashMap<>();
                }
                for (Map.Entry<String, String> input : noneDefaultFlowScopes.entrySet()) {
                    defaultTypeScopes.put(input.getKey(), input.getValue());
                }
                defaultTypeFlow.setScopes(defaultTypeScopes);
                //Check X-Scope Bindings
                Map<String, String> noneDefaultScopeBindings = null;
                Map<String, Object> defaultTypeExtension = defaultTypeFlow.getVendorExtensions();
                if (noneDefaultFlowType.getVendorExtensions() != null && (noneDefaultScopeBindings =
                        (Map<String, String>) noneDefaultFlowType.getVendorExtensions().get(APIConstants.SWAGGER_X_SCOPES_BINDINGS))
                        != null) {
                    if (defaultScopeBindings == null) {
                        defaultScopeBindings = new HashMap<>();
                    }
                    //Inject non default scope bindings into default scheme
                    for (Map.Entry<String, String> roleInUse : noneDefaultScopeBindings.entrySet()) {
                        defaultScopeBindings.put(roleInUse.getKey(), roleInUse.getValue());
                    }
                }
                defaultTypeExtension.put(APIConstants.SWAGGER_X_SCOPES_BINDINGS, defaultScopeBindings);
                defaultTypeFlow.setVendorExtensions(defaultTypeExtension);
                securityDefinitions.put(SWAGGER_SECURITY_SCHEMA_KEY, defaultTypeFlow);
            }
        }
        //update list of security schemes in the swagger object
        swagger.setSecurityDefinitions(securityDefinitions);
    }
    setOtherSchemes(otherSetOfSchemes);
    return swagger;
}
 
Example 8
Source File: Swagger2Feature.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
protected void addSwaggerResource(Server server, Bus bus) {
    JAXRSServiceFactoryBean sfb =
            (JAXRSServiceFactoryBean) server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());

    ServerProviderFactory factory =
            (ServerProviderFactory)server.getEndpoint().get(ServerProviderFactory.class.getName());
    final ApplicationInfo appInfo = DefaultApplicationFactory.createApplicationInfoOrDefault(server,
            factory, sfb, bus, isScan());

    List<Object> swaggerResources = new LinkedList<>();

    if (customizer == null) {
        customizer = new Swagger2Customizer();
    }
    ApiListingResource apiListingResource = new Swagger2ApiListingResource(customizer);
    swaggerResources.add(apiListingResource);

    List<Object> providers = new ArrayList<>();
    providers.add(new SwaggerSerializers());

    if (isRunAsFilter()) {
        providers.add(new SwaggerContainerRequestFilter(appInfo == null ? null : appInfo.getProvider(),
                customizer));
    }

    final Properties swaggerProps = getSwaggerProperties(propertiesLocation, bus);
    final Registration swaggerUiRegistration = getSwaggerUi(bus, swaggerProps, isRunAsFilter());

    if (!isRunAsFilter()) {
        swaggerResources.addAll(swaggerUiRegistration.getResources());
    }

    providers.addAll(swaggerUiRegistration.getProviders());
    sfb.setResourceClassesFromBeans(swaggerResources);

    List<ClassResourceInfo> cris = sfb.getClassResourceInfo();
    if (!isRunAsFilter()) {
        for (ClassResourceInfo cri : cris) {
            if (ApiListingResource.class.isAssignableFrom(cri.getResourceClass())) {
                InjectionUtils.injectContextProxies(cri, apiListingResource);
            }
        }
    }
    customizer.setClassResourceInfos(cris);
    customizer.setDynamicBasePath(dynamicBasePath);

    BeanConfig beanConfig = appInfo == null
            ? new BeanConfig()
            : new ApplicationBeanConfig(appInfo.getProvider());
    initBeanConfig(beanConfig, swaggerProps);

    Swagger swagger = beanConfig.getSwagger();
    if (swagger != null && securityDefinitions != null) {
        swagger.setSecurityDefinitions(securityDefinitions);
    }
    customizer.setBeanConfig(beanConfig);

    providers.add(new ReaderConfigFilter());

    if (beanConfig.isUsePathBasedConfig()) {
        providers.add(new ServletConfigProvider());
    }

    factory.setUserProviders(providers);
}