principalClass; + private final PrincipalFactory
factory; + private final Authorizer
authorizer; + private final UnauthorizedHandler unauthorizedHandler; + + /** + * Construct the ApiKeyBundle using the provided Principal class, PrincipalFactory and + * Authorizer. + * + * @param principalClass The class of the class extending the Principal type. + * @param factory The PrincipalFactory instance, which can create new P objects. + * @param authorizer The Authorizer instance, which can create new P objects. + */ + public ApiKeyBundle(Class
principalClass, PrincipalFactory
factory, + Authorizer
authorizer) { + this(principalClass, factory, authorizer, new DefaultUnauthorizedHandler()); + } + + /** + * Construct the ApiKeyBundle using the provided Principal class, PrincipalFactory, + * Authorizer and UnauthorizedHandler. + * + * @param principalClass The class of the class extending the Principal type. + * @param factory The PrincipalFactory instance, which can create new P objects. + * @param authorizer The Authorizer instance, which can create new P objects. + * @param unauthorizedHandler The UnauthorizedHandler instance. + */ + public ApiKeyBundle(Class
principalClass, PrincipalFactory
factory, + Authorizer
authorizer, UnauthorizedHandler unauthorizedHandler) {
+ this.principalClass = checkNotNull(principalClass);
+ this.factory = checkNotNull(factory);
+ this.authorizer = checkNotNull(authorizer);
+ this.unauthorizedHandler = checkNotNull(unauthorizedHandler);
+ }
+
@Override
public void initialize(Bootstrap> bootstrap) {
}
@@ -32,25 +75,34 @@ public void run(T bundleConfiguration, Environment environment) throws Exception
Optional createBasicCredentialAuthFilter(AuthConfiguration config,
+ MetricRegistry metrics) {
+ final BasicCredentialAuthFilter authFilter =
+ new BasicCredentialAuthFilter.Builder ()
+ .setAuthenticator(createAuthenticator(config, metrics))
+ .setRealm(config.getRealm())
+ .setAuthorizer(authorizer)
+ .setUnauthorizedHandler(unauthorizedHandler)
+ .buildAuthFilter();
+ return authFilter;
}
- private BasicAuthFactory
+ implements Authenticator factory;
- BasicCredentialsAuthenticator(ApiKeyProvider provider) {
+ BasicCredentialsAuthenticator(ApiKeyProvider provider, PrincipalFactory factory) {
this.provider = checkNotNull(provider);
+ this.factory = checkNotNull(factory);
}
@Override
- public Optional authenticate(BasicCredentials credentials)
throws AuthenticationException {
checkNotNull(credentials);
@@ -34,6 +38,6 @@ public Optional the type of principal
+ */
+public interface PrincipalFactory {
+ /**
+ * Create an instance of P from the specified name.
+ */
+ P create(String name);
+}
diff --git a/src/test/java/io/dropwizard/bundles/apikey/ApiKeyBundleClientTest.java b/src/test/java/io/dropwizard/bundles/apikey/ApiKeyBundleClientTest.java
index b6b011a..816ec43 100644
--- a/src/test/java/io/dropwizard/bundles/apikey/ApiKeyBundleClientTest.java
+++ b/src/test/java/io/dropwizard/bundles/apikey/ApiKeyBundleClientTest.java
@@ -1,9 +1,12 @@
package io.dropwizard.bundles.apikey;
import io.dropwizard.auth.Auth;
-import io.dropwizard.auth.AuthFactory;
-import io.dropwizard.auth.basic.BasicAuthFactory;
+import io.dropwizard.auth.AuthDynamicFeature;
+import io.dropwizard.auth.AuthValueFactoryProvider;
+import io.dropwizard.auth.PermitAllAuthorizer;
+import io.dropwizard.auth.basic.BasicCredentialAuthFilter;
import io.dropwizard.testing.junit.ResourceTestRule;
+import java.security.Principal;
import java.util.Base64;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -31,7 +34,7 @@ public String insecure() {
@GET
@Path("/secure")
- public String secure(@Auth String application) {
+ public String secure(@Auth Principal application) {
return "secure";
}
}
@@ -49,13 +52,21 @@ public ApiKey get(String username) {
}
};
- private final BasicCredentialsAuthenticator authenticator =
- new BasicCredentialsAuthenticator(provider);
+ private final BasicCredentialsAuthenticator