io.vertx.ext.auth.KeyStoreOptions Java Examples

The following examples show how to use io.vertx.ext.auth.KeyStoreOptions. 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: WebExamples.java    From vertx-web with 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 File: WebExamples.java    From vertx-web with 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 File: WebExamples.java    From vertx-web with 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 File: AuthJWTExamples.java    From vertx-auth with 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 File: JWTAuthProviderTest.java    From vertx-auth with 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 File: JWTAuthProviderTest.java    From vertx-auth with 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
Source File: SecureServiceBinderTest.java    From vertx-service-proxy with Apache License 2.0 5 votes vote down vote up
private JWTAuthOptions getJWTConfig() {
  return new JWTAuthOptions()
    .setKeyStore(new KeyStoreOptions()
      .setPath("keystore.jceks")
      .setType("jceks")
      .setPassword("secret"));
}
 
Example #8
Source File: AuthJWTExamples.java    From vertx-auth with 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 File: JWTAuthProviderTest.java    From vertx-auth with 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 File: JWTAuthOptions.java    From vertx-auth with Apache License 2.0 4 votes vote down vote up
public KeyStoreOptions getKeyStore() {
  return keyStore;
}
 
Example #11
Source File: JWTAuthOptions.java    From vertx-auth with Apache License 2.0 4 votes vote down vote up
public JWTAuthOptions setKeyStore(KeyStoreOptions keyStore) {
  this.keyStore = keyStore;
  return this;
}
 
Example #12
Source File: JWTAuthProviderImpl.java    From vertx-auth with 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);
  }
}