Java Code Examples for io.vertx.ext.auth.jwt.JWTAuth#create()

The following examples show how to use io.vertx.ext.auth.jwt.JWTAuth#create() . 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-auth   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 2
Source Project: vertx-web   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 3
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGoodScopes() {
  //JWT is valid because required scopes "a" & "b" are well included in the access_token.
  authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(
    new JWTOptions()
      .addScope("a")
      .addScope("b")));

  JsonObject payload = new JsonObject()
    .put("sub", "Paulo");

  final String token = authProvider.generateToken(payload,
    new JWTOptions().addScope("a").addScope("b").addScope("c"));

  assertNotNull(token);

  TokenCredentials authInfo = new TokenCredentials(token);

  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 4
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGoodScopesWithDelimiter() {
  //JWT is valid because required scopes "a" & "b" are well included in the access_token.
  authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(
    new JWTOptions()
      .addScope("a")
      .addScope("b")
      .withScopeDelimiter(",")));

  JsonObject payload = new JsonObject()
    .put("sub", "Paulo");

  final String token = authProvider.generateToken(payload,
    new JWTOptions().addScope("a").addScope("b").addScope("c").withScopeDelimiter(","));

  assertNotNull(token);

  TokenCredentials authInfo = new TokenCredentials(token);

  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 5
Source Project: vertx-auth   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 6
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateNewTokenWithMacSecret() {
  authProvider = JWTAuth.create(vertx, new JWTAuthOptions()
    .addJwk(new JsonObject()
      .put("kty", "oct")
      .put("k", "notasecret"))
  );

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

  // reverse
  TokenCredentials authInfo = new TokenCredentials(token);
  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 7
Source Project: vertx-auth   File: AuthJWTExamples.java    License: Apache License 2.0 6 votes vote down vote up
public void example17(Vertx vertx) {
  JWTAuth provider = JWTAuth.create(vertx, new JWTAuthOptions()
    .addPubSecKey(new PubSecKeyOptions()
      .setAlgorithm("ES256")
      .setBuffer(
        "-----BEGIN PRIVATE KEY-----\n" +
          "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgeRyEfU1NSHPTCuC9\n" +
          "rwLZMukaWCH2Fk6q5w+XBYrKtLihRANCAAStpUnwKmSvBM9EI+W5QN3ALpvz6bh0\n" +
          "SPCXyz5KfQZQuSj4f3l+xNERDUDaygIUdLjBXf/bc15ur2iZjcq4r0Mr\n" +
          "-----END PRIVATE KEY-----\n")
    ));

  String token = provider.generateToken(
    new JsonObject(),
    new JWTOptions().setAlgorithm("ES256"));
}
 
Example 8
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGoodScopesWithDefaultDelimiter() {
  //JWT is valid because required scopes "a" & "b" are well included in the access_token.
  authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(
    new JWTOptions()
      .addScope("a")
      .addScope("b")));

  JsonObject payload = new JsonObject()
    .put("sub", "Paulo");

  final String token = authProvider.generateToken(payload,
    new JWTOptions().addScope("a").addScope("b").addScope("c").withScopeDelimiter(" "));

  assertNotNull(token);

  TokenCredentials authInfo = new TokenCredentials(token);

  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 9
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLeeway3() {
  authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(new JWTOptions().setLeeway(5)));

  long now = System.currentTimeMillis() / 1000;

  JsonObject payload = new JsonObject()
    .put("sub", "Paulo")
    .put("exp", now)
    .put("iat", now);

  String token = authProvider.generateToken(payload);
  assertNotNull(token);

  TokenCredentials authInfo = new TokenCredentials(token);
  // fail because exp is <= to now
  authProvider.authenticate(authInfo, onSuccess(t -> testComplete()));
  await();
}
 
Example 10
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGoodAudience() {

  authProvider = JWTAuth.create(vertx, getConfig().setJWTOptions(
    new JWTOptions()
      .addAudience("b")
      .addAudience("d")));

  JsonObject payload = new JsonObject()
    .put("sub", "Paulo");

  final String token = authProvider.generateToken(payload,
    new JWTOptions().addAudience("a").addAudience("b").addAudience("c"));

  assertNotNull(token);

  TokenCredentials authInfo = new TokenCredentials(token);

  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 11
Source Project: xyz-hub   File: JwtGenerator.java    License: Apache License 2.0 5 votes vote down vote up
private static void setup() throws IOException {
  JWTAuthOptions authConfig = new JWTAuthOptions()
      .setJWTOptions(jwtOptions)
      .addPubSecKey(new PubSecKeyOptions()
          .setAlgorithm("RS256")
          .setPublicKey(readResourceFile("/auth/jwt.pub"))
          .setSecretKey(readResourceFile("/auth/jwt.key")));

  authProvider = JWTAuth.create(Service.vertx, authConfig);
}
 
Example 12
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testJWKShouldNotCrash() {

  authProvider = JWTAuth.create(vertx, new JWTAuthOptions().addJwk(
    new JsonObject()
    .put("kty", "RSA")
    .put("n", "0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKRXjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHzu6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKgw")
    .put("e", "AQAB")
    .put("alg", "RS256")
    .put("kid", "2011-04-29")));

}
 
Example 13
Source Project: nubes   File: Config.java    License: Apache License 2.0 5 votes vote down vote up
private void createAuthHandlers() {
  String auth = json.getString("auth-type");
  JsonObject authProperties = json.getJsonObject("auth-properties");

  // TODO : discuss it. I'm really not convinced about all the boilerplate needed in config (dbName only for JDBC, etc.)
  if (authProperties != null) {
    // For now, only JWT,Shiro and JDBC supported (same as for Vert.x web)
    switch (auth) {
      case "JWT":// For now only allow properties realm
        this.authProvider = JWTAuth.create(vertx, authProperties);
        break;
      case "Shiro":
        ShiroAuth.create(vertx, new ShiroAuthOptions(authProperties));
        break;
      case "JDBC":
        String dbName = json.getString("db-name");
        Objects.requireNonNull(dbName);
        JDBCClient client = JDBCClient.createShared(vertx, authProperties, dbName);
        this.authProvider = JDBCAuth.create(vertx, client);
        break;
      default:
        LOG.warn("Unknown type of auth : " + auth + " . Ignoring.");
    }
  } else if (auth != null) {
    LOG.warn("You have defined " + auth + " as auth type, but didn't provide any configuration, can't create authProvider");
  }

}
 
Example 14
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateTokenWithValidMacSecret() {
  String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1MDE3ODUyMDZ9.08K_rROcCmKTF1cKfPCli2GQFYIOP8dePxeS1SE4dc8";
  authProvider = JWTAuth.create(vertx, new JWTAuthOptions()
    .addJwk(new JsonObject()
      .put("kty", "oct")
      .put("k", "notasecret"))
  );
  TokenCredentials authInfo = new TokenCredentials(token);
  authProvider.authenticate(authInfo, onSuccess(res -> {
    assertNotNull(res);
    testComplete();
  }));
  await();
}
 
Example 15
Source Project: vertx-web   File: JWTAuthHandlerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Before
public void setup() throws Exception {
  JsonObject authConfig = new JsonObject().put("keyStore", new JsonObject()
      .put("type", "jceks")
      .put("path", "keystore.jceks")
      .put("password", "secret"));

  authProvider = JWTAuth.create(vertx, new JWTAuthOptions(authConfig));
}
 
Example 16
Source Project: vertx-auth   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 17
Source Project: vertx-auth   File: AuthJWTExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void example8(Vertx vertx) {

    JWTAuthOptions config = new JWTAuthOptions()
      .addPubSecKey(new PubSecKeyOptions()
        .setAlgorithm("RS256")
        .setBuffer("BASE64-ENCODED-PUBLIC_KEY"));

    AuthenticationProvider provider = JWTAuth.create(vertx, config);
  }
 
Example 18
Source Project: vertx-auth   File: AuthJWTExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void example14(Vertx vertx) {

    JsonObject config = new JsonObject()
      .put("public-key", "BASE64-ENCODED-PUBLIC_KEY")
      // since we're consuming keycloak JWTs we need
      // to locate the permission claims in the token
      .put("permissionsClaimKey", "realm_access/roles");

    AuthenticationProvider provider =
      JWTAuth.create(vertx, new JWTAuthOptions(config));
  }
 
Example 19
Source Project: vertx-auth   File: AuthJWTExamples.java    License: Apache License 2.0 5 votes vote down vote up
public void example16(Vertx vertx) {
  JWTAuth provider = JWTAuth.create(vertx, new JWTAuthOptions()
    .addPubSecKey(new PubSecKeyOptions()
      .setAlgorithm("HS256")
      .setBuffer("keyboard cat")));

  String token = provider.generateToken(new JsonObject());
}
 
Example 20
Source Project: vertx-auth   File: JWTAuthProviderTest.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  authProvider = JWTAuth.create(vertx, getConfig());
}