Skip to content

Commit

Permalink
Merge pull request federecio#60 from smoketurner/jp-auth-filter
Browse files Browse the repository at this point in the history
Add auth filter
  • Loading branch information
jplock committed Dec 26, 2016
2 parents 534feb7 + 7f758be commit 77c2ba3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.smoketurner</groupId>
<artifactId>dropwizard-swagger</artifactId>
<version>1.0.2-1-SNAPSHOT</version>
<version>1.0.5-1-SNAPSHOT</version>

<name>Dropwizard Swagger support</name>
<description>A simple way to document your REST APIs in DropWizard using Swagger</description>
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/io/federecio/dropwizard/swagger/AuthParamFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2014 Federico Recio
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.federecio.dropwizard.swagger;

import java.util.List;
import java.util.Map;
import io.swagger.core.filter.SwaggerSpecFilter;
import io.swagger.model.ApiDescription;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.Property;

/**
* This SwaggerSpecFilter checks for the presence of an
* access=internal @ApiParam annotation, and hides the parameter if it is
* present.
*
* This is primarily useful for hiding Dropwizard @Auth parameters.
*
* From: https://www.reonomy.com/augmenting-dropwizard-with-swagger/
*/
public class AuthParamFilter implements SwaggerSpecFilter {
@Override
public boolean isOperationAllowed(Operation operation, ApiDescription api,
Map<String, List<String>> params, Map<String, String> cookies,
Map<String, List<String>> headers) {
return true;
}

@Override
public boolean isParamAllowed(Parameter parameter, Operation operation,
ApiDescription api, Map<String, List<String>> params,
Map<String, String> cookies, Map<String, List<String>> headers) {
String access = parameter.getAccess();
if (access != null && access.equals("internal"))
return false;
return true;
}

@Override
public boolean isPropertyAllowed(Model model, Property property,
String propertyName, Map<String, List<String>> params,
Map<String, String> cookies, Map<String, List<String>> headers) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.dropwizard.views.ViewBundle;
import io.swagger.config.FilterFactory;
import io.swagger.converter.ModelConverters;
import io.swagger.jackson.ModelResolver;
import io.swagger.jaxrs.listing.ApiListingResource;
Expand Down Expand Up @@ -60,6 +61,8 @@ public void run(T configuration, Environment environment) throws Exception {

swaggerBundleConfiguration.build(configurationHelper.getUrlPattern());

FilterFactory.setFilter(new AuthParamFilter());

environment.jersey().register(new ApiListingResource());
environment.jersey().register(new SwaggerSerializers());
environment.jersey().register(new SwaggerResource(
Expand Down

0 comments on commit 77c2ba3

Please sign in to comment.