Skip to content

Commit

Permalink
Fix AuthSuccessHandler to use HttpRequest and HttpResponse (#2562)
Browse files Browse the repository at this point in the history
Motivation:
Related: #2323

Result:
- (Breaking) `AuthSuccessHandler` and `AuthFailureHandler` now does not have type parameters.
  • Loading branch information
minwoox authored Mar 18, 2020
1 parent 46eff82 commit 8e0cdb1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,26 @@

import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.RpcRequest;
import com.linecorp.armeria.common.RpcResponse;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.Service;
import com.linecorp.armeria.server.ServiceRequestContext;

/**
* A callback which is invoked to handle an authorization failure indicated by {@link Authorizer}.
*
* @param <I> the type of incoming {@link Request}. Must be {@link HttpRequest} or {@link RpcRequest}.
* @param <O> the type of outgoing {@link Response}. Must be {@link HttpResponse} or {@link RpcResponse}.
*
* @see AuthServiceBuilder#onFailure(AuthFailureHandler)
*/
@FunctionalInterface
public interface AuthFailureHandler<I extends Request, O extends Response> {
public interface AuthFailureHandler {
/**
* Invoked when the authorization of the specified {@link Request} has failed.
* Invoked when the authorization of the specified {@link HttpRequest} has failed.
*
* @param delegate the next {@link Service} in the decoration chain
* @param ctx the {@link ServiceRequestContext} of {@code req}
* @param req the {@link Request} being handled
* @param cause {@code null} if {@code req} has been rejected by the {@link Authorizer}.
* @param ctx the {@link ServiceRequestContext}
* @param req the {@link HttpRequest} being handled
* @param cause {@code null} if the {@link HttpRequest} has been rejected by the {@link Authorizer}.
* non-{@code null} if the {@link Authorizer} raised an {@link Exception}.
*/
O authFailed(Service<I, O> delegate, ServiceRequestContext ctx,
I req, @Nullable Throwable cause) throws Exception;
HttpResponse authFailed(HttpService delegate, ServiceRequestContext ctx,
HttpRequest req, @Nullable Throwable cause) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ public static AuthServiceBuilder builder() {
}

private final Authorizer<HttpRequest> authorizer;
private final AuthSuccessHandler<HttpRequest, HttpResponse> successHandler;
private final AuthFailureHandler<HttpRequest, HttpResponse> failureHandler;
private final AuthSuccessHandler successHandler;
private final AuthFailureHandler failureHandler;

AuthService(HttpService delegate, Authorizer<HttpRequest> authorizer,
AuthSuccessHandler<HttpRequest, HttpResponse> successHandler,
AuthFailureHandler<HttpRequest, HttpResponse> failureHandler) {
AuthSuccessHandler successHandler, AuthFailureHandler failureHandler) {
super(delegate);
this.authorizer = authorizer;
this.successHandler = successHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public final class AuthServiceBuilder {

@Nullable
private Authorizer<HttpRequest> authorizer;
private AuthSuccessHandler<HttpRequest, HttpResponse> successHandler = Service::serve;
private AuthFailureHandler<HttpRequest, HttpResponse> failureHandler = (delegate, ctx, req, cause) -> {
private AuthSuccessHandler successHandler = Service::serve;
private AuthFailureHandler failureHandler = (delegate, ctx, req, cause) -> {
if (cause != null) {
AuthService.logger.warn("Unexpected exception during authorization.", cause);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ public <T> AuthServiceBuilder addTokenAuthorizer(
* Sets the {@link AuthSuccessHandler} which handles successfully authorized requests.
* By default, the request will be delegated to the next {@link HttpService}.
*/
public AuthServiceBuilder onSuccess(AuthSuccessHandler<HttpRequest, HttpResponse> successHandler) {
public AuthServiceBuilder onSuccess(AuthSuccessHandler successHandler) {
this.successHandler = requireNonNull(successHandler, "successHandler");
return this;
}
Expand All @@ -154,7 +154,7 @@ public AuthServiceBuilder onSuccess(AuthSuccessHandler<HttpRequest, HttpResponse
* By default, an exception thrown during authorization is logged at WARN level (if any) and a
* {@code 401 Unauthorized} response will be sent.
*/
public AuthServiceBuilder onFailure(AuthFailureHandler<HttpRequest, HttpResponse> failureHandler) {
public AuthServiceBuilder onFailure(AuthFailureHandler failureHandler) {
this.failureHandler = requireNonNull(failureHandler, "failureHandler");
return this;
}
Expand All @@ -167,15 +167,18 @@ public AuthService build(HttpService delegate) {
successHandler, failureHandler);
}

private AuthService build(HttpService delegate, Authorizer<HttpRequest> authorizer) {
return new AuthService(requireNonNull(delegate, "delegate"), authorizer,
successHandler, failureHandler);
}

/**
* Returns a newly-created decorator that decorates an {@link HttpService} with a new
* {@link AuthService} based on the {@link Authorizer}s added to this builder.
*/
public Function<? super HttpService, AuthService> newDecorator() {
final Authorizer<HttpRequest> authorizer = authorizer();
final AuthSuccessHandler<HttpRequest, HttpResponse> successHandler = this.successHandler;
final AuthFailureHandler<HttpRequest, HttpResponse> failureHandler = this.failureHandler;
return service -> new AuthService(service, authorizer, successHandler, failureHandler);
return delegate -> build(delegate, authorizer);
}

private Authorizer<HttpRequest> authorizer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,24 @@

import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.Request;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.RpcRequest;
import com.linecorp.armeria.common.RpcResponse;
import com.linecorp.armeria.server.Service;
import com.linecorp.armeria.server.HttpService;
import com.linecorp.armeria.server.ServiceRequestContext;

/**
* A callback which is invoked to handle an authorization success indicated by {@link Authorizer}.
*
* @param <I> the type of incoming {@link Request}. Must be {@link HttpRequest} or {@link RpcRequest}.
* @param <O> the type of outgoing {@link Response}. Must be {@link HttpResponse} or {@link RpcResponse}.
*
* @see AuthServiceBuilder#onSuccess(AuthSuccessHandler)
*/
@FunctionalInterface
public interface AuthSuccessHandler<I extends Request, O extends Response> {
public interface AuthSuccessHandler {

/**
* Invoked when the authorization of the specified {@link Request} has succeeded.
* Invoked when the authorization of the specified {@link HttpRequest} has succeeded.
*
* @param delegate the next {@link Service} in the decoration chain
* @param ctx the {@link ServiceRequestContext} of {@code req}
* @param req the {@link Request} being handled
* @param delegate the next {@link HttpService} in the decoration chain
* @param ctx the {@link ServiceRequestContext}
* @param req the {@link HttpRequest} being handled
*/
O authSucceeded(Service<I, O> delegate, ServiceRequestContext ctx, I req) throws Exception;
HttpResponse authSucceeded(
HttpService delegate, ServiceRequestContext ctx, HttpRequest req) throws Exception;
}

0 comments on commit 8e0cdb1

Please sign in to comment.