title: Get user and validate JWT token in vert.x
body: <p>I have set up a keycloak server and I am developing an API which will be out on the internet. The API will receive calls by third parties (clients). These clients will first call the keycloak server with a clientId and a secret in order to get the token and then they will call my API with this token. </p>
  
  <p>I need to see how I can parse and validate this token. This token will probably be a JWT.
  So, in my test case, I have an http request having a json web token in the header. The token is provided by keycloak launched in localhost. I have copied the whole token:</p>
  
  <pre><code>{"Authorization":"Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJGSjg2R2NGM2pUYk5MT2NvNE52WmtVQ0lVbWZZQ3FvcXRPUWVNZmJoTmxFIn0.eyJqdGkiOiI2YWZlZjBiMC03ZmQ1LTRiOWUtOTk3NC0yOGFjMzBkMGM5OWQiLCJleHAiOjE0OTU2MTA0NTQsIm5iZiI6MCwiaWF0IjoxNDk1NjEwMTU0LCJpc3MiOiJodHRwOi8vMTI3LjAuMC4xOjgxMDAvYXV0aC9yZWFsbXMvZXhhbXBsZSIsImF1ZCI6ImpzLWNvbnNvbGUiLCJzdWIiOiJjNGY4NjE0Zi02YjFlLTRlYjItYmYxZC0wOTJmNGYxNWQwYmIiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJqcy1jb25zb2xlIiwiYXV0aF90aW1lIjowLCJzZXNzaW9uX3N0YXRlIjoiNTQ4NDJjNTgtMzYxYi00MDk2LThhNjgtNGZkZTg5OGUwNzg5IiwiYWNyIjoiMSIsImNsaWVudF9zZXNzaW9uIjoiNDNjMWEzMjAtNGZmNi00NmRmLThmZjUtNTU2ZjgxNGZhYzk1IiwiYWxsb3dlZC1vcmlnaW5zIjpbXSwicmVhbG1fYWNjZXNzIjp7InJvbGVzIjpbInVzZXIiXX0sInJlc291cmNlX2FjY2VzcyI6eyJhY2NvdW50Ijp7InJvbGVzIjpbIm1hbmFnZS1hY2NvdW50IiwibWFuYWdlLWFjY291bnQtbGlua3MiLCJ2aWV3LXByb2ZpbGUiXX19LCJuYW1lIjoiU2FtcGxlIFVzZXIiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ1c2VyIiwiZ2l2ZW5fbmFtZSI6IlNhbXBsZSIsImZhbWlseV9uYW1lIjoiVXNlciIsImVtYWlsIjoic2FtcGxlLXVzZXJAZXhhbXBsZSJ9.n1K0KIGYJYPZkjSq1eSg5gWnCAj44mgl4M-GJOlzgCj8y5TGw5OhT7sr84o9Ja9K6WMW3t0ti6feZIgc8mps3RK0HuuXeCNcrN6H2dPEtBphTvfXEUR2iMg83iCmxjhXgXso7oX2vyreJqB6WCCFEPbAQH2e5kHZqv6cfmXRlYU"}
  </code></pre>
  
  <p>I want to:</p>
  
  <ul>
  <li>Parse the token </li>
  <li>Get the key/value pairs I need </li>
  <li>Validate the token in the keycloak server. </li>
  </ul>
  
  <p>The goal is to protect the rest services and grant access to specific roles.
  My application is a 3.4.1 vert.x with routes. </p>
  
  <p>What I have now is an example I found that sets an JWTAuthHandler </p>
  
  <pre><code>    JWTAuth authProvider = JWTAuth.create(vertx, config().getJsonObject("keycloak.oidc"));
      router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));
  
      router.route("/protected/somepage").handler(ctx -&gt; {
          logger.info("Headers: {}", ctx.request().headers().get("Authorization"));
          logger.info(ctx.user().principal().encodePrettily());
      });
  </code></pre>
  
  <p>Keycloak configuration for the call from my API (used for JWTAuth) is:</p>
  
  <pre><code>   "keycloak.oidc": {
      "realm": "myrealm",
      "auth-server-url": "http://localhost:8100/auth",
      "ssl-required": "none",
      "resource": "app-client",
      "public-client": true
  }
  </code></pre>
  
  <p>When I make the rest call in postman, the jvm does not really manage to get in the handler and log the headers though, it throws this exception immediately
  saying 
  <code>io.vertx.ext.web.handler.impl.JWTAuthHandlerImpl
  AVERTISSEMENT: JWT decode failure java.lang.RuntimeException: Not enough or too many segments</code></p>