Search in sources :

Example 11 with HugeUser

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

the class AuthTest method testUpdateUser.

@Test
public void testUpdateUser() throws InterruptedException {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id = authManager.createUser(makeUser("tom", "pass1"));
    HugeUser user = authManager.getUser(id);
    Assert.assertEquals("tom", user.name());
    Assert.assertEquals("pass1", user.password());
    Assert.assertEquals(user.create(), user.update());
    Date oldUpdateTime = user.update();
    Thread.sleep(1L);
    user.password("pass2");
    authManager.updateUser(user);
    HugeUser user2 = authManager.getUser(id);
    Assert.assertEquals("tom", user2.name());
    Assert.assertEquals("pass2", user2.password());
    Assert.assertEquals(oldUpdateTime, user2.create());
    Assert.assertNotEquals(oldUpdateTime, user2.update());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        authManager.updateUser(makeUser("tom2", "pass1"));
    }, e -> {
        Assert.assertContains("Can't save user", e.getMessage());
        Assert.assertContains("that not exists", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeUser(com.baidu.hugegraph.auth.HugeUser) Date(java.util.Date) Test(org.junit.Test)

Example 12 with HugeUser

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

the class AuthTest method testLogout.

@Test
public void testLogout() throws AuthenticationException {
    AuthManager authManager = graph().authManager();
    HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
    @SuppressWarnings("unused") Id userId = authManager.createUser(user);
    // Login
    String token = authManager.loginUser("test", "pass");
    // Logout
    Cache<Id, String> tokenCache = Whitebox.getInternalState(authManager, "tokenCache");
    Assert.assertTrue(tokenCache.containsKey(IdGenerator.of(token)));
    authManager.logoutUser(token);
    Assert.assertFalse(tokenCache.containsKey(IdGenerator.of(token)));
}
Also used : AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeUser(com.baidu.hugegraph.auth.HugeUser) Test(org.junit.Test)

Example 13 with HugeUser

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

the class AuthTest method testValidateUserByNameAndPassword.

@Test
public void testValidateUserByNameAndPassword() {
    AuthManager authManager = graph().authManager();
    HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
    Id userId = authManager.createUser(user);
    UserWithRole userWithRole;
    userWithRole = authManager.validateUser("test", "pass");
    Assert.assertEquals(userId, userWithRole.userId());
    Assert.assertEquals("test", userWithRole.username());
    Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
    // Error case
    userWithRole = authManager.validateUser("huge", "graph");
    Assert.assertNull(userWithRole.userId());
    Assert.assertEquals("huge", 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 14 with HugeUser

use of com.baidu.hugegraph.auth.HugeUser 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 15 with HugeUser

use of com.baidu.hugegraph.auth.HugeUser 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)

Aggregations

HugeUser (com.baidu.hugegraph.auth.HugeUser)19 HugeGraph (com.baidu.hugegraph.HugeGraph)13 AuthManager (com.baidu.hugegraph.auth.AuthManager)12 Test (org.junit.Test)12 Id (com.baidu.hugegraph.backend.id.Id)9 Timed (com.codahale.metrics.annotation.Timed)5 Produces (jakarta.ws.rs.Produces)5 GET (jakarta.ws.rs.GET)3 Path (jakarta.ws.rs.Path)3 HugeTarget (com.baidu.hugegraph.auth.HugeTarget)2 UserWithRole (com.baidu.hugegraph.auth.UserWithRole)2 Consumes (jakarta.ws.rs.Consumes)2 HashMap (java.util.HashMap)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 HugeGroup (com.baidu.hugegraph.auth.HugeGroup)1 HugeProject (com.baidu.hugegraph.auth.HugeProject)1 HugeResource (com.baidu.hugegraph.auth.HugeResource)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 POST (jakarta.ws.rs.POST)1 PUT (jakarta.ws.rs.PUT)1