Search in sources :

Example 1 with ApiKeyAuthDefinition

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

the class SwaggerSerializerTest method convertSpec.

@Test(description = "it should convert a spec")
public void convertSpec() throws IOException {
    final Model personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Model errorModel = ModelConverters.getInstance().read(Error.class).get("Error");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "value");
    info.setVendorExtension("x-test2", map);
    info.setVendorExtension("x-test", "value");
    final Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").securityDefinition("api-key", new ApiKeyAuthDefinition("key", In.HEADER)).scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel).model("Error", errorModel);
    final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id").deprecated(true);
    get.parameter(new QueryParameter().name("tags").description("tags to filter by").required(false).property(new StringProperty()));
    get.parameter(new PathParameter().name("petId").description("pet to fetch").property(new LongProperty()));
    final Response response = new Response().description("pets returned").schema(new RefProperty().asDefault("Person")).example("application/json", "fun!");
    final Response errorResponse = new Response().description("error response").schema(new RefProperty().asDefault("Error"));
    get.response(200, response).defaultResponse(errorResponse);
    final Operation post = new Operation().summary("adds a new pet").description("you can add a new pet this way").tag("Pet Operations").operationId("add pet").defaultResponse(errorResponse).parameter(new BodyParameter().description("the pet to add").schema(new RefModel().asDefault("Person")));
    swagger.path("/pets", new Path().get(get).post(post));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);
    SerializationMatchers.assertEqualsToJson(rebuilt, swaggerJson);
}
Also used : Path(io.swagger.models.Path) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) HashMap(java.util.HashMap) Error(io.swagger.models.Error) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) Contact(io.swagger.models.Contact) RefProperty(io.swagger.models.properties.RefProperty) Response(io.swagger.models.Response) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Person(io.swagger.models.Person) Test(org.testng.annotations.Test)

Example 2 with ApiKeyAuthDefinition

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

the class SwaggerSerializerTest method writeSpecWithParameterReferences.

@Test(description = "it should write a spec with parameter references")
public void writeSpecWithParameterReferences() throws IOException {
    final Model personModel = ModelConverters.getInstance().read(Person.class).get("Person");
    final Info info = new Info().version("1.0.0").title("Swagger Petstore");
    final Contact contact = new Contact().name("Swagger API Team").email("foo@bar.baz").url("http://swagger.io");
    info.setContact(contact);
    final Swagger swagger = new Swagger().info(info).host("petstore.swagger.io").securityDefinition("api-key", new ApiKeyAuthDefinition("key", In.HEADER)).scheme(Scheme.HTTP).consumes("application/json").produces("application/json").model("Person", personModel);
    final QueryParameter parameter = new QueryParameter().name("id").description("a common get parameter").property(new LongProperty());
    final Operation get = new Operation().produces("application/json").summary("finds pets in the system").description("a longer description").tag("Pet Operations").operationId("get pet by id").parameter(new RefParameter("foo"));
    swagger.parameter("foo", parameter).path("/pets", new Path().get(get));
    final String swaggerJson = Json.mapper().writeValueAsString(swagger);
    final Swagger rebuilt = Json.mapper().readValue(swaggerJson, Swagger.class);
    assertEquals(Json.pretty(swagger), Json.pretty(rebuilt));
}
Also used : Path(io.swagger.models.Path) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) QueryParameter(io.swagger.models.parameters.QueryParameter) RefParameter(io.swagger.models.parameters.RefParameter) Swagger(io.swagger.models.Swagger) LongProperty(io.swagger.models.properties.LongProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) Person(io.swagger.models.Person) Contact(io.swagger.models.Contact) Test(org.testng.annotations.Test)

Example 3 with ApiKeyAuthDefinition

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

the class AuthSerializationTest method testHeaderKeyToJson.

@Test(description = "it should convert serialize a header key model")
public void testHeaderKeyToJson() throws IOException {
    final ApiKeyAuthDefinition auth = new ApiKeyAuthDefinition().name("api-key").in(In.HEADER);
    final String json = "{\"type\":\"apiKey\",\"name\":\"api-key\",\"in\":\"header\"}";
    SerializationMatchers.assertEqualsToJson(auth, json);
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Test(org.testng.annotations.Test)

Example 4 with ApiKeyAuthDefinition

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

the class SwaggerDeserializerTest method testSecurityDefinition.

@Test
public void testSecurityDefinition() {
    String json = "{\n" + "  \"swagger\": \"2.0\",\n" + "  \"securityDefinitions\": {\n" + "    \"basic_auth\": {\n" + "      \"type\": \"basic\",\n" + "      \"x-foo\": \"basicBar\"\n" + "    },\n" + "    \"api_key\": {\n" + "      \"type\": \"apiKey\",\n" + "      \"name\": \"api_key\",\n" + "      \"in\": \"header\",\n" + "      \"description\": \"api key description\",\n" + "      \"x-foo\": \"apiKeyBar\"\n" + "    }\n" + "  },\n" + "  \"paths\": {\n" + "    \"/pet\": {\n" + "      \"get\": {\n" + "        \"security\": [\n" + "          {\n" + "            \"basic_auth\": [],\n" + "            \"api_key\": []\n" + "          }\n" + "        ]\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    SwaggerParser parser = new SwaggerParser();
    SwaggerDeserializationResult result = parser.readWithInfo(json);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<String>(messageList);
    Swagger swagger = result.getSwagger();
    assertNotNull(swagger.getSecurityDefinitions());
    assertTrue(swagger.getSecurityDefinitions().keySet().size() == 2);
    // Basic Authentication
    SecuritySchemeDefinition definitionBasic = swagger.getSecurityDefinitions().get("basic_auth");
    assertNotNull(definitionBasic);
    assertTrue(definitionBasic instanceof BasicAuthDefinition);
    assertEquals(definitionBasic.getVendorExtensions().get("x-foo"), "basicBar");
    // API Key Authentication
    SecuritySchemeDefinition definition = swagger.getSecurityDefinitions().get("api_key");
    assertNotNull(definition);
    assertTrue(definition instanceof ApiKeyAuthDefinition);
    ApiKeyAuthDefinition apiKey = (ApiKeyAuthDefinition) definition;
    assertEquals(apiKey.getName(), "api_key");
    assertEquals(apiKey.getIn(), In.HEADER);
    assertEquals(apiKey.getDescription(), "api key description");
    assertEquals(apiKey.getVendorExtensions().get("x-foo"), "apiKeyBar");
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 5 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(AnnotationAttachmentAttributeValueNode annotationAttributeValue, Swagger swagger) {
    if (null != annotationAttributeValue) {
        Map<String, SecuritySchemeDefinition> securitySchemeDefinitionMap = new HashMap<>();
        for (AnnotationAttachmentAttributeValueNode authorizationValues : annotationAttributeValue.getValueArray()) {
            if (authorizationValues instanceof AnnotationAttachmentNode) {
                AnnotationAttachmentNode authAnnotationAttachment = (AnnotationAttachmentNode) authorizationValues;
                Map<String, AnnotationAttachmentAttributeValueNode> authAttributes = this.listToMap(authAnnotationAttachment);
                if (authAttributes.containsKey("name") && authAttributes.containsKey("authType")) {
                    String name = this.getStringLiteralValue(authAttributes.get("name"));
                    String type = this.getStringLiteralValue(authAttributes.get("authType"));
                    String description = "";
                    if (authAttributes.containsKey("description")) {
                        description = this.getStringLiteralValue(authAttributes.get("description"));
                    }
                    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(this.getStringLiteralValue(authAttributes.get("apiName")));
                        apiKeyAuthDefinition.setIn(In.forValue(this.getStringLiteralValue(authAttributes.get("in"))));
                        apiKeyAuthDefinition.setDescription(description);
                        securitySchemeDefinitionMap.put(name, apiKeyAuthDefinition);
                    } else if ("oauth2".equals(type)) {
                        OAuth2Definition oAuth2Definition = new OAuth2Definition();
                        oAuth2Definition.setFlow(this.getStringLiteralValue(authAttributes.get("flow")));
                        oAuth2Definition.setAuthorizationUrl(this.getStringLiteralValue(authAttributes.get("authorizationUrl")));
                        oAuth2Definition.setTokenUrl(this.getStringLiteralValue(authAttributes.get("tokenUrl")));
                        this.createSecurityDefinitionScopesModel(authAttributes.get("authorizationScopes"), oAuth2Definition);
                        oAuth2Definition.setDescription(description);
                        securitySchemeDefinitionMap.put(name, oAuth2Definition);
                    }
                }
            }
        }
        swagger.setSecurityDefinitions(securitySchemeDefinitionMap);
    }
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) HashMap(java.util.HashMap) AnnotationAttachmentAttributeValueNode(org.ballerinalang.model.tree.expressions.AnnotationAttachmentAttributeValueNode) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)14 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