GithubHelp home page GithubHelp logo

bbeck / dropwizard-api-key-bundle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mainstreethub/dropwizard-api-key-bundle

0.0 2.0 0.0 33 KB

A Dropwizard bundle that allows your service to be protected by API keys.

License: Apache License 2.0

Java 100.00%

dropwizard-api-key-bundle's Introduction

dropwizard-api-key-bundle

A Dropwizard bundle that provides a simple way to manage API keys for callers of your service. The bundle provides support for authentication only; authorization is supported by optionally providing an Authorizer as documented below.

[Build Status] (http://travis-ci.org/dropwizard-bundles/dropwizard-api-key-bundle)

Getting Started

Just add this maven dependency to get started:

<dependency>
  <groupId>io.dropwizard-bundles</groupId>
  <artifactId>dropwizard-api-key-bundle</artifactId>
  <version>1.0.0</version>
</dependency>

If you only need authentication and a default Principal implementation add the default version of the bundle to your environment:

public class MyApplication extends Application<MyConfiguration> {
  @Override
  public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    bootstrap.addBundle(new ApiKeyBundle<>());
  }

  @Override
  public void run(MyConfiguration cfg, Environment env) throws Exception {
    // ...
  }
}

If you need to provide an Authorizer or a different Principal (extending type), or both, add the bundle to your environment and provide the type extending the Principal interface, an implementation of the Authorizer and PrincipalFactory as appropriate:

public class MyApplication extends Application<MyConfiguration> {
  @Override
  public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    bootstrap.addBundle(new PrincipalApiKeyBundle<>(User.class, new PrincipalFactory<User>() {
      @Override
      public User create(String name) {
        // Do something interesting...
        return new User(name);
      }}, new Authorizer<User>() {
        @Override
        public boolean authorize(User user, String role) {
          return user.getName().equals("application-1") && role.equals("ADMIN");
        }
    }));
  }

  @Override
  public void run(MyConfiguration cfg, Environment env) throws Exception {
    // ...
  }
}

Additionally you can also pass an UnauthorizedHandler when creating the bundle, which is useful if you need to customize the unauthorized response (e.g. type or entity).

You will also need to make your MyConfiguration class implement ApiKeyBundleConfiguration in order to provide the bundle with the necessary information it needs to know your API keys.

public class MyConfiguration implements ApiKeyBundleConfiguration {
  @Valid
  @NotNull
  @JsonProperty("authentication")
  private final ApiKeyConfiguration apiKeyConfiguration = null;

  /**
   * Return the API key configuration.
   */
  @Override
  public ApiKeyConfiguration getApiKeyConfiguration() {
    return apiKeyConfiguration;
  }
}

Now you can use API key based authentication in your application by declaring a method on a resource that has an @Auth annotated Principal parameter (or an extending type). See the Dropwizard Authentication documentation for more details. The name of the Principal will be the name of the application that made the request if the authentication process was successful.

As far as configuration goes you can define your API keys, and roles for those applications in your application's config file. Assuming, like in the above example, you called your API key configuration element authentication then you can use a config file like this:

authentication:
  basic-http:
    cache-spec: maximumSize=1000, expireAfterAccess=10m
    realm: MyApplication
    keys:
      application-1: api-key-1
      application-2: api-key-2
    roles:
      application-1:
        - admin
        - user
      application-2:
        - user

dropwizard-api-key-bundle's People

Contributors

alexwen avatar bbeck avatar msh-jenkins avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.