Java Code Examples for io.vertx.ext.auth.KeyStoreOptions

The following examples show how to use io.vertx.ext.auth.KeyStoreOptions. These examples are extracted from open source projects. 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 Project: vertx-web   Source File: WebExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void example50(Vertx vertx) {

    Router router = Router.router(vertx);

    JWTAuthOptions authConfig = new JWTAuthOptions()
      .setKeyStore(new KeyStoreOptions()
        .setType("jceks")
        .setPath("keystore.jceks")
        .setPassword("secret"));

    JWTAuth jwt = JWTAuth.create(vertx, authConfig);

    router.route("/login").handler(ctx -> {
      // this is an example, authentication should be done with another provider...
      if (
        "paulo".equals(ctx.request().getParam("username")) &&
          "secret".equals(ctx.request().getParam("password"))) {
        ctx.response()
          .end(jwt.generateToken(new JsonObject().put("sub", "paulo")));
      } else {
        ctx.fail(401);
      }
    });
  }
 
Example 2
Source Project: vertx-web   Source File: WebExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void example51(Vertx vertx) {

    Router router = Router.router(vertx);

    JWTAuthOptions authConfig = new JWTAuthOptions()
      .setKeyStore(new KeyStoreOptions()
        .setType("jceks")
        .setPath("keystore.jceks")
        .setPassword("secret"));

    JWTAuth authProvider = JWTAuth.create(vertx, authConfig);

    router.route("/protected/*").handler(JWTAuthHandler.create(authProvider));

    router.route("/protected/somepage").handler(ctx -> {
      // some handle code...
    });
  }
 
Example 3
Source Project: vertx-web   Source File: WebExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void example52(Vertx vertx) {

    JWTAuthOptions authConfig = new JWTAuthOptions()
      .setKeyStore(new KeyStoreOptions()
        .setType("jceks")
        .setPath("keystore.jceks")
        .setPassword("secret"));

    JWTAuth authProvider = JWTAuth.create(vertx, authConfig);

    authProvider
      .generateToken(
        new JsonObject()
          .put("sub", "paulo")
          .put("someKey", "some value"),
        new JWTOptions());
  }
 
Example 4
Source Project: vertx-auth   Source File: AuthJWTExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void example7(Vertx vertx, String username, String password) {

    JWTAuthOptions config = new JWTAuthOptions()
      .setKeyStore(new KeyStoreOptions()
        .setPath("keystore.jceks")
        .setPassword("secret"));

    JWTAuth provider = JWTAuth.create(vertx, config);

    // on the verify endpoint once you verify the identity
    // of the user by its username/password
    if ("paulo".equals(username) && "super_secret".equals(password)) {
      String token = provider.generateToken(
        new JsonObject().put("sub", "paulo"), new JWTOptions());

      // now for any request to protected resources you should
      // pass this string in the HTTP header Authorization as:
      // Authorization: Bearer <token>
    }
  }
 
Example 5
Source Project: vertx-auth   Source File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateNewTokenES256() {
  authProvider = JWTAuth.create(vertx, new JWTAuthOptions()
    .setKeyStore(new KeyStoreOptions()
      .setPath("es256-keystore.jceks")
      .setType("jceks")
      .setPassword("secret")));

  String token = authProvider.generateToken(new JsonObject().put("sub", "paulo"), new JWTOptions().setAlgorithm("ES256"));
  assertNotNull(token);

  TokenCredentials authInfo = new TokenCredentials(token);

  authProvider.authenticate(authInfo, res -> {
    if (res.failed()) {
      res.cause().printStackTrace();
      fail();
    }

    assertNotNull(res.result());
    testComplete();
  });
  await();
}
 
Example 6
Source Project: vertx-auth   Source File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateNewTokenForceAlgorithm() {
  authProvider = JWTAuth.create(vertx, new JWTAuthOptions()
    .setKeyStore(new KeyStoreOptions()
      .setPath("keystore.jceks")
      .setType("jceks")
      .setPassword("secret")));

  String token = authProvider.generateToken(new JsonObject(), new JWTOptions().setAlgorithm("RS256"));
  assertNotNull(token);

  // reverse
  TokenCredentials authInfo = new TokenCredentials(token);
  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 7
private JWTAuthOptions getJWTConfig() {
  return new JWTAuthOptions()
    .setKeyStore(new KeyStoreOptions()
      .setPath("keystore.jceks")
      .setType("jceks")
      .setPassword("secret"));
}
 
Example 8
Source Project: vertx-auth   Source File: AuthJWTExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void example6(Vertx vertx) {

    JWTAuthOptions config = new JWTAuthOptions()
      .setKeyStore(new KeyStoreOptions()
        .setPath("keystore.jceks")
        .setPassword("secret"));

    AuthenticationProvider provider = JWTAuth.create(vertx, config);
  }
 
Example 9
Source Project: vertx-auth   Source File: JWTAuthProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
private JWTAuthOptions getConfig() {
  return new JWTAuthOptions()
    .setKeyStore(new KeyStoreOptions()
      .setPath("keystore.jceks")
      .setType("jceks")
      .setPassword("secret"));
}
 
Example 10
Source Project: vertx-auth   Source File: JWTAuthOptions.java    License: Apache License 2.0 4 votes vote down vote up
public KeyStoreOptions getKeyStore() {
  return keyStore;
}
 
Example 11
Source Project: vertx-auth   Source File: JWTAuthOptions.java    License: Apache License 2.0 4 votes vote down vote up
public JWTAuthOptions setKeyStore(KeyStoreOptions keyStore) {
  this.keyStore = keyStore;
  return this;
}
 
Example 12
Source Project: vertx-auth   Source File: JWTAuthProviderImpl.java    License: Apache License 2.0 4 votes vote down vote up
public JWTAuthProviderImpl(Vertx vertx, JWTAuthOptions config) {
  this.permissionsClaimKey = config.getPermissionsClaimKey();
  this.jwtOptions = config.getJWTOptions();

  final KeyStoreOptions keyStore = config.getKeyStore();

  // attempt to load a Key file
  try {
    if (keyStore != null) {
      KeyStore ks = KeyStore.getInstance(keyStore.getType());

      // synchronize on the class to avoid the case where multiple file accesses will overlap
      synchronized (JWTAuthProviderImpl.class) {
        final Buffer keystore = vertx.fileSystem().readFileBlocking(keyStore.getPath());

        try (InputStream in = new ByteArrayInputStream(keystore.getBytes())) {
          ks.load(in, keyStore.getPassword().toCharArray());
        }
      }
      // load all available keys in the keystore
      for (JWK key : JWK.load(ks, keyStore.getPassword(), keyStore.getPasswordProtection())) {
        jwt.addJWK(key);
      }
    }
    // attempt to load pem keys
    final List<PubSecKeyOptions> keys = config.getPubSecKeys();

    if (keys != null) {
      for (PubSecKeyOptions pubSecKey : config.getPubSecKeys()) {
        jwt.addJWK(new JWK(pubSecKey));
      }
    }

    // attempt to load jwks
    final List<JsonObject> jwks = config.getJwks();

    if (jwks != null) {
      for (JsonObject jwk : jwks) {
        this.jwt.addJWK(new JWK(jwk));
      }
    }

  } catch (KeyStoreException | IOException | FileSystemException | CertificateException | NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
  }
}