io.vertx.ext.web.handler.JWTAuthHandler Java Examples

The following examples show how to use io.vertx.ext.web.handler.JWTAuthHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XYZHubRESTVerticle.java    From xyz-hub with Apache License 2.0 6 votes vote down vote up
/**
 * Add the security handlers.
 */
private AuthHandler createJWTHandler() {
  JWTAuthOptions authConfig = new JWTAuthOptions().addPubSecKey(
      new PubSecKeyOptions().setAlgorithm("RS256")
          .setPublicKey(Service.configuration.JWT_PUB_KEY));

  JWTAuth authProvider = new XyzAuthProvider(vertx, authConfig);

  ChainAuthHandler authHandler = ChainAuthHandler.create()
      .append(JWTAuthHandler.create(authProvider))
      .append(JWTURIHandler.create(authProvider));

  if (Service.configuration.XYZ_HUB_AUTH == AuthorizationType.DUMMY) {
    authHandler.append(JwtDummyHandler.create(authProvider));
  }

  return authHandler;
}
 
Example #2
Source File: AuthenticationFactory.java    From nubes with Apache License 2.0 5 votes vote down vote up
public AuthenticationFactory(Config config) {
  this.config = config;
  authHandlers = new EnumMap<>(AuthMethod.class);
  authHandlers.put(BASIC, BasicAuthHandler::create);
  authHandlers.put(JWT, auth -> JWTAuthHandler.create((JWTAuth)config.getAuthProvider()));
  authHandlers.put(API_TOKEN, CheckTokenHandler::new);
}
 
Example #3
Source File: JWTURIHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
public static JWTAuthHandler create(JWTAuth authProvider) {
  return new JWTURIHandler(authProvider);
}
 
Example #4
Source File: JWTURIHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public JWTAuthHandler setAudience(List<String> audience) {
  options.put("audience", new JsonArray(audience));
  return this;
}
 
Example #5
Source File: JWTURIHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public JWTAuthHandler setIssuer(String issuer) {
  options.put("issuer", issuer);
  return this;
}
 
Example #6
Source File: JWTURIHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public JWTAuthHandler setIgnoreExpiration(boolean ignoreExpiration) {
  options.put("ignoreExpiration", ignoreExpiration);
  return this;
}
 
Example #7
Source File: JwtDummyHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
public static JWTAuthHandler create(JWTAuth authProvider) {
  return new JwtDummyHandler(authProvider);
}
 
Example #8
Source File: JwtDummyHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public JWTAuthHandler setAudience(List<String> audience) {
  options.put("audience", new JsonArray(audience));
  return this;
}
 
Example #9
Source File: JwtDummyHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public JWTAuthHandler setIssuer(String issuer) {
  options.put("issuer", issuer);
  return this;
}
 
Example #10
Source File: JwtDummyHandler.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
@Override
public JWTAuthHandler setIgnoreExpiration(boolean ignoreExpiration) {
  options.put("ignoreExpiration", ignoreExpiration);
  return this;
}
 
Example #11
Source File: SubRouterFactoryImpl.java    From AlipayWechatPlatform with GNU General Public License v3.0 4 votes vote down vote up
private SubRouterFactoryImpl(JWTAuth jwtProvider){
    this.jwtProvider = jwtProvider;
    this.jwtHandler = JWTAuthHandler.create(jwtProvider);
}
 
Example #12
Source File: OpenAPI3Examples.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public void addJWT(RouterFactory routerFactory, JWTAuth jwtAuthProvider) {
  routerFactory.securityHandler("jwt_auth", JWTAuthHandler.create(jwtAuthProvider));
}
 
Example #13
Source File: OpenAPI3Examples.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public void mainExample(Vertx vertx, JWTAuth jwtAuth) {
  // Load the api spec. This operation is asynchronous
  RouterFactory.create(vertx, "src/main/resources/petstore.yaml",
    routerFactoryAsyncResult -> {
    if (routerFactoryAsyncResult.succeeded()) {
      // Spec loaded with success, retrieve the router
      RouterFactory routerFactory = routerFactoryAsyncResult.result();
      // You can enable or disable different features of router factory using RouterFactoryOptions
      RouterFactoryOptions options = new RouterFactoryOptions();
      // Set the options
      routerFactory.setOptions(options);
      // Add an handler to operation listPets
      routerFactory.operation("listPets").handler(routingContext -> {
        // Handle listPets operation
        routingContext.response().setStatusMessage("Called listPets").end();
      }).handler(routingContext -> { // Add a failure handler to the same operation
        // This is the failure handler
        Throwable failure = routingContext.failure();
        if (failure instanceof BadRequestException)
          // Handle Validation Exception
          routingContext
            .response()
            .setStatusCode(400)
            .putHeader("content-type", "application/json")
            .end(((BadRequestException)failure).toJson().toBuffer());
      });

      // Add a security handler
      // Handle security here
      routerFactory.securityHandler(
        "api_key",
        JWTAuthHandler.create(jwtAuth)
      );

      // Now you have to generate the router
      Router router = routerFactory.createRouter();

      // Now you can use your Router instance
      HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
      server.requestHandler(router).listen();

    } else {
      // Something went wrong during router factory initialization
      Throwable exception = routerFactoryAsyncResult.cause();
    }
  });
}
 
Example #14
Source File: OpenAPI3Examples.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public void addJWT(OpenAPI3RouterFactory routerFactory, JWTAuth jwtAuthProvider) {
  routerFactory.addSecurityHandler("jwt_auth", JWTAuthHandler.create(jwtAuthProvider));
}