Search in sources :

Example 11 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project swagger-parser by swagger-api.

the class SwaggerParserTest method testIssue480.

@Test
public void testIssue480() {
    final Swagger swagger = new SwaggerParser().read("src/test/resources/issue-480.yaml");
    for (String key : swagger.getSecurityDefinitions().keySet()) {
        SecuritySchemeDefinition definition = swagger.getSecurityDefinitions().get(key);
        if ("petstore_auth".equals(key)) {
            assertTrue(definition instanceof OAuth2Definition);
            OAuth2Definition oauth = (OAuth2Definition) definition;
            assertEquals("This is a description", oauth.getDescription());
        }
        if ("api_key".equals(key)) {
            assertTrue(definition instanceof ApiKeyAuthDefinition);
            ApiKeyAuthDefinition auth = (ApiKeyAuthDefinition) definition;
            assertEquals("This is another description", auth.getDescription());
        }
    }
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Test(org.testng.annotations.Test)

Example 12 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project swagger-parser by swagger-api.

the class SwaggerCompatConverter method convert.

public Swagger convert(ResourceListing resourceListing, List<ApiDeclaration> apiDeclarations) {
    Info info = new Info();
    if (resourceListing.getInfo() != null) {
        ApiInfo apiInfo = resourceListing.getInfo();
        Contact contact = null;
        if (apiInfo.getContact() != null) {
            contact = new Contact().url(apiInfo.getContact());
        }
        License license = null;
        if (apiInfo.getLicense() != null) {
            license = new License().name(apiInfo.getLicense()).url(apiInfo.getLicenseUrl());
        }
        info = new Info().description(apiInfo.getDescription()).version(resourceListing.getApiVersion()).title(apiInfo.getTitle()).termsOfService(apiInfo.getTermsOfServiceUrl()).contact(contact).license(license);
    } else if (resourceListing.getApiVersion() != null) {
        info = new Info().version(resourceListing.getApiVersion());
    }
    Map<String, Path> paths = new HashMap<String, Path>();
    Map<String, Model> definitions = new HashMap<String, Model>();
    String basePath = null;
    for (ApiDeclaration apiDeclaration : apiDeclarations) {
        String tag;
        if (apiDeclaration.getApiListingRef() != null) {
            String refPath = apiDeclaration.getApiListingRef().getPath();
            tag = refPath.substring(refPath.lastIndexOf("/") + 1);
        } else {
            tag = apiDeclaration.getResourcePath();
        }
        if (tag != null) {
            tag = tag.replaceAll("/", "");
        }
        if (basePath != null) {
            if (!basePath.equals(apiDeclaration.getBasePath()) && apiDeclaration.getBasePath() != null) {
                LOGGER.warn("warning!  multiple basePath values not supported!");
            }
        } else {
            basePath = apiDeclaration.getBasePath();
        }
        List<Api> apis = apiDeclaration.getApis();
        for (Api api : apis) {
            String apiPath = api.getPath();
            String description = api.getDescription();
            List<io.swagger.models.apideclaration.Operation> ops = api.getOperations();
            Path path = paths.get(apiPath);
            if (path == null) {
                path = new Path();
                paths.put(apiPath, path);
            }
            for (io.swagger.models.apideclaration.Operation op : ops) {
                Operation operation = convertOperation(tag, op, apiDeclaration);
                if (op.getMethod() != null) {
                    path.set(op.getMethod().toString().toLowerCase(), operation);
                } else {
                    LOGGER.info("skipping operation with missing method:\n" + Json.pretty(op));
                }
            }
        }
        // model definitions
        Map<String, io.swagger.models.apideclaration.Model> apiModels = apiDeclaration.getModels();
        for (String key : apiModels.keySet()) {
            Model model = convertModel(apiModels.get(key));
            definitions.put(key, model);
        }
    }
    String host = null;
    String scheme = "http";
    if (basePath != null) {
        String[] parts = basePath.split("://");
        if (parts.length == 2) {
            scheme = parts[0];
            int pos = parts[1].indexOf("/");
            if (pos != -1) {
                host = parts[1].substring(0, pos);
                basePath = parts[1].substring(pos);
            } else {
                host = parts[1];
                basePath = "/";
            }
        }
        if (!basePath.startsWith("/")) {
            basePath = "/" + basePath;
        }
    }
    Swagger swagger = new Swagger().host(host).scheme(Scheme.forValue(scheme)).basePath(basePath).info(info).paths(paths).basePath(basePath);
    swagger.setDefinitions(definitions);
    // host is read from the api declarations
    Map<String, Authorization> authorizations = resourceListing.getAuthorizations();
    if (authorizations != null) {
        for (String authNickname : authorizations.keySet()) {
            Authorization auth = authorizations.get(authNickname);
            if (auth instanceof OAuth2Authorization) {
                OAuth2Authorization o2 = (OAuth2Authorization) auth;
                List<AuthorizationScope> scopes = o2.getScopes();
                if (o2.getGrantTypes().getImplicit() != null) {
                    ImplicitGrant ig = o2.getGrantTypes().getImplicit();
                    OAuth2Definition oauth2 = new OAuth2Definition().implicit(ig.getLoginEndpoint().getUrl());
                    if (swagger.getSecurityDefinitions() != null && swagger.getSecurityDefinitions().keySet().contains(authNickname)) {
                        System.err.println("Warning!  Authorization nickname already in use!");
                    } else {
                        swagger.securityDefinition(authNickname, oauth2);
                    }
                    for (AuthorizationScope scope : scopes) {
                        oauth2.scope(scope.getScope(), scope.getDescription());
                    }
                } else if (o2.getGrantTypes().getAuthorization_code() != null) {
                    AuthorizationCodeGrant ac = (AuthorizationCodeGrant) o2.getGrantTypes().getAuthorization_code();
                    OAuth2Definition oauth2 = new OAuth2Definition().accessCode(ac.getTokenRequestEndpoint().getUrl(), ac.getTokenEndpoint().getUrl());
                    if (swagger.getSecurityDefinitions() != null && swagger.getSecurityDefinitions().keySet().contains(authNickname)) {
                        System.err.println("Warning!  Authorization nickname already in use!");
                    } else {
                        swagger.securityDefinition(authNickname, oauth2);
                    }
                    for (AuthorizationScope scope : scopes) {
                        oauth2.scope(scope.getScope(), scope.getDescription());
                    }
                }
            } else if (auth instanceof ApiKeyAuthorization) {
                ApiKeyAuthorization o2 = (ApiKeyAuthorization) auth;
                ApiKeyAuthDefinition def = new ApiKeyAuthDefinition();
                PassAs passAs = o2.getPassAs();
                if (PassAs.HEADER.equals(passAs)) {
                    def.in(In.HEADER);
                } else {
                    def.in(In.QUERY);
                }
                def.setName(o2.getKeyname());
                swagger.securityDefinition(authNickname, def);
            } else if (auth instanceof BasicAuthorization) {
                BasicAuthDefinition def = new BasicAuthDefinition();
                swagger.securityDefinition(authNickname, def);
            }
        }
    }
    return swagger;
}
Also used : HashMap(java.util.HashMap) BasicAuthorization(io.swagger.models.resourcelisting.BasicAuthorization) License(io.swagger.models.License) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) Operation(io.swagger.models.Operation) BasicAuthorization(io.swagger.models.resourcelisting.BasicAuthorization) ApiKeyAuthorization(io.swagger.models.resourcelisting.ApiKeyAuthorization) Authorization(io.swagger.models.resourcelisting.Authorization) OAuth2Authorization(io.swagger.models.resourcelisting.OAuth2Authorization) PassAs(io.swagger.models.PassAs) Swagger(io.swagger.models.Swagger) ApiKeyAuthorization(io.swagger.models.resourcelisting.ApiKeyAuthorization) Path(io.swagger.models.Path) ApiDeclaration(io.swagger.models.apideclaration.ApiDeclaration) OAuth2Authorization(io.swagger.models.resourcelisting.OAuth2Authorization) Info(io.swagger.models.Info) ApiInfo(io.swagger.models.resourcelisting.ApiInfo) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Contact(io.swagger.models.Contact) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) AuthorizationCodeGrant(io.swagger.models.resourcelisting.AuthorizationCodeGrant) ApiInfo(io.swagger.models.resourcelisting.ApiInfo) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ArrayModel(io.swagger.models.ArrayModel) ImplicitGrant(io.swagger.models.resourcelisting.ImplicitGrant) Api(io.swagger.models.apideclaration.Api) AuthorizationScope(io.swagger.models.AuthorizationScope)

Example 13 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project swagger-parser by swagger-api.

the class SwaggerConverter method convertApiKeySecurityScheme.

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;
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) SecurityScheme(io.swagger.v3.oas.models.security.SecurityScheme)

Example 14 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project ballerina by ballerina-lang.

the class SwaggerServiceMapper method createSecurityDefinitionsModel.

/**
 * Creates the security definition models for swagger definition.
 * @param annotationAttributeValue The annotation attribute value for security definitions.
 * @param swagger The swagger definition.
 */
private void createSecurityDefinitionsModel(AnnAttributeValue annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = new HashMap<>();
        for (AnnAttributeValue authorizationValues : annotationAttributeValue.getAttributeValueArray()) {
            AnnAttachmentInfo authAnnotationAttachment = authorizationValues.getAnnotationAttachmentValue();
            Map<String, AnnAttributeValue> authAnnAttributeValueMap = SwaggerUtils.convertToAttributeMap(authAnnotationAttachment);
            if (null != authAnnAttributeValueMap.get("name") && null != authAnnAttributeValueMap.get("authType")) {
                String name = authAnnAttributeValueMap.get("name").getStringValue();
                String type = authAnnAttributeValueMap.get("authType").getStringValue();
                String description = "";
                if (null != authAnnAttributeValueMap.get("description")) {
                    description = authAnnAttributeValueMap.get("description").getStringValue();
                }
                if ("basic".equals(type)) {
                    BasicAuthDefinition basicAuthDefinition = new BasicAuthDefinition();
                    basicAuthDefinition.setDescription(description);
                    securitySchemeDefinitionMap.put(name, basicAuthDefinition);
                } else if ("apiKey".equals(type)) {
                    ApiKeyAuthDefinition apiKeyAuthDefinition = new ApiKeyAuthDefinition();
                    apiKeyAuthDefinition.setName(authAnnAttributeValueMap.get("apiName").getStringValue());
                    apiKeyAuthDefinition.setIn(In.forValue(authAnnAttributeValueMap.get("in").getStringValue()));
                    apiKeyAuthDefinition.setDescription(description);
                    securitySchemeDefinitionMap.put(name, apiKeyAuthDefinition);
                } else if ("oauth2".equals(type)) {
                    OAuth2Definition oAuth2Definition = new OAuth2Definition();
                    oAuth2Definition.setFlow(authAnnAttributeValueMap.get("flow").getStringValue());
                    oAuth2Definition.setAuthorizationUrl(authAnnAttributeValueMap.get("authorizationUrl").getStringValue());
                    oAuth2Definition.setTokenUrl(authAnnAttributeValueMap.get("tokenUrl").getStringValue());
                    this.createSecurityDefinitionScopesModel(authAnnAttributeValueMap.get("authorizationScopes"), oAuth2Definition);
                    oAuth2Definition.setDescription(description);
                    securitySchemeDefinitionMap.put(name, oAuth2Definition);
                }
            }
        }
        swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
    }
}
Also used : AnnAttachmentInfo(org.ballerinalang.util.codegen.AnnAttachmentInfo) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) HashMap(java.util.HashMap) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) AnnAttributeValue(org.ballerinalang.util.codegen.AnnAttributeValue) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition)

Example 15 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project killbill by killbill.

the class KillBillApiDefinition method beforeScan.

@Override
public void beforeScan(final io.swagger.jaxrs.Reader reader, final Swagger swagger) {
    BasicAuthDefinition basicAuthDefinition = new BasicAuthDefinition();
    swagger.addSecurityDefinition(BASIC_AUTH_SCHEME, basicAuthDefinition);
    ApiKeyAuthDefinition xKillbillApiKey = new ApiKeyAuthDefinition("X-Killbill-ApiKey", In.HEADER);
    swagger.addSecurityDefinition(API_KEY_SCHEME, xKillbillApiKey);
    ApiKeyAuthDefinition xKillbillApiSecret = new ApiKeyAuthDefinition("X-Killbill-ApiSecret", In.HEADER);
    swagger.addSecurityDefinition(API_SECRET_SCHEME, xKillbillApiSecret);
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition)

Aggregations

ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)15 Test (org.testng.annotations.Test)7 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)6 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)6 Operation (io.swagger.models.Operation)5 Swagger (io.swagger.models.Swagger)5 BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)5 HashMap (java.util.HashMap)5 Contact (io.swagger.models.Contact)4 Info (io.swagger.models.Info)4 Path (io.swagger.models.Path)4 RefModel (io.swagger.models.RefModel)4 QueryParameter (io.swagger.models.parameters.QueryParameter)4 Model (io.swagger.models.Model)3 LongProperty (io.swagger.models.properties.LongProperty)3 License (io.swagger.models.License)2 Person (io.swagger.models.Person)2 Response (io.swagger.models.Response)2 BodyParameter (io.swagger.models.parameters.BodyParameter)2 PathParameter (io.swagger.models.parameters.PathParameter)2