Search in sources :

Example 1 with BasicAuthDefinition

use of io.swagger.models.auth.BasicAuthDefinition 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 2 with BasicAuthDefinition

use of io.swagger.models.auth.BasicAuthDefinition in project candlepin by candlepin.

the class CandlepinSwaggerConfig method afterScan.

@Override
public void afterScan(Reader reader, Swagger swagger) {
    swagger.setExternalDocs(new ExternalDocs("Project website: ", "http://candlepinproject.org/"));
    BasicAuthDefinition basic = new BasicAuthDefinition();
    basic.setDescription("Candlepin requires HTTP Basic authentication of an owner");
    swagger.addSecurityDefinition(BASIC_AUTH, basic);
    SecurityRequirement req = new SecurityRequirement();
    req.setRequirements(BASIC_AUTH, new ArrayList<>());
    swagger.addSecurity(req);
}
Also used : BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) ExternalDocs(io.swagger.models.ExternalDocs) SecurityRequirement(io.swagger.models.SecurityRequirement)

Example 3 with BasicAuthDefinition

use of io.swagger.models.auth.BasicAuthDefinition 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)

Example 4 with BasicAuthDefinition

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

the class AuthSerializationTest method testBasicAuth.

@Test(description = "it should convert serialize a basic auth model")
public void testBasicAuth() throws IOException {
    final BasicAuthDefinition auth = new BasicAuthDefinition();
    final String json = "{\"type\":\"basic\"}";
    SerializationMatchers.assertEqualsToJson(auth, json);
}
Also used : BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Test(org.testng.annotations.Test)

Example 5 with BasicAuthDefinition

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

the class SwaggerTest method testSecurityDefinition.

@Test
public void testSecurityDefinition() {
    // given
    SecuritySchemeDefinition securityDefinition = new BasicAuthDefinition();
    String name = "name";
    // when
    swagger.securityDefinition(name, securityDefinition);
    // then
    assertEquals(swagger.getSecurityDefinitions().get(name), securityDefinition, "Must be able to retrieve the added security definition");
}
Also used : SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) BasicAuthDefinition(io.swagger.models.auth.BasicAuthDefinition) Test(org.testng.annotations.Test)

Aggregations

BasicAuthDefinition (io.swagger.models.auth.BasicAuthDefinition)10 ApiKeyAuthDefinition (io.swagger.models.auth.ApiKeyAuthDefinition)5 SecuritySchemeDefinition (io.swagger.models.auth.SecuritySchemeDefinition)4 OAuth2Definition (io.swagger.models.auth.OAuth2Definition)3 HashMap (java.util.HashMap)3 Test (org.testng.annotations.Test)3 Swagger (io.swagger.models.Swagger)2 SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)1 Reader (io.swagger.jaxrs.Reader)1 ReaderListener (io.swagger.jaxrs.config.ReaderListener)1 ArrayModel (io.swagger.models.ArrayModel)1 AuthorizationScope (io.swagger.models.AuthorizationScope)1 Contact (io.swagger.models.Contact)1 ExternalDocs (io.swagger.models.ExternalDocs)1 Info (io.swagger.models.Info)1 License (io.swagger.models.License)1 Model (io.swagger.models.Model)1 Operation (io.swagger.models.Operation)1 PassAs (io.swagger.models.PassAs)1 Path (io.swagger.models.Path)1