Dropwizard JWT Authentication

JSON Web Token based authentication for Dropwizard.

License

License

Categories

Categories

DropWizard Container Microservices
GroupId

GroupId

de.borntohula.dropwizard
ArtifactId

ArtifactId

dropwizard-auth-jwt
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Dropwizard JWT Authentication
JSON Web Token based authentication for Dropwizard.
Project URL

Project URL

https://github.com/andban/dropwizard-auth-jwt
Source Code Management

Source Code Management

https://github.com/andban/dropwizard-auth-jwt

Download dropwizard-auth-jwt

How to add to project

<!-- https://jarcasting.com/artifacts/de.borntohula.dropwizard/dropwizard-auth-jwt/ -->
<dependency>
    <groupId>de.borntohula.dropwizard</groupId>
    <artifactId>dropwizard-auth-jwt</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/de.borntohula.dropwizard/dropwizard-auth-jwt/
implementation 'de.borntohula.dropwizard:dropwizard-auth-jwt:0.1.1'
// https://jarcasting.com/artifacts/de.borntohula.dropwizard/dropwizard-auth-jwt/
implementation ("de.borntohula.dropwizard:dropwizard-auth-jwt:0.1.1")
'de.borntohula.dropwizard:dropwizard-auth-jwt:jar:0.1.1'
<dependency org="de.borntohula.dropwizard" name="dropwizard-auth-jwt" rev="0.1.1">
  <artifact name="dropwizard-auth-jwt" type="jar" />
</dependency>
@Grapes(
@Grab(group='de.borntohula.dropwizard', module='dropwizard-auth-jwt', version='0.1.1')
)
libraryDependencies += "de.borntohula.dropwizard" % "dropwizard-auth-jwt" % "0.1.1"
[de.borntohula.dropwizard/dropwizard-auth-jwt "0.1.1"]

Dependencies

compile (2)

Group / Artifact Type Version
io.dropwizard : dropwizard-auth jar 0.8.1
org.bitbucket.b_c : jose4j jar 0.4.1

test (3)

Group / Artifact Type Version
org.glassfish.jersey.test-framework : jersey-test-framework-core jar 2.17
org.glassfish.jersey.test-framework.providers : jersey-test-framework-provider-grizzly2 jar 2.17
io.dropwizard : dropwizard-testing jar 0.8.1

Project Modules

There are no modules declared in this project.

dropwizard-auth-jwt Build Status

JSON Web Token based authentication for Dropwizard 0.8.x using the jose4j library.

Getting Started

Maven:

<dependency>
    <groupId>de.borntohula.dropwizard</groupId>
    <artifactId>dropwizard-auth-jwt</artifactId>
    <version>0.1.1</version>
</dependency>

Gradle:

compile 'de.borntohula.dropwizard:dropwizard-auth-jwt:0.1.1'

Examples

The JwtAuthFactory enables JSON Web Token authentication, and requires an authenticator which transforms the token string into a principal (see jose4j on how to consume JWTs):

@Override
public void run(ExampleConfiguration config, Environment environment) {
    final JsonWebKey jwk = OctJwkGenerator.generateJwk(2048);

    environment.jersey().register(AuthFactory.binder(
            new JwtAuthFactory<User>(new ExampleAuthenticator(jwk.getKey()),
                                     "MyRealm",
                                      User.class));
}

The abstract BaseJwtAuthenticator class provides simple validation and processing through a given JwtConsumer. Only the creation of the principal from the the claims send through the token needs to be implemented:

public class ExampleAuthenticator extends BaseJwtAuthenticator<User> {
    public ExampleAuthenticator(Key verificationKey) {
        super(new JwtConsumerBuilder()
                .setRequireExpirationTime()
                .setAllowedClockSkewInSeconds(30)
                .setRequireSubject()
                .setExpectedIssuer("Issuer")
                .setExpectedAudience("Audience")
                .setVerificationKey(verificationKey)
                .build());
    }

    @Override
    public Optional<User> validateClaims(JwtClaims claims)
            throws AuthenticationException {
        if (TokenRegistry.getInstance().isRevoked(claims.getJwtId()) {
            return Optional.absent();
        }

        return Optional.of(new User(jwtClaims.getSubject()));
    }
}

License

Apache 2.0 License. See LICENSE for further information.

Versions

Version
0.1.1
0.1.0