Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove Authenticator generic parameter from AuthFilterBuilder
Generic parameter that controls a type of a passed authenticator to the `AuthFilterBuilder` is not actually needed. Type-safety is already forced by the generic parameters C(credentials) and P(principal). The user has to provide a class that implements interface `Authenticator<C,P>`, and we don't care about concrete type of this implementation, because it doesn't affect the auth filter. This commit also moves the common logic of building auth filters to the `AuthFilterBuilder` and adds some comments for the public API.
- Loading branch information
Showing
9 changed files
with
93 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 63 additions & 25 deletions
88
dropwizard-auth/src/main/java/io/dropwizard/auth/AuthFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,98 @@ | ||
package io.dropwizard.auth; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import javax.annotation.Priority; | ||
import javax.ws.rs.Priorities; | ||
import javax.ws.rs.container.ContainerRequestFilter; | ||
import java.security.Principal; | ||
|
||
@Priority(Priorities.AUTHENTICATION) | ||
public abstract class AuthFilter<C, P extends Principal> implements ContainerRequestFilter { | ||
|
||
protected String prefix; | ||
protected String realm; | ||
protected Authenticator<C, P> authenticator; | ||
protected Authorizer<P> authorizer; | ||
protected UnauthorizedHandler unauthorizedHandler = new DefaultUnauthorizedHandler(); | ||
|
||
protected void setPrefix(String prefix) { | ||
this.prefix = prefix; | ||
} | ||
|
||
protected void setRealm(String realm) { | ||
this.realm = realm; | ||
} | ||
|
||
protected void setAuthenticator(Authenticator<C, P> authenticator) { | ||
this.authenticator = authenticator; | ||
} | ||
|
||
protected void setAuthorizer(Authorizer<P> authorizer) { | ||
this.authorizer = authorizer; | ||
} | ||
/** | ||
* Abstract builder for auth filters. | ||
* | ||
* @param <C> the type of credentials that the filter accepts | ||
* @param <P> the type of the principal that the filter accepts | ||
*/ | ||
public abstract static class AuthFilterBuilder<C, P extends Principal, T extends AuthFilter<C, P>> { | ||
|
||
public abstract static class AuthFilterBuilder<C, P extends Principal, T extends AuthFilter<C, P>, A extends Authenticator<C, P>> { | ||
protected String realm = "realm"; | ||
protected String prefix = "Basic"; | ||
protected Authenticator<C, P> authenticator; | ||
protected Authorizer<P> authorizer; | ||
private String realm = "realm"; | ||
private String prefix = "Basic"; | ||
private Authenticator<C, P> authenticator; | ||
private Authorizer<P> authorizer; | ||
|
||
public AuthFilterBuilder<C, P, T, A> setRealm(String realm) { | ||
/** | ||
* Sets the given realm | ||
* | ||
* @param realm a realm | ||
* @return the current builder | ||
*/ | ||
public AuthFilterBuilder<C, P, T> setRealm(String realm) { | ||
this.realm = realm; | ||
return this; | ||
} | ||
|
||
public AuthFilterBuilder<C, P, T, A> setPrefix(String prefix) { | ||
/** | ||
* Sets the given prefix | ||
* | ||
* @param prefix a prefix | ||
* @return the current builder | ||
*/ | ||
public AuthFilterBuilder<C, P, T> setPrefix(String prefix) { | ||
this.prefix = prefix; | ||
return this; | ||
} | ||
|
||
public AuthFilterBuilder<C, P, T, A> setAuthorizer(Authorizer<P> authorizer) { | ||
/** | ||
* Sets the given authorizer | ||
* | ||
* @param authorizer an {@link Authorizer} | ||
* @return the current builder | ||
*/ | ||
public AuthFilterBuilder<C, P, T> setAuthorizer(Authorizer<P> authorizer) { | ||
this.authorizer = authorizer; | ||
return this; | ||
} | ||
|
||
public AuthFilterBuilder<C, P, T, A> setAuthenticator(A authenticator) { | ||
/** | ||
* Sets the given authenticator | ||
* | ||
* @param authenticator an {@link Authenticator} | ||
* @return the current builder | ||
*/ | ||
public AuthFilterBuilder<C, P, T> setAuthenticator(Authenticator<C, P> authenticator) { | ||
this.authenticator = authenticator; | ||
return this; | ||
} | ||
|
||
public abstract T buildAuthFilter(); | ||
/** | ||
* Builds an instance of the filter with provided an authenticator, | ||
* an authorizer, a prefix, and a realm. | ||
* | ||
* @return a new instance of a filter | ||
*/ | ||
public T buildAuthFilter() { | ||
Preconditions.checkArgument(realm != null, "Realm is not set"); | ||
Preconditions.checkArgument(prefix != null, "Prefix is not set"); | ||
Preconditions.checkArgument(authenticator != null, "Authenticator is not set"); | ||
Preconditions.checkArgument(authorizer != null, "Authorizer is not set"); | ||
|
||
T authFilter = newInstance(); | ||
authFilter.authorizer = authorizer; | ||
authFilter.authenticator = authenticator; | ||
authFilter.prefix = prefix; | ||
authFilter.realm = realm; | ||
return authFilter; | ||
} | ||
|
||
protected abstract T newInstance(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters