Search in sources :

Example 1 with RestClient

use of com.baidu.hugegraph.client.RestClient in project incubator-hugegraph-toolchain by apache.

the class LogoutApiTest method testLogout.

@Test
public void testLogout() {
    User user1 = new User();
    user1.name("user1");
    user1.password("p1");
    userAPI.create(user1);
    Login login = new Login();
    login.name("user1");
    login.password("p1");
    LoginResult result = loginAPI.login(login);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.token());
    // Client will set Authentication Header use Basic
    Assert.assertThrows(ServerException.class, () -> {
        logoutAPI.logout();
    }, e -> {
        Assert.assertContains("Only HTTP Bearer authentication is supported", e.getMessage());
    });
    String token = result.token();
    RestClient client = Whitebox.getInternalState(logoutAPI, "client");
    client.setAuthContext("Bearer " + token);
    logoutAPI.logout();
}
Also used : User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) RestClient(com.baidu.hugegraph.client.RestClient) Login(com.baidu.hugegraph.structure.auth.Login) Test(org.junit.Test)

Example 2 with RestClient

use of com.baidu.hugegraph.client.RestClient in project incubator-hugegraph-toolchain by apache.

the class TokenApiTest method testVerify.

@Test
public void testVerify() {
    User user1 = new User();
    user1.name("user1");
    user1.password("p1");
    User user = userAPI.create(user1);
    Login login = new Login();
    login.name("user1");
    login.password("p1");
    LoginResult result = loginAPI.login(login);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.token());
    // Client will set Authentication Header use Basic
    Assert.assertThrows(ServerException.class, () -> {
        tokenAPI.verifyToken();
    }, e -> {
        Assert.assertContains("Only HTTP Bearer authentication is supported", e.getMessage());
    });
    String token = result.token();
    RestClient client = Whitebox.getInternalState(tokenAPI, "client");
    client.setAuthContext("Bearer " + token);
    TokenPayload payload = tokenAPI.verifyToken();
    Assert.assertEquals("user1", payload.username());
    Assert.assertEquals(user.id(), payload.userId());
    client.setAuthContext("Bearer qweqwaasa");
    Assert.assertThrows(ServerException.class, () -> {
        tokenAPI.verifyToken();
    }, e -> {
        Assert.assertContains("Invalid token", e.getMessage());
    });
    RestClient client2 = Whitebox.getInternalState(logoutAPI, "client");
    Assert.assertThrows(ServerException.class, () -> {
        logoutAPI.logout();
    }, e -> {
        Assert.assertContains("Only HTTP Bearer authentication is supported", e.getMessage());
    });
    client2.setAuthContext("Bearer " + token);
    logoutAPI.logout();
}
Also used : User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) RestClient(com.baidu.hugegraph.client.RestClient) Login(com.baidu.hugegraph.structure.auth.Login) TokenPayload(com.baidu.hugegraph.structure.auth.TokenPayload) Test(org.junit.Test)

Aggregations

RestClient (com.baidu.hugegraph.client.RestClient)2 Login (com.baidu.hugegraph.structure.auth.Login)2 LoginResult (com.baidu.hugegraph.structure.auth.LoginResult)2 User (com.baidu.hugegraph.structure.auth.User)2 Test (org.junit.Test)2 TokenPayload (com.baidu.hugegraph.structure.auth.TokenPayload)1