io.swagger.models.auth.SecuritySchemeDefinition Java Examples

The following examples show how to use io.swagger.models.auth.SecuritySchemeDefinition. 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: SecurityDefinitionTest.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecurityDefinitionRetainsWantedName() throws GenerateException {
    SecurityDefinition definition = new SecurityDefinition();
    definition.setJson("securityDefinition.json");

    Map<String, SecuritySchemeDefinition> definitions = definition.generateSecuritySchemeDefinitions();

    SecuritySchemeDefinition api_key = definitions.get("api_key");
    Assert.assertNotNull(api_key);
    Assert.assertTrue(api_key instanceof ApiKeyAuthDefinition);
    Assert.assertEquals(((ApiKeyAuthDefinition)api_key).getName(), "api_key_name");

    // No name is set for this auth
    // The name should be set to the name of the definition
    // So that the name is never actually empty
    SecuritySchemeDefinition api_key_empty_name = definitions.get("api_key_empty_name");
    Assert.assertNotNull(api_key_empty_name);
    Assert.assertTrue(api_key_empty_name instanceof ApiKeyAuthDefinition);
    Assert.assertEquals(((ApiKeyAuthDefinition)api_key_empty_name).getName(), "api_key_empty_name");


    SecuritySchemeDefinition petstore_auth = definitions.get("petstore_auth");
    Assert.assertNotNull(petstore_auth);
    Assert.assertTrue(petstore_auth instanceof OAuth2Definition);
}
 
Example #2
Source File: SecuritySchemeDefinitionComponentTest.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecuritySchemeDefinitionComponentWithApiKey() throws URISyntaxException {
    //Given
    Path file = Paths.get(SecuritySchemeDefinitionComponentTest.class.getResource("/yaml/swagger_petstore.yaml").toURI());
    Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(file).build();
    Swagger swagger = converter.getContext().getSchema();

    SecuritySchemeDefinition securitySchemeDefinition = swagger.getSecurityDefinitions().get("api_key");

    Swagger2MarkupConverter.SwaggerContext context = converter.getContext();
    MarkupDocBuilder markupDocBuilder = context.createMarkupDocBuilder();

    markupDocBuilder = new SecuritySchemeDefinitionComponent(context).apply(
            markupDocBuilder, SecuritySchemeDefinitionComponent.parameters("api_key",
                    securitySchemeDefinition,
                    OverviewDocument.SECTION_TITLE_LEVEL));
    markupDocBuilder.writeToFileWithoutExtension(apiKeyOutputDirectory, StandardCharsets.UTF_8);

    Path expectedFile = getExpectedFile(API_KEY_NAME);
    DiffUtils.assertThatFileIsEqual(expectedFile, apiKeyOutputDirectory, getReportName(API_KEY_NAME));

}
 
Example #3
Source File: SecuritySchemeDefinitionComponentTest.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
@Test
public void testSecuritySchemeDefinitionComponentWithOAuth() throws URISyntaxException {
    //Given
    Path file = Paths.get(SecuritySchemeDefinitionComponentTest.class.getResource("/yaml/swagger_petstore.yaml").toURI());
    Swagger2MarkupConverter converter = Swagger2MarkupConverter.from(file).build();
    Swagger swagger = converter.getContext().getSchema();

    SecuritySchemeDefinition securitySchemeDefinition = swagger.getSecurityDefinitions().get("petstore_auth");

    Swagger2MarkupConverter.SwaggerContext context = converter.getContext();
    MarkupDocBuilder markupDocBuilder = context.createMarkupDocBuilder();

    markupDocBuilder = new SecuritySchemeDefinitionComponent(context).apply(
            markupDocBuilder, SecuritySchemeDefinitionComponent.parameters("petstore_auth",
                    securitySchemeDefinition,
                    OverviewDocument.SECTION_TITLE_LEVEL));
    markupDocBuilder.writeToFileWithoutExtension(oauthOutputDirectory, StandardCharsets.UTF_8);

    Path expectedFile = getExpectedFile(O_AUTH_NAME);
    DiffUtils.assertThatFileIsEqual(expectedFile, oauthOutputDirectory, getReportName(O_AUTH_NAME));

}
 
Example #4
Source File: ApiGatewaySdkSwaggerApiImporter.java    From aws-apigateway-importer with Apache License 2.0 6 votes vote down vote up
private Boolean isApiKeyRequired(Operation op) {
    Optional<Map.Entry<String, SecuritySchemeDefinition>> apiKeySecurityDefinition = Optional.empty();

    if (swagger.getSecurityDefinitions() != null) {
        apiKeySecurityDefinition = swagger.getSecurityDefinitions().entrySet()
                .stream().filter(p -> p.getValue().getType().equals("apiKey")).findFirst();
    }

    if (!apiKeySecurityDefinition.isPresent()) {
        return false;
    }

    String securityDefinitionName = apiKeySecurityDefinition.get().getKey();

    if (op.getSecurity() != null) {
        return op.getSecurity().stream().anyMatch(s -> s.containsKey(securityDefinitionName));
    }
    if (swagger.getSecurityRequirement() != null) {
        return swagger.getSecurityRequirement().stream().anyMatch(s -> s.getName().equals(securityDefinitionName));
    }
    return false;
}
 
Example #5
Source File: SecuritySchemeDefinitionComponent.java    From swagger2markup with Apache License 2.0 6 votes vote down vote up
@Override
public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, Parameters params) {
    String securitySchemeDefinitionName = params.securitySchemeDefinitionName;
    SecuritySchemeDefinition securitySchemeDefinition = params.securitySchemeDefinition;
    applySecurityDocumentExtension(new Context(Position.SECURITY_SCHEME_BEFORE, markupDocBuilder, securitySchemeDefinitionName, securitySchemeDefinition));
    markupDocBuilder.sectionTitleWithAnchorLevel(params.titleLevel, securitySchemeDefinitionName);
    applySecurityDocumentExtension(new Context(Position.SECURITY_SCHEME_BEGIN, markupDocBuilder, securitySchemeDefinitionName, securitySchemeDefinition));
    String description = securitySchemeDefinition.getDescription();
    if (isNotBlank(description)) {
        markupDocBuilder.paragraph(markupDescription(MarkupLanguage.valueOf(config.getSchemaMarkupLanguage().name()),
                markupDocBuilder, description));
    }
    buildSecurityScheme(markupDocBuilder, securitySchemeDefinition);
    applySecurityDocumentExtension(new Context(Position.SECURITY_SCHEME_END, markupDocBuilder, securitySchemeDefinitionName, securitySchemeDefinition));
    applySecurityDocumentExtension(new Context(Position.SECURITY_SCHEME_AFTER, markupDocBuilder, securitySchemeDefinitionName, securitySchemeDefinition));
    return markupDocBuilder;
}
 
Example #6
Source File: SecurityDefinitionDeserializer.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override public SecuritySchemeDefinition deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {
    SecuritySchemeDefinition result = null;

    JsonNode node = jp.getCodec().readTree(jp);
    JsonNode inNode = node.get("type");

    if (inNode != null) {
        String type = inNode.asText();
        if ("basic".equals(type)) {
            result = Json.mapper().convertValue(node, BasicAuthDefinition.class);
        } else if ("apiKey".equals(type)) {
            result = Json.mapper().convertValue(node, ApiKeyAuthDefinition.class);
        } else if ("oauth2".equals(type)) {
            result = Json.mapper().convertValue(node, OAuth2Definition.class);
        }
    }

    return result;
}
 
Example #7
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the "Auth2" security scheme key
 *
 * @param swagger Swgger object
 * @return "Auth2" security scheme key
 */
private String getOAuth2SecuritySchemeKey(Swagger swagger) {
    final String oauth2Type = new OAuth2Definition().getType();
    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    boolean hasDefaultKey = false;
    boolean hasRESTAPIScopeKey = false;
    if (securityDefinitions != null) {
        for (Map.Entry<String, SecuritySchemeDefinition> definitionEntry : securityDefinitions.entrySet()) {
            if (oauth2Type.equals(definitionEntry.getValue().getType())) {
                //sets hasDefaultKey to true if at least once SWAGGER_APIM_DEFAULT_SECURITY becomes the key
                hasDefaultKey = hasDefaultKey || SWAGGER_APIM_DEFAULT_SECURITY.equals(definitionEntry.getKey());
                //sets hasRESTAPIScopeKey to true if at least once SWAGGER_APIM_RESTAPI_SECURITY becomes the key
                hasRESTAPIScopeKey = hasRESTAPIScopeKey
                        || SWAGGER_APIM_RESTAPI_SECURITY.equals(definitionEntry.getKey());
            }
        }
    }
    if (hasDefaultKey) {
        return SWAGGER_APIM_DEFAULT_SECURITY;
    } else if (hasRESTAPIScopeKey) {
        return SWAGGER_APIM_RESTAPI_SECURITY;
    } else {
        return null;
    }
}
 
Example #8
Source File: SecurityDefinition.java    From swagger-maven-plugin with Apache License 2.0 6 votes vote down vote up
public Map<String, SecuritySchemeDefinition> generateSecuritySchemeDefinitions() throws GenerateException {
    Map<String, SecuritySchemeDefinition> map = new HashMap<String, SecuritySchemeDefinition>();

    Map<String, JsonNode> securityDefinitions = new HashMap<String, JsonNode>();
    if (json != null || jsonPath != null) {
        securityDefinitions = loadSecurityDefintionsFromJsonFile();
    } else {
        JsonNode tree = mapper.valueToTree(this);
        securityDefinitions.put(tree.get("name").asText(), tree);
    }

    for (Map.Entry<String, JsonNode> securityDefinition : securityDefinitions.entrySet()) {
        JsonNode definition = securityDefinition.getValue();
        SecuritySchemeDefinition ssd = getSecuritySchemeDefinitionByType(definition.get("type").asText(), definition);
        tryFillNameField(ssd, securityDefinition.getKey());

        if (ssd != null) {
            map.put(securityDefinition.getKey(), ssd);
        }
    }

    return map;
}
 
Example #9
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 #10
Source File: SecurityDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * Builds the security MarkupDocument.
 *
 * @return the security MarkupDocument
 */
@Override
public MarkupDocBuilder apply(MarkupDocBuilder markupDocBuilder, SecurityDocument.Parameters params) {
    Map<String, SecuritySchemeDefinition> definitions = params.securitySchemeDefinitions;
    if (MapUtils.isNotEmpty(definitions)) {
        applySecurityDocumentExtension(new Context(Position.DOCUMENT_BEFORE, markupDocBuilder));
        buildSecurityTitle(markupDocBuilder, labels.getLabel(SECURITY));
        applySecurityDocumentExtension(new Context(Position.DOCUMENT_BEGIN, markupDocBuilder));
        buildSecuritySchemeDefinitionsSection(markupDocBuilder, definitions);
        applySecurityDocumentExtension(new Context(Position.DOCUMENT_END, markupDocBuilder));
        applySecurityDocumentExtension(new Context(Position.DOCUMENT_AFTER, markupDocBuilder));
    }
    return markupDocBuilder;
}
 
Example #11
Source File: SecurityDocument.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
private void buildSecuritySchemeDefinitionsSection(MarkupDocBuilder markupDocBuilder, Map<String, SecuritySchemeDefinition> securitySchemes) {
    Map<String, SecuritySchemeDefinition> securitySchemeNames = toSortedMap(securitySchemes, null); // TODO : provide a dedicated ordering configuration for security schemes
    securitySchemeNames.forEach((String securitySchemeName, SecuritySchemeDefinition securityScheme) ->
            securitySchemeDefinitionComponent.apply(markupDocBuilder, SecuritySchemeDefinitionComponent.parameters(
                    securitySchemeName, securityScheme, 2
            )));
}
 
Example #12
Source File: SecuritySchemeDefinitionComponent.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
public Parameters(String securitySchemeDefinitionName,
                  SecuritySchemeDefinition securitySchemeDefinition,
                  int titleLevel) {
    this.securitySchemeDefinitionName = Validate.notBlank(securitySchemeDefinitionName, "SecuritySchemeDefinitionName must not be empty");
    this.securitySchemeDefinition = Validate.notNull(securitySchemeDefinition, "SecuritySchemeDefinition must not be null");
    this.titleLevel = titleLevel;
}
 
Example #13
Source File: SwaggerConverter.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
private SecurityScheme convertApiKeySecurityScheme(SecuritySchemeDefinition definition) {
    SecurityScheme securityScheme = new SecurityScheme();
    ApiKeyAuthDefinition apiKeyAuthDefinition = (ApiKeyAuthDefinition) definition;

    securityScheme.setType(SecurityScheme.Type.APIKEY);
    securityScheme.setName(apiKeyAuthDefinition.getName());
    securityScheme.setIn(SecurityScheme.In.valueOf(apiKeyAuthDefinition.getIn().toString()));

    return securityScheme;
}
 
Example #14
Source File: SecurityDefinition.java    From swagger-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * <p>Try to fill the name property of some authentication definition, if no user defined value was set.</p>
 * <p>If the current value of the name property is empty, this will fill it to be the same as the name of the
 * security definition.</br>
 * If no {@link Field} named "name" is found inside the given SecuritySchemeDefinition, no action will be taken.
 *
 * @param ssd security scheme
 * @param value value to set the name to
 */
private void tryFillNameField(SecuritySchemeDefinition ssd, String value) {
    if (ssd == null) {
        return;
    }

    Field nameField = FieldUtils.getField(ssd.getClass(), "name", true);
    try {
        if (nameField != null && nameField.get(ssd) == null) {
            nameField.set(ssd, value);
        }
    } catch (IllegalAccessException e) {
        // ignored
    }
}
 
Example #15
Source File: DeserializationModule.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
public DeserializationModule(boolean includePathDeserializer,
        boolean includeResponseDeserializer) {

    if (includePathDeserializer) {
        this.addDeserializer(Path.class, new PathDeserializer());
    }
    if (includeResponseDeserializer) {
        this.addDeserializer(Response.class, new ResponseDeserializer());
    }

    this.addDeserializer(Property.class, new PropertyDeserializer());
    this.addDeserializer(Model.class, new ModelDeserializer());
    this.addDeserializer(Parameter.class, new ParameterDeserializer());
    this.addDeserializer(SecuritySchemeDefinition.class, new SecurityDefinitionDeserializer());
}
 
Example #16
Source File: SecurityDocumentExtension.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * @param position           the current position
 * @param docBuilder         the MarkupDocBuilder
 * @param securitySchemeName the name of the current securityScheme
 * @param securityScheme     the current security scheme securityScheme
 */
public Context(Position position, MarkupDocBuilder docBuilder, String securitySchemeName, SecuritySchemeDefinition securityScheme) {
    super(docBuilder);
    Validate.inclusiveBetween(Position.SECURITY_SCHEME_BEFORE, Position.SECURITY_SCHEME_AFTER, position);
    Validate.notNull(securitySchemeName);
    Validate.notNull(securityScheme);
    this.position = position;
    this.securitySchemeName = securitySchemeName;
    this.securityScheme = securityScheme;
}
 
Example #17
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the oauth scopes according to the given swagger
 *
 * @param resourceConfigsJSON resource json
 * @return scope set
 * @throws APIManagementException
 */
@Override
public Set<Scope> getScopes(String resourceConfigsJSON) throws APIManagementException {
    Swagger swagger = getSwagger(resourceConfigsJSON);
    String oauth2SchemeKey = getOAuth2SecuritySchemeKey(swagger);

    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    OAuth2Definition oAuth2Definition;
    if (securityDefinitions != null
            && (oAuth2Definition = (OAuth2Definition) securityDefinitions.get(oauth2SchemeKey)) != null
            && oAuth2Definition.getScopes() != null) {
        Set<Scope> scopeSet = new LinkedHashSet<>();
        for (Map.Entry<String, String> entry : oAuth2Definition.getScopes().entrySet()) {
            Scope scope = new Scope();
            scope.setKey(entry.getKey());
            scope.setName(entry.getKey());
            scope.setDescription(entry.getValue());
            Map<String, String> scopeBindings;
            if (oAuth2Definition.getVendorExtensions() != null && (scopeBindings =
                    (Map<String, String>) oAuth2Definition.getVendorExtensions()
                            .get(APIConstants.SWAGGER_X_SCOPES_BINDINGS)) != null) {
                if (scopeBindings.get(scope.getKey()) != null) {
                    scope.setRoles(scopeBindings.get(scope.getKey()));
                }
            }
            scopeSet.add(scope);
        }
        return OASParserUtil.sortScopes(scopeSet);
    } else {
        return OASParserUtil.sortScopes(getScopesFromExtensions(swagger));
    }
}
 
Example #18
Source File: SecurityDocumentExtension.java    From swagger2markup with Apache License 2.0 5 votes vote down vote up
/**
 * @param position           the current position
 * @param document         the MarkupDocBuilder
 * @param securitySchemeName the name of the current securityScheme
 * @param securityScheme     the current security scheme securityScheme
 */
public Context(Position position, Document document, String securitySchemeName, SecuritySchemeDefinition securityScheme) {
    super(document);
    Validate.inclusiveBetween(Position.SECURITY_SCHEME_BEFORE, Position.SECURITY_SCHEME_AFTER, position);
    Validate.notNull(securitySchemeName);
    Validate.notNull(securityScheme);
    this.position = position;
    this.securitySchemeName = securitySchemeName;
    this.securityScheme = securityScheme;
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
Source File: SwaggerGenerator.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private static String addNonConflictingSecurityDefinition(
    Swagger swagger, IssuerConfig issuerConfig, ImmutableSet<String> audiences)
    throws ApiConfigException {
  Map<String, SecuritySchemeDefinition> securityDefinitions =
      getOrCreateSecurityDefinitionMap(swagger);
  String issuerPlusHash = String.format("%s-%x", issuerConfig.getName(), audiences.hashCode());
  SecuritySchemeDefinition existingDef = securityDefinitions.get(issuerConfig.getName());
  SecuritySchemeDefinition newDef = toScheme(issuerConfig, audiences);
  if (existingDef != null && !existingDef.equals(newDef)) {
    throw new ApiConfigException(
        "Multiple conflicting definitions found for issuer " + issuerConfig.getName());
  }
  swagger.securityDefinition(issuerPlusHash, newDef);
  return issuerPlusHash;
}
 
Example #24
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 #25
Source File: SwaggerGenerator.java    From endpoints-java with Apache License 2.0 5 votes vote down vote up
private static SecuritySchemeDefinition toScheme(
    IssuerConfig issuerConfig, ImmutableSet<String> audiences) {
  OAuth2Definition tokenDef = new OAuth2Definition().implicit("");
  tokenDef.setVendorExtension("x-google-issuer", issuerConfig.getIssuer());
  if (!com.google.common.base.Strings.isNullOrEmpty(issuerConfig.getJwksUri())) {
    tokenDef.setVendorExtension("x-google-jwks_uri", issuerConfig.getJwksUri());
  }
  tokenDef.setVendorExtension("x-google-audiences", COMMA_JOINER.join(audiences));
  return tokenDef;
}
 
Example #26
Source File: OAS2Parser.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
/**
 * This method returns the boolean value which checks whether the swagger is included default security scheme or not
 *
 * @param swaggerContent resource json
 * @return boolean
 * @throws APIManagementException
 */
private boolean isDefaultGiven(String swaggerContent) throws APIManagementException {
    Swagger swagger = getSwagger(swaggerContent);

    Map<String, SecuritySchemeDefinition> securityDefinitions = swagger.getSecurityDefinitions();
    if (securityDefinitions == null) {
        return false;
    }
    OAuth2Definition checkDefault = (OAuth2Definition) securityDefinitions.get(SWAGGER_SECURITY_SCHEMA_KEY);
    if (checkDefault == null) {
        return false;
    }
    return true;
}
 
Example #27
Source File: SwaggerAuthHandlerFactory.java    From vertx-swagger with Apache License 2.0 5 votes vote down vote up
private AuthHandler getAuthHandler(String name) {
    AuthHandler authHandler = this.authHandlers.get(name);
    if (authHandler != null) {
        return authHandler;
    }

    AuthProvider authProvider = getAuthProviderFactory().getAuthProviderByName(name);
    if (authProvider == null) {
        return null;
    }

    SecuritySchemeDefinition securityScheme = this.securitySchemes.get(name);
    if(securityScheme != null) {
     switch (securityScheme.getType()) {
         case "apiKey":
             ApiKeyAuthDefinition apiKeyAuthDefinition = (ApiKeyAuthDefinition) securityScheme;
             Location apiKeyLocation = Location.valueOf(apiKeyAuthDefinition.getIn().name());
             authHandler = ApiKeyAuthHandler.create(authProvider, apiKeyLocation, apiKeyAuthDefinition.getName());
             break;
         case "basic":
             authHandler = BasicAuthHandler.create(authProvider);
             break;
         case "oauth2":
             vertxLogger.warn("OAuth2 authentication has not been implemented yet!");
             break;
         default:
             vertxLogger.warn("SecurityScheme is not authorized : " + securityScheme.getType());
             break;
     }
     
	
     if (authHandler != null) {
         this.authHandlers.put(name, authHandler);
     }
    } else {
        vertxLogger.warn("No securityScheme definition in swagger file for auth provider: " + name);
    }

    return authHandler;
}
 
Example #28
Source File: HaskellHttpClientCodegen.java    From TypeScript-Microservices with MIT License 5 votes vote down vote up
@Override
public List<CodegenSecurity> fromSecurity(Map<String, SecuritySchemeDefinition> schemes) {
    List<CodegenSecurity> secs = super.fromSecurity(schemes);
    for(CodegenSecurity sec : secs) {
       String prefix = "";
       if(sec.isBasic) prefix = "AuthBasic";
       if(sec.isApiKey) prefix = "AuthApiKey";
       if(sec.isOAuth) prefix = "AuthOAuth";
       sec.name = prefix + toTypeName("",sec.name);
    }
    return secs;
}
 
Example #29
Source File: SwaggerHelper.java    From light-rest-4j with Apache License 2.0 5 votes vote down vote up
private static String getOAuth2Name() {
    String name = null;
    Map<String, SecuritySchemeDefinition> defMap = swagger.getSecurityDefinitions();
    if(defMap != null) {
        for(Map.Entry<String, SecuritySchemeDefinition> entry : defMap.entrySet()) {
            if(entry.getValue().getType().equals("oauth2")) {
                name = entry.getKey();
                break;
            }
        }
    }
    return name;
}
 
Example #30
Source File: SecuritySchemeDefinitionComponent.java    From swagger2markup with Apache License 2.0 4 votes vote down vote up
public static SecuritySchemeDefinitionComponent.Parameters parameters(String securitySchemeDefinitionName,
                                                                      SecuritySchemeDefinition securitySchemeDefinition,
                                                                      int titleLevel) {
    return new SecuritySchemeDefinitionComponent.Parameters(securitySchemeDefinitionName, securitySchemeDefinition, titleLevel);
}