Search in sources :

Example 11 with SecurityScheme

use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method readSecuritySchemesObject.

@Test(dataProvider = "data")
public void readSecuritySchemesObject(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);
    Assert.assertNotNull(result);
    List<String> messages = result.getMessages();
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.openIdConnectUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth'.tokenUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_password'.authorizationUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.petstore_auth_clientCredentials'.authorizationUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.flows is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.api_key'.openIdConnectUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.flows is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.http'.openIdConnectUrl is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.name is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.in is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.scheme is missing"));
    assertTrue(!messages.contains("attribute components.securitySchemes'.openID'.flows is missing"));
    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);
    final Map<String, SecurityScheme> securitySchemes = openAPI.getComponents().getSecuritySchemes();
    Assert.assertNotNull(securitySchemes);
    Assert.assertEquals(securitySchemes.size(), 10);
    SecurityScheme securityScheme = securitySchemes.get("reference");
    assertTrue(securityScheme.get$ref().equals("#/components/securitySchemes/api_key"));
    securityScheme = securitySchemes.get("remote_reference");
    assertTrue(securityScheme.get$ref().equals("http://localhost:${dynamicPort}/remote/security#/petstore_remote"));
    securityScheme = securitySchemes.get("petstore_auth");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("petstore_auth_password");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("petstore_auth_clientCredentials");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("petstore_auth_authorizationCode");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OAUTH2);
    securityScheme = securitySchemes.get("api_key");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.APIKEY);
    assertTrue(securityScheme.getIn() == SecurityScheme.In.HEADER);
    securityScheme = securitySchemes.get("api_key_cookie");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.APIKEY);
    assertTrue(securityScheme.getIn() == SecurityScheme.In.COOKIE);
    securityScheme = securitySchemes.get("http");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.HTTP);
    securityScheme = securitySchemes.get("openID");
    assertTrue(securityScheme.getType() == SecurityScheme.Type.OPENIDCONNECT);
}
Also used : SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 12 with SecurityScheme

use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.

the class SwaggerConverter method convertOauth2SecurityScheme.

private SecurityScheme convertOauth2SecurityScheme(SecuritySchemeDefinition definition) {
    SecurityScheme securityScheme = new SecurityScheme();
    OAuth2Definition oAuth2Definition = (OAuth2Definition) definition;
    OAuthFlows oAuthFlows = new OAuthFlows();
    OAuthFlow oAuthFlow = new OAuthFlow();
    securityScheme.setType(SecurityScheme.Type.OAUTH2);
    String flow = oAuth2Definition.getFlow();
    if (flow != null) {
        switch(flow) {
            case "implicit":
                oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
                oAuthFlows.setImplicit(oAuthFlow);
                break;
            case "password":
                oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
                oAuthFlows.setPassword(oAuthFlow);
                break;
            case "application":
                oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
                oAuthFlows.setClientCredentials(oAuthFlow);
                break;
            case "accessCode":
                oAuthFlow.setAuthorizationUrl(oAuth2Definition.getAuthorizationUrl());
                oAuthFlow.setTokenUrl(oAuth2Definition.getTokenUrl());
                oAuthFlows.setAuthorizationCode(oAuthFlow);
                break;
        }
    }
    Scopes scopes = new Scopes();
    Map<String, String> oAuth2Scopes = oAuth2Definition.getScopes();
    if (oAuth2Scopes != null) {
        oAuth2Scopes.forEach((k, v) -> scopes.addString(k, v));
    }
    oAuthFlow.setScopes(scopes);
    securityScheme.setFlows(oAuthFlows);
    return securityScheme;
}
Also used : OAuthFlows(io.swagger.v3.oas.models.security.OAuthFlows) OAuthFlow(io.swagger.v3.oas.models.security.OAuthFlow) Scopes(io.swagger.v3.oas.models.security.Scopes) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme)

Example 13 with SecurityScheme

use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.

the class SwaggerConverter method createBasicSecurityScheme.

private SecurityScheme createBasicSecurityScheme() {
    SecurityScheme securityScheme = new SecurityScheme();
    securityScheme.setType(SecurityScheme.Type.HTTP);
    securityScheme.setScheme("basic");
    return securityScheme;
}
Also used : SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme)

Example 14 with SecurityScheme

use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.

the class V2ConverterTest method testIssue15.

@Test(description = "Convert extensions everywhere applicable #15")
public void testIssue15() throws Exception {
    OpenAPI oas = getConvertedOpenAPIFromJsonFile(ISSUE_15_JSON);
    assertNotNull(oas);
    Info info = oas.getInfo();
    assertNotNull(info.getExtensions().get("x-apis-json"));
    assertNotNull(info.getLicense().getExtensions().get("x-notes"));
    assertNotNull(oas.getExternalDocs().getExtensions().get("x-docs-extension"));
    assertNotNull(oas.getTags().get(0).getExtensions().get("x-tag-extension"));
    assertNotNull(oas.getTags().get(0).getExternalDocs().getExtensions().get("x-tag-docs-extension"));
    PathItem pathItem = oas.getPaths().get("/something");
    assertNotNull(pathItem.getExtensions().get("x-path-item-extension"));
    Operation get = pathItem.getGet();
    assertNotNull(get.getExtensions().get("x-version"));
    assertNotNull(get.getExternalDocs().getExtensions().get("x-operation-docs-extension"));
    assertNotNull(get.getResponses().get("200").getExtensions().get("x-response-extension"));
    ArraySchema schema = (ArraySchema) get.getParameters().get(0).getSchema();
    assertNull(schema.getItems().getExtensions().get(X_EXAMPLE));
    Map<String, SecurityScheme> securitySchemes = oas.getComponents().getSecuritySchemes();
    assertNotNull(securitySchemes);
    assertNotNull(securitySchemes.get("OAuth2Implicit").getExtensions().get("x-auth-extension"));
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Operation(io.swagger.v3.oas.models.Operation) Info(io.swagger.v3.oas.models.info.Info) OpenAPI(io.swagger.v3.oas.models.OpenAPI) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme) Test(org.testng.annotations.Test)

Example 15 with SecurityScheme

use of io.swagger.v3.oas.models.security.SecurityScheme in project swagger-parser by swagger-api.

the class ComponentsProcessor method processSecuritySchemes.

private void processSecuritySchemes(Set<String> securitySchemeKey, Map<String, SecurityScheme> securitySchemes) {
    securitySchemeKey.addAll(securitySchemes.keySet());
    for (String securitySchemeName : securitySchemeKey) {
        final SecurityScheme securityScheme = securitySchemes.get(securitySchemeName);
        SecurityScheme resolvedSecurityScheme = securitySchemeProcessor.processSecurityScheme(securityScheme);
        securitySchemes.replace(securitySchemeName, securityScheme, resolvedSecurityScheme);
    }
}
Also used : SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme)

Aggregations

SecurityScheme (io.swagger.v3.oas.models.security.SecurityScheme)35 OpenAPI (io.swagger.v3.oas.models.OpenAPI)20 Components (io.swagger.v3.oas.models.Components)15 OAuthFlow (io.swagger.v3.oas.models.security.OAuthFlow)13 Scopes (io.swagger.v3.oas.models.security.Scopes)12 OAuthFlows (io.swagger.v3.oas.models.security.OAuthFlows)11 Test (org.testng.annotations.Test)11 SecurityRequirement (io.swagger.v3.oas.models.security.SecurityRequirement)8 Operation (io.swagger.v3.oas.models.Operation)7 PathItem (io.swagger.v3.oas.models.PathItem)7 Info (io.swagger.v3.oas.models.info.Info)7 HashMap (java.util.HashMap)6 LinkedHashMap (java.util.LinkedHashMap)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 MediaType (io.swagger.v3.oas.models.media.MediaType)5 Schema (io.swagger.v3.oas.models.media.Schema)5 Parameter (io.swagger.v3.oas.models.parameters.Parameter)5 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)5 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)5 ArrayList (java.util.ArrayList)5