Search in sources :

Example 6 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project java-chassis by ServiceComb.

the class SwaggerDefinitionProcessor method convertApiKey.

private SecuritySchemeDefinition convertApiKey(io.swagger.annotations.ApiKeyAuthDefinition annotation) {
    if (StringUtils.isEmpty(annotation.name())) {
        return null;
    }
    ApiKeyAuthDefinition definition = new ApiKeyAuthDefinition();
    definition.setDescription(emptyAsNull(annotation.description()));
    definition.setIn(In.forValue(annotation.in().name()));
    definition.setName(annotation.name());
    return definition;
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition)

Example 7 with ApiKeyAuthDefinition

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

the class AuthSerializationTest method testHeaderKeyToYaml.

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

Example 8 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method addSecuritySchemeToSwaggerDefinition.

private void addSecuritySchemeToSwaggerDefinition(Swagger swagger, API api) {
    KeyMgtConfigurations keyMgtConfigurations = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getKeyManagerConfigs();
    if ((api.getSecurityScheme() & 2) == 2) {
        // apikey
        log.debug("API security scheme : API Key Scheme");
        if (swagger.getSecurityDefinitions() == null || !swagger.getSecurityDefinitions().containsKey(APIMgtConstants.SWAGGER_APIKEY)) {
            swagger.securityDefinition(APIMgtConstants.SWAGGER_APIKEY, new ApiKeyAuthDefinition(APIMgtConstants.SWAGGER_APIKEY, In.HEADER));
        }
    }
    if ((api.getSecurityScheme() & 1) == 1) {
        log.debug("API security Scheme : Oauth");
        OAuth2Definition oAuth2Definition = new OAuth2Definition();
        oAuth2Definition = oAuth2Definition.application(keyMgtConfigurations.getTokenEndpoint());
        oAuth2Definition.setScopes(Collections.emptyMap());
        if (swagger.getSecurityDefinitions() == null || !swagger.getSecurityDefinitions().containsKey(APIMgtConstants.OAUTH2SECURITY)) {
            swagger.securityDefinition(APIMgtConstants.OAUTH2SECURITY, oAuth2Definition);
        }
    }
}
Also used : ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) OAuth2Definition(io.swagger.models.auth.OAuth2Definition)

Example 9 with ApiKeyAuthDefinition

use of io.swagger.models.auth.ApiKeyAuthDefinition in project endpoints-java by cloudendpoints.

the class SwaggerGenerator method writeApiMethod.

private void writeApiMethod(ApiMethodConfig methodConfig, ApiConfig apiConfig, Swagger swagger, GenerationContext genCtx) throws ApiConfigException {
    Path path = getOrCreatePath(swagger, methodConfig);
    Operation operation = new Operation();
    operation.setOperationId(getOperationId(apiConfig, methodConfig));
    operation.setDescription(methodConfig.getDescription());
    Collection<String> pathParameters = methodConfig.getPathParameters();
    for (ApiParameterConfig parameterConfig : methodConfig.getParameterConfigs()) {
        switch(parameterConfig.getClassification()) {
            case API_PARAMETER:
                boolean isPathParameter = pathParameters.contains(parameterConfig.getName());
                SerializableParameter parameter = isPathParameter ? new PathParameter() : new QueryParameter();
                parameter.setName(parameterConfig.getName());
                parameter.setDescription(parameterConfig.getDescription());
                boolean required = isPathParameter || (!parameterConfig.getNullable() && parameterConfig.getDefaultValue() == null);
                if (parameterConfig.isRepeated()) {
                    TypeToken<?> t = parameterConfig.getRepeatedItemSerializedType();
                    parameter.setType("array");
                    Property p = getSwaggerArrayProperty(t);
                    if (parameterConfig.isEnum()) {
                        // TODO: Not sure if this is the right check
                        ((StringProperty) p).setEnum(getEnumValues(t));
                    }
                    parameter.setItems(p);
                } else if (parameterConfig.isEnum()) {
                    parameter.setType("string");
                    parameter.setEnum(getEnumValues(parameterConfig.getType()));
                    parameter.setRequired(required);
                } else {
                    parameter.setType(TYPE_TO_STRING_MAP.get(parameterConfig.getSchemaBaseType().getType()));
                    parameter.setFormat(TYPE_TO_FORMAT_MAP.get(parameterConfig.getSchemaBaseType().getType()));
                    parameter.setRequired(required);
                }
                operation.parameter(parameter);
                break;
            case RESOURCE:
                TypeToken<?> requestType = parameterConfig.getSchemaBaseType();
                Schema schema = genCtx.schemata.getOrAdd(requestType, apiConfig);
                BodyParameter bodyParameter = new BodyParameter();
                bodyParameter.setName("body");
                bodyParameter.setSchema(new RefModel(schema.name()));
                operation.addParameter(bodyParameter);
                break;
            case UNKNOWN:
                throw new IllegalArgumentException("Unclassifiable parameter type found.");
            case INJECTED:
                // Do nothing, these are synthetic and created by the framework.
                break;
        }
    }
    Response response = new Response().description("A successful response");
    if (methodConfig.hasResourceInResponse()) {
        TypeToken<?> returnType = ApiAnnotationIntrospector.getSchemaType(methodConfig.getReturnType(), apiConfig);
        Schema schema = genCtx.schemata.getOrAdd(returnType, apiConfig);
        response.setSchema(new RefProperty(schema.name()));
    }
    operation.response(200, response);
    writeAuthConfig(swagger, methodConfig, operation);
    if (methodConfig.isApiKeyRequired()) {
        List<Map<String, List<String>>> security = operation.getSecurity();
        // security requirements, add a new one for just the API key.
        if (security != null) {
            for (Map<String, List<String>> securityEntry : security) {
                securityEntry.put(API_KEY, ImmutableList.<String>of());
            }
        } else {
            operation.addSecurity(API_KEY, ImmutableList.<String>of());
        }
        Map<String, SecuritySchemeDefinition> definitions = swagger.getSecurityDefinitions();
        if (definitions == null || !definitions.containsKey(API_KEY)) {
            swagger.securityDefinition(API_KEY, new ApiKeyAuthDefinition(API_KEY_PARAM, In.QUERY));
        }
    }
    path.set(methodConfig.getHttpMethod().toLowerCase(), operation);
    addDefinedMetricCosts(genCtx.limitMetrics, operation, methodConfig.getMetricCosts());
}
Also used : SerializableParameter(io.swagger.models.parameters.SerializableParameter) QueryParameter(io.swagger.models.parameters.QueryParameter) RefModel(io.swagger.models.RefModel) Schema(com.google.api.server.spi.config.model.Schema) StringProperty(io.swagger.models.properties.StringProperty) Operation(io.swagger.models.Operation) BodyParameter(io.swagger.models.parameters.BodyParameter) PathParameter(io.swagger.models.parameters.PathParameter) RefProperty(io.swagger.models.properties.RefProperty) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) LongProperty(io.swagger.models.properties.LongProperty) Property(io.swagger.models.properties.Property) DoubleProperty(io.swagger.models.properties.DoubleProperty) DateTimeProperty(io.swagger.models.properties.DateTimeProperty) ByteArrayProperty(io.swagger.models.properties.ByteArrayProperty) RefProperty(io.swagger.models.properties.RefProperty) FloatProperty(io.swagger.models.properties.FloatProperty) DateProperty(io.swagger.models.properties.DateProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) BooleanProperty(io.swagger.models.properties.BooleanProperty) Path(io.swagger.models.Path) ApiParameterConfig(com.google.api.server.spi.config.model.ApiParameterConfig) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Response(io.swagger.models.Response) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 10 with ApiKeyAuthDefinition

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

the class LegacyConverterTest method convertSingleFile.

/**
 * reads a single-file swagger definition
 */
@Test
public void convertSingleFile() throws Exception {
    Swagger swagger = converter.read("src/test/resources/specs/v1_2/singleFile.json");
    assertTrue(swagger.getSecurityDefinitions().size() == 2);
    SecuritySchemeDefinition auth = swagger.getSecurityDefinitions().get("oauth2");
    assertNotNull(auth);
    assertEquals(auth.getClass(), OAuth2Definition.class);
    OAuth2Definition oauth2 = (OAuth2Definition) auth;
    assertEquals(oauth2.getFlow(), "implicit");
    assertEquals(oauth2.getAuthorizationUrl(), "http://petstore.swagger.io/oauth/dialog");
    assertTrue(oauth2.getScopes().size() == 2);
    Map<String, String> scopes = oauth2.getScopes();
    assertEquals(scopes.get("email"), "Access to your email address");
    assertEquals(scopes.get("pets"), "Access to your pets");
    auth = swagger.getSecurityDefinitions().get("apiKey");
    assertNotNull(auth);
    assertEquals(auth.getClass(), ApiKeyAuthDefinition.class);
    ApiKeyAuthDefinition apiKey = (ApiKeyAuthDefinition) auth;
    assertEquals(apiKey.getName(), "api_key");
    assertEquals(apiKey.getIn(), In.HEADER);
    assertEquals(swagger.getSwagger(), "2.0");
    assertEquals(swagger.getHost(), "petstore.swagger.io");
    assertEquals(swagger.getBasePath(), "/api");
    assertNotNull(swagger.getInfo());
    Info info = swagger.getInfo();
    assertEquals(info.getVersion(), "1.0.0");
    assertEquals(info.getTitle(), "Swagger Sample App");
    assertEquals(info.getTermsOfService(), "http://swagger.io/terms/");
    Contact contact = info.getContact();
    assertEquals(contact.getUrl(), "apiteam@swagger.io");
    License license = info.getLicense();
    assertEquals(license.getName(), "Apache 2.0");
    assertEquals(license.getUrl(), "http://www.apache.org/licenses/LICENSE-2.0.html");
    assertTrue(swagger.getDefinitions().size() == 3);
    assertTrue(swagger.getPaths().size() == 5);
    Operation patchOperation = swagger.getPaths().get("/pet/{petId}").getPatch();
    List<Map<String, List<String>>> security = patchOperation.getSecurity();
    assertTrue(security.size() == 1);
    Map<String, List<String>> securityDetail = security.get(0);
    String key = securityDetail.keySet().iterator().next();
    assertEquals(key, "oauth2");
    List<String> oauth2Scopes = securityDetail.get(key);
    assertEquals(oauth2Scopes.size(), 1);
    assertEquals(oauth2Scopes.get(0), "test:anything");
    Operation fetchOperation = swagger.getPaths().get("/pet/findByStatus").getGet();
    QueryParameter param = (QueryParameter) fetchOperation.getParameters().get(0);
    assertEquals(param.getDefaultValue(), "available");
    List<String> _enum = param.getEnum();
    assertEquals(_enum.get(0), "available");
    assertEquals(_enum.get(1), "pending");
    assertEquals(_enum.get(2), "sold");
}
Also used : QueryParameter(io.swagger.models.parameters.QueryParameter) OAuth2Definition(io.swagger.models.auth.OAuth2Definition) License(io.swagger.models.License) SecuritySchemeDefinition(io.swagger.models.auth.SecuritySchemeDefinition) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) Contact(io.swagger.models.Contact) ApiKeyAuthDefinition(io.swagger.models.auth.ApiKeyAuthDefinition) Swagger(io.swagger.models.Swagger) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Test(org.testng.annotations.Test)

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