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

Web ReverseProxy fails after authentication with OAuth2 callback

Open skoya opened this issue 1 year ago • 0 comments

Questions

Version

4.5.10

Context

I encountered an exception which looks suspicious while .

I setup OAuth2 and then tried to proxy. Everything looks okay but then the proxied request failed after a time out. Commenting out all the OAuth2 code and it works fine as a reverse proxy.

Do you have a reproducer?

v = Vertx.vertx();

        HttpClient proxyClient = v.createHttpClient();
        HttpProxy httpProxy = HttpProxy.reverseProxy(proxyClient);
        Router proxyRouter = Router.router(v);
        
        HttpServer proxyServer = v.createHttpServer();

        OAuth2Auth authProvider = AzureADAuth.create(v, "CLIENT_ID", "CLIENT_SECRET", "TENANT");
        OAuth2AuthHandler oauth2 = OAuth2AuthHandler.create(v, authProvider, "http://localhost:8201/callback/");

        oauth2.setupCallback(proxyRouter.route("/callback/"));

        proxyRouter.route().handler(ctx -> {
            System.out.println("Global handler: Request path = " + ctx.request().path());
            ctx.next();  // Pass request to the next handler in the chain
        });

        proxyRouter.route("/*").handler(oauth2);

        proxyRouter.route("/*").handler(ctx-> {
            if (ctx.user() == null) {
                System.out.println("OAuth2 Auth handler triggered for path: " + ctx.request().path());
                oauth2.handle(ctx);
            } else {
                System.out.println("User authenticated: " + ctx.user().principal());
                ctx.next();
            }
        });
        proxyRouter.route(HttpMethod.GET, "/*").handler(ProxyHandler.create(httpProxy, 8200, "localhost"));

        proxyServer.requestHandler(proxyRouter).listen(8201);   

Steps to reproduce

  1. Create an app registration in Azure AD
  2. Create any webserver as an Origin for the reverse proxy to hit on port 8200
  3. Spin up the above code and http http://localhost:8201 (proxy server address).
  4. Login with Azure Oauth 2 creds
  5. Callback is called, but then the proxy handler timesout.
  6. Comment out all the OAuth2 code and the reverse proxy works fine!

Extra

I am using Windows 11 H2. Not sure if there is any weirdness with DNS.

skoya avatar Oct 21 '24 21:10 skoya