Skip to content

Commit

Permalink
Added tests for combinations and fixes.
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Lopes <[email protected]>
  • Loading branch information
pmlopes committed May 11, 2020
1 parent 717eef9 commit c238bf9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private boolean handlePreflight(RoutingContext ctx) {
return false;
}

private AuthenticationProvider getAuthProvider(RoutingContext ctx) {
protected AuthenticationProvider getAuthProvider(RoutingContext ctx) {
try {
AuthenticationProvider provider = ctx.get(AUTH_PROVIDER_CONTEXT_KEY);
if (provider != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private void iterate(final int idx, final RoutingContext ctx, JsonObject result,

// setup the desired auth provider if we can
if (authHandler instanceof AuthenticationHandlerImpl) {
ctx.put(AuthenticationHandlerImpl.AUTH_PROVIDER_CONTEXT_KEY, ((AuthenticationHandlerImpl) authHandler).authProvider);
ctx.put(AuthenticationHandlerImpl.AUTH_PROVIDER_CONTEXT_KEY, ((AuthenticationHandlerImpl) authHandler).getAuthProvider(ctx));
}

if (all) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.vertx.ext.web.handler;

import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.WebTestBase;
import io.vertx.ext.web.handler.impl.AuthenticationHandlerImpl;
import io.vertx.ext.web.handler.impl.HttpStatusException;
import io.vertx.ext.web.sstore.LocalSessionStore;
import org.junit.Test;

public class ChainAuthMixHandlerTest extends WebTestBase {

private AuthenticationHandler success = new AuthenticationHandlerImpl((authInfo, resultHandler) -> resultHandler.handle(Future.succeededFuture(User.create(new JsonObject())))) {
@Override
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
handler.handle(Future.succeededFuture(new JsonObject()));
}
};

private AuthenticationHandler failure = new AuthenticationHandlerImpl((authInfo, resultHandler) -> resultHandler.handle(Future.failedFuture("Oops!"))) {
@Override
public void parseCredentials(RoutingContext context, Handler<AsyncResult<JsonObject>> handler) {
handler.handle(Future.failedFuture(new HttpStatusException(401)));
}
};

@Test
public void testFailOrFailOrSuccess() throws Exception {

// (failure OR (failure OR success))
ChainAuthHandler chain =
ChainAuthHandler.any()
.add(failure)
.add(
ChainAuthHandler.any()
.add(failure)
.add(success)
);


router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
router.route().handler(chain);
router.route().handler(ctx -> ctx.response().end());

testRequest(HttpMethod.GET, "/", 200, "OK");
}

@Test
public void testFailOrSuccessAndFail() throws Exception {

// (failure OR (sucess AND failure))
ChainAuthHandler chain =
ChainAuthHandler.any()
.add(failure)
.add(
ChainAuthHandler.all()
.add(success)
.add(failure)
);


router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));
router.route().handler(chain);
router.route().handler(ctx -> ctx.response().end());

testRequest(HttpMethod.GET, "/", 401, "Unauthorized");
}
}

0 comments on commit c238bf9

Please sign in to comment.