Java Code Examples for io.vertx.ext.auth.authentication.AuthenticationProvider#authenticate()

The following examples show how to use io.vertx.ext.auth.authentication.AuthenticationProvider#authenticate() . 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: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticate(TestContext should) {
  final Async test = should.async();

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopus").put("password", "secret");

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.succeeded());
    should.assertNotNull(authenticate.result());
    should.assertEquals("lopus", authenticate.result().principal().getString("username"));
    test.complete();
  });
}
 
Example 2
Source File: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticateBadPassword(TestContext should) {
  final Async test = should.async();

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopus").put("password", "s3cr3t");

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.failed());
    should.assertNull(authenticate.result());
    should.assertEquals("Invalid username/password", authenticate.cause().getMessage());
    test.complete();
  });
}
 
Example 3
Source File: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthenticateBadUser(TestContext should) {
  final Async test = should.async();

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopes").put("password", "s3cr3t");

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.failed());
    should.assertNull(authenticate.result());
    should.assertEquals("Invalid username/password", authenticate.cause().getMessage());
    test.complete();
  });
}
 
Example 4
Source File: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthoriseHasRole(TestContext should) {
  final Async test = should.async();

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopus").put("password", "secret");

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.succeeded());
    final User user = authenticate.result();
    should.assertNotNull(user);
    AuthorizationProvider authz = SqlAuthorization.create(mysql);
    authz.getAuthorizations(user, getAuthorizations -> {
      should.assertTrue(getAuthorizations.succeeded());
      // attest
      should.assertTrue(RoleBasedAuthorization.create("dev").match(user));
      test.complete();
    });
  });
}
 
Example 5
Source File: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthoriseNotHasRole(TestContext should) {
  final Async test = should.async();

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopus").put("password", "secret");

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.succeeded());
    final User user = authenticate.result();
    should.assertNotNull(user);
    AuthorizationProvider authz = SqlAuthorization.create(mysql);
    authz.getAuthorizations(user, getAuthorizations -> {
      should.assertTrue(getAuthorizations.succeeded());
      // attest
      should.assertFalse(RoleBasedAuthorization.create("manager").match(user));
      test.complete();
    });
  });
}
 
Example 6
Source File: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthoriseHasPermission(TestContext should) {
  final Async test = should.async();

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopus").put("password", "secret");

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.succeeded());
    final User user = authenticate.result();
    should.assertNotNull(user);
    AuthorizationProvider authz = SqlAuthorization.create(mysql);
    authz.getAuthorizations(user, getAuthorizations -> {
      should.assertTrue(getAuthorizations.succeeded());
      // attest
      should.assertTrue(PermissionBasedAuthorization.create("commit_code").match(user));
      test.complete();
    });
  });
}
 
Example 7
Source File: MySQLTest.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
@Test
public void testAuthoriseNotHasPermission(TestContext should) {
  final Async test = should.async();

  JsonObject authInfo = new JsonObject();
  authInfo.put("username", "lopus").put("password", "secret");

  AuthenticationProvider authn = SqlAuthentication.create(mysql);

  authn.authenticate(authInfo, authenticate -> {
    should.assertTrue(authenticate.succeeded());
    final User user = authenticate.result();
    should.assertNotNull(user);
    AuthorizationProvider authz = SqlAuthorization.create(mysql);
    authz.getAuthorizations(user, getAuthorizations -> {
      should.assertTrue(getAuthorizations.succeeded());
      // attest
      should.assertFalse(PermissionBasedAuthorization.create("eat_sandwich").match(user));
      test.complete();
    });
  });
}
 
Example 8
Source File: AuthCommonExamples.java    From vertx-auth with Apache License 2.0 6 votes vote down vote up
public void example1(AuthenticationProvider authProvider) {

    JsonObject authInfo = new JsonObject()
      .put("username", "tim").put("password", "mypassword");

    authProvider.authenticate(authInfo, res -> {
      if (res.succeeded()) {

        User user = res.result();

        System.out.println("User " + user.principal() + " is now authenticated");

      } else {
        res.cause().printStackTrace();
      }
    });
  }