Search in sources :

Example 36 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testDeleteTarget.

@Test
public void testDeleteTarget() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id1 = authManager.createTarget(makeTarget("target1", "url1"));
    Id id2 = authManager.createTarget(makeTarget("target2", "url2"));
    Assert.assertEquals(2, authManager.listAllTargets(-1).size());
    HugeTarget target = authManager.deleteTarget(id1);
    Assert.assertEquals("target1", target.name());
    Assert.assertEquals(1, authManager.listAllTargets(-1).size());
    target = authManager.deleteTarget(id2);
    Assert.assertEquals("target2", target.name());
    Assert.assertEquals(0, authManager.listAllTargets(-1).size());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id user = authManager.createUser(makeUser("tom", "pass1"));
        authManager.deleteTarget(user);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) Test(org.junit.Test)

Example 37 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testValidateUserByToken.

@Test
public void testValidateUserByToken() throws AuthenticationException {
    AuthManager authManager = graph().authManager();
    HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
    Id userId = authManager.createUser(user);
    String token = authManager.loginUser("test", "pass");
    UserWithRole userWithRole;
    userWithRole = authManager.validateUser(token);
    Assert.assertEquals(userId, userWithRole.userId());
    Assert.assertEquals("test", userWithRole.username());
    Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
    // Token cache missed
    Cache<Id, String> tokenCache = Whitebox.getInternalState(authManager, "tokenCache");
    tokenCache.invalidate(IdGenerator.of(token));
    Assert.assertFalse(tokenCache.containsKey(IdGenerator.of(token)));
    userWithRole = authManager.validateUser(token);
    Assert.assertEquals(userId, userWithRole.userId());
    Assert.assertEquals("test", userWithRole.username());
    Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
    Assert.assertTrue(tokenCache.containsKey(IdGenerator.of(token)));
    // User deleted after login and token not expire
    authManager.deleteUser(userId);
    userWithRole = authManager.validateUser(token);
    Assert.assertNull(null, userWithRole.userId());
    Assert.assertEquals("test", userWithRole.username());
    Assert.assertNull(userWithRole.role());
}
Also used : AuthManager(com.baidu.hugegraph.auth.AuthManager) UserWithRole(com.baidu.hugegraph.auth.UserWithRole) Id(com.baidu.hugegraph.backend.id.Id) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 38 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method clearAll.

@After
public void clearAll() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    for (HugeUser user : authManager.listAllUsers(-1)) {
        authManager.deleteUser(user.id());
    }
    for (HugeGroup group : authManager.listAllGroups(-1)) {
        authManager.deleteGroup(group.id());
    }
    for (HugeTarget target : authManager.listAllTargets(-1)) {
        authManager.deleteTarget(target.id());
    }
    for (HugeProject project : authManager.listAllProject(-1)) {
        if (!CollectionUtils.isEmpty(project.graphs())) {
            authManager.projectRemoveGraphs(project.id(), project.graphs());
        }
        authManager.deleteProject(project.id());
    }
    Assert.assertEquals(0, authManager.listAllAccess(-1).size());
    Assert.assertEquals(0, authManager.listAllBelong(-1).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeProject(com.baidu.hugegraph.auth.HugeProject) HugeGroup(com.baidu.hugegraph.auth.HugeGroup) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) HugeUser(com.baidu.hugegraph.auth.HugeUser) After(org.junit.After)

Example 39 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testListAllUsers.

@Test
public void testListAllUsers() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    authManager.createUser(makeUser("tom", "pass1"));
    authManager.createUser(makeUser("james", "pass2"));
    List<HugeUser> users = authManager.listAllUsers(-1);
    Assert.assertEquals(2, users.size());
    Assert.assertEquals(ImmutableSet.of("tom", "james"), ImmutableSet.of(users.get(0).name(), users.get(1).name()));
    Assert.assertEquals(0, authManager.listAllUsers(0).size());
    Assert.assertEquals(1, authManager.listAllUsers(1).size());
    Assert.assertEquals(2, authManager.listAllUsers(2).size());
    Assert.assertEquals(2, authManager.listAllUsers(3).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 40 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testListAllBelong.

@Test
public void testListAllBelong() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id user = authManager.createUser(makeUser("tom", "pass1"));
    Id group1 = authManager.createGroup(makeGroup("group1"));
    Id group2 = authManager.createGroup(makeGroup("group2"));
    authManager.createBelong(makeBelong(user, group1));
    authManager.createBelong(makeBelong(user, group2));
    List<HugeBelong> belongs = authManager.listAllBelong(-1);
    Assert.assertEquals(2, belongs.size());
    Assert.assertEquals(ImmutableSet.of(group1, group2), ImmutableSet.of(belongs.get(0).target(), belongs.get(1).target()));
    Assert.assertEquals(0, authManager.listAllBelong(0).size());
    Assert.assertEquals(1, authManager.listAllBelong(1).size());
    Assert.assertEquals(2, authManager.listAllBelong(2).size());
    Assert.assertEquals(2, authManager.listAllBelong(3).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Aggregations

AuthManager (com.baidu.hugegraph.auth.AuthManager)46 Test (org.junit.Test)43 Id (com.baidu.hugegraph.backend.id.Id)39 HugeGraph (com.baidu.hugegraph.HugeGraph)36 HugeUser (com.baidu.hugegraph.auth.HugeUser)12 HugeProject (com.baidu.hugegraph.auth.HugeProject)8 HugeTarget (com.baidu.hugegraph.auth.HugeTarget)8 HugeGroup (com.baidu.hugegraph.auth.HugeGroup)7 HugeAccess (com.baidu.hugegraph.auth.HugeAccess)6 HugeBelong (com.baidu.hugegraph.auth.HugeBelong)6 HashMap (java.util.HashMap)6 Date (java.util.Date)5 UserWithRole (com.baidu.hugegraph.auth.UserWithRole)2 HugeResource (com.baidu.hugegraph.auth.HugeResource)1 RolePermission (com.baidu.hugegraph.auth.RolePermission)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 Timed (com.codahale.metrics.annotation.Timed)1 Consumes (jakarta.ws.rs.Consumes)1 PUT (jakarta.ws.rs.PUT)1 Path (jakarta.ws.rs.Path)1