Skip to content

Instantly share code, notes, and snippets.

@Lugribossk
Created December 19, 2014 13:06
Show Gist options
  • Save Lugribossk/5b9d94b92a33f281c3b9 to your computer and use it in GitHub Desktop.
Save Lugribossk/5b9d94b92a33f281c3b9 to your computer and use it in GitHub Desktop.
Dropwizard 0.8.0-rc1 error with OAuthFactory
package bo.gotthardt.oauth2;
import com.google.common.base.Optional;
import io.dropwizard.auth.Auth;
import io.dropwizard.auth.AuthFactory;
import io.dropwizard.auth.AuthenticationException;
import io.dropwizard.auth.Authenticator;
import io.dropwizard.auth.oauth.OAuthFactory;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.ClassRule;
import org.junit.Test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
public class OAuthTest {
@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new DemoResource())
.addResource(AuthFactory.binder(new OAuthFactory<>(new DemoAuthenticator(), "demo", String.class)))
.build();
@Test
public void test() {
// Will throw a NullPointerException in OAuthFactory#provide() due to request being null.
resources.client().target("/demo/hello").request().get();
}
@Path("/demo")
public static class DemoResource {
@GET
@Path("/hello")
public String test(@Auth String username) {
return "Hello " + username;
}
}
public static class DemoAuthenticator implements Authenticator<String, String> {
@Override
public Optional<String> authenticate(String credentials) throws AuthenticationException {
return Optional.of("world");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment