0

I am stuck with web socket authentication in quarkus with keycloak. I am using vert.x event bus and SockJSocket for the connection. For the rest end point i was able to do that authentication. But I want to use that authentication provider to authorize the web socket. (if want to see this is my full question - Quarkus Keycloak Custome Autherization)

I am try to identify the process happening in the REST end point. I found that there is a class called OidcIdentityProvider and I am try to use that for web socket authentication. I try to use its authenticate(......) method. But I can't figure out how to create that "AuthenticationRequestContext context" that want to pass int to the authenticate method ? Can some one help me to solve this. I am stuck with this since few days. Thank you very much.

  import io.vertx.mutiny.core.Vertx;
  import io.vertx.mutiny.ext.web.Router;
  import io.vertx.mutiny.ext.web.handler.sockjs.SockJSHandler;
  import io.vertx.mutiny.ext.web.handler.sockjs.SockJSSocket;
  import io.quarkus.oidc.runtime.OidcIdentityProvider;
    
  @ApplicationScoped
  public class EventBusBridgeConfig {
        
           
       private final Vertx vertx;
       private SockJSSocket sock;
       private final OidcIdentityProvider oidcIdentityProvider;


       private final String token = "eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI2dEdTZ0d5ekh2MWVlUGZMbFdWWUNYcDBqemp2OUZVSnVQdW5Bc3p1b3RrIn0.eyJleHAiOjE2ODYwMjY4MjgsImlhdCI6MTY4NjAyNTkyOCwiYXV0aF90aW1lIjoxNjg2MDI1OTI4LCJqdGkiOiIwNDA1ODUwNi1kMTllLTQ3MmEtYjk4NS00ZGIzZDU0ZTQ3YTgiLCJpc3MiOiJodHRwOi8vaWRwLmdvdmkubG9jYWwvcmVhbG1zL2RpbW8iLCJhdWQiOiJhY2NvdW50Iiwic3ViIjoiODM4YmZjOTUtZTM3My00NTg5LTgzMzItNDYzNDNhMWRiY2I0IiwidHlwIjoiQmVhcmVyIiwiYXpwIjoidGVsZW1ldHJ5LXNlcnZpY2UiLCJzZXNzaW9uX3N0YXRlIjoiOWY5MjUxZWMtMWQ3Ni00MDllLWFmYTYtMWQ0MzRjZTFkYWE1IiwiYWNyIjoiMSIsImFsbG93ZWQtb3JpZ2lucyI6WyJodHRwOi8vZGltby5hcHAuZ292aS5sb2NhbDo4MDgxIl0sInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyJkZWZhdWx0LXJvbGVzLWRpbW8iLCJvZmZsaW5lX2FjY2VzcyIsInVtYV9hdXRob3JpemF0aW9uIl19LCJyZXNvdXJjZV9hY2Nlc3MiOnsiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJwcm9maWxlIGVtYWlsIiwic2lkIjoiOWY5MjUxZWMtMWQ3Ni00MDllLWFmYTYtMWQ0MzRjZTFkYWE1IiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJtYWRodSIsImdpdmVuX25hbWUiOiIiLCJmYW1pbHlfbmFtZSI6IiJ9.qHdaJ9WWYu0u9GRP49CRGw_qhcYQHro6rG9uHP7YBzCoDH3rtcGHfhbtXLPqY-ywiXJC0DBaHCDq9lMOXJtFu_1THbkSqe4_g34u8F3yGni81FjdMuxfC1cSrnKsWUZGQCH_vwN-D8K1QIXPuX0IbahIEkKPcOeFXi1TYfmY01u0MgVuVgo0VWYGdMlQtvfzZr0hxWGp9wlPNHwHJ9PsqFCc0pBuECBcJoaPro3seT7S0xurUCy6VOrtJKRI5iFCuGkz98zKK7PGhZy4gaS8AIoihZ3KwdmbiE3mpYhp9N75J94TB7HMfTCLtXYopQx-0t1PkRqXFCaR7nrNBQYuvQ";
      private final String type = "bearer";
            
        
      private static final String SEC_WEBSOCKET_KEY = "sec-websocket-key";
        
      public EventBusBridgeConfig(Vertx vertxy) {
          this.vertx = vertx;
      }
        
      public void init(@Observes Router router) {
          SockJSBridgeOptions options = new SockJSBridgeOptions()
                        .addInboundPermitted(new PermittedOptions().setAddress("telemetry-subscribe"))
                        .addOutboundPermitted(new PermittedOptions().setAddressRegex("telemetry-feed-.*"));

           if (oidcIdentityProvider == null) {
              Log.info(">>>>>>>>>>>>>>>>: Oidc identity provider null");
           } else {
              Log.info(">>>>>>>>>>>>>>>>: Oidc identity provider NOT null");
           }

           TokenCredential securityCredential = new TokenCredential(token, type);
           TokenAuthenticationRequest tokenAuthenticationRequest = new TokenAuthenticationRequest(securityCredential);
        
                
           SockJSHandler handler = SockJSHandler.create(vertx);
        
           Log.info("mounting handler");
           router.mountSubRouter("/event-bus", handler.bridge(options, evt -> {
               sock = evt.socket();
               Log.info("Bearer token: " + sock.routingContext().queryParam("token"));
        
                  
        
            if (evt.type() == BridgeEventType.REGISTER) {
                  
                  Log.debug("headers: " + sock.headers());
        
                    
        
               } else if (evt.type() == BridgeEventType.SOCKET_CLOSED) {
                       
                   String connectionId = sock.headers().get(SEC_WEBSOCKET_KEY);
               }
        
               evt.complete(true);
           }));
            
   }
} 

this is the class I am try to use

enter image description here

  • Hi, have a look at the possible solution at https://github.com/SetoKaiba/reproduce/tree/master/src/main/java/net/kaiba, the user code should not be required to deal with the identity provider manually if `quarkus-oidc` bearer token verification is already configured. – Sergey Beryozkin Jun 11 '23 at 10:58

0 Answers0