vertx-web icon indicating copy to clipboard operation
vertx-web copied to clipboard

Cannot use per-operation AuthorizationHandlers with vertx-web-openapi

Open ikstewa opened this issue 2 years ago • 5 comments

Version

4.3.0

Context

After the introduction of RFC: Web Handlers Setup Mistake Free in vertx 4.3.0, we can no longer register AuthorizationHandler per-operation when using vertx-web-openapi.

Before vertx 4.3.0 we would use the RouterBuilder similar to:

routerBuilder
  .operation("awesomeOperation")
  .handler(// AuthZ handler)
  .handler(// User handler);

With the upgrade to 4.3.0 it now fails with: java.lang.IllegalStateException: Cannot add [AUTHORIZATION] handler to route with [USER] handler at index 2

The referenced USER handler is the ValidationHandler auto-generated by the open api spec.

Do you have a reproducer?

Not currently

ikstewa avatar May 27 '22 17:05 ikstewa

Any update or recommended work-around?

This is currently blocking us from upgrading to vertx 4.3

ikstewa avatar Jun 13 '22 22:06 ikstewa

I am also running into this. It's because the OpenAPI3 router builder adds ValidationHandlerImpl which is a USER handler.

I think vertx-web needs to implement some sort of runtime sorting of handlers if we are to continue using things like the OpenAPI libs which insert multiple handlers when createRouter() is called, which has given the client programmer the chance to install a whole host of their own.

chris-brace avatar Aug 05 '22 21:08 chris-brace

Also, for context, im in a similar situation: My API needs to have some endpoints that do not have authentication AND it needs extra authorization handlers on another subset.

chris-brace avatar Aug 05 '22 21:08 chris-brace

@ikstewa fyi you can turn this check off by doing

        System.setProperty("io.vertx.web.router.setup.lenient", "true");

before building the router.

chris-brace avatar Aug 09 '22 22:08 chris-brace

We're able to upgrade and get past the check however the check is correct. We're running our AuthZ checks after the body validations. So for example if I have an authentic token which does not have authorization for this particular route, the request body validation is going to be ran before the AuthZ check.

The core issue is: How are you supposed to register AuthZ handlers per-route, such that they are ran before the validation handlers?

ikstewa avatar Sep 30 '22 19:09 ikstewa

Related: https://github.com/vert-x3/vertx-web/issues/1895

ikstewa avatar Oct 18 '22 21:10 ikstewa

After this pull request it's possible to register AuthZ handlers using a new method on the operation. The original example is supported as:

routerBuilder
  .operation("awesomeOperation")
  .authorizationHandler(// AuthZ handler)
  .handler(// User handler);

@vietj @pmlopes Does this seem like a reasonable approach to support?

ikstewa avatar Oct 18 '22 22:10 ikstewa

@chris-brace Would the above proposal fix your use case?

ikstewa avatar Oct 18 '22 23:10 ikstewa

I think this would help me because our issue is only that we need to attach authorization handlers.

chris-brace avatar Oct 19 '22 19:10 chris-brace

I'd also like to add operation specific authorization handlers. The lenient workaround works, although it prints warnings for all routes with authorization handlers.

If I understand the code in OpenAPI3RouterBuilderImpl correctly there is currently no way to use AuthorizationHandlers with the openapi module. https://github.com/vert-x3/vertx-web/blob/58678777c7607289a0b6b8cb87a514155017217f/vertx-web-openapi/src/main/java/io/vertx/ext/web/openapi/impl/OpenAPI3RouterBuilderImpl.java#L278-L296

Is there any other way to register authorization handlers wit openapi that I am missing? If so: which one?

The solution of @ikstewa looks great. Are there any reason besides no time/resources for review that prohibit a merge?

lukasjelonek avatar Mar 08 '23 14:03 lukasjelonek

The new OpenAPI module which supports both OpenAPI 3.0 and 3.1 does solve this issue. There is no need for being lenient or adding more methods to the API that would perform the same result.

A regression test is added to ensure this case passes

pmlopes avatar Mar 29 '23 14:03 pmlopes