Search in sources :

Example 1 with UserWithRole

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

the class LoginAPI method verifyToken.

@GET
@Timed
@Path("verify")
@Status(StatusFilter.Status.OK)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String verifyToken(@Context GraphManager manager, @PathParam("graph") String graph, @HeaderParam(HttpHeaders.AUTHORIZATION) String token) {
    E.checkArgument(StringUtils.isNotEmpty(token), "Request header Authorization must not be null");
    LOG.debug("Graph [{}] get user: {}", graph, token);
    if (!token.startsWith(AuthenticationFilter.BEARER_TOKEN_PREFIX)) {
        throw new BadRequestException("Only HTTP Bearer authentication is supported");
    }
    token = token.substring(AuthenticationFilter.BEARER_TOKEN_PREFIX.length());
    UserWithRole userWithRole = manager.authManager().validateUser(token);
    HugeGraph g = graph(manager, graph);
    return manager.serializer(g).writeMap(ImmutableMap.of(AuthConstant.TOKEN_USER_NAME, userWithRole.username(), AuthConstant.TOKEN_USER_ID, userWithRole.userId()));
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) UserWithRole(com.baidu.hugegraph.auth.UserWithRole) BadRequestException(jakarta.ws.rs.BadRequestException) Path(jakarta.ws.rs.Path) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 2 with UserWithRole

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

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

Aggregations

UserWithRole (com.baidu.hugegraph.auth.UserWithRole)3 AuthManager (com.baidu.hugegraph.auth.AuthManager)2 HugeUser (com.baidu.hugegraph.auth.HugeUser)2 Id (com.baidu.hugegraph.backend.id.Id)2 Test (org.junit.Test)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 Timed (com.codahale.metrics.annotation.Timed)1 BadRequestException (jakarta.ws.rs.BadRequestException)1 Consumes (jakarta.ws.rs.Consumes)1 GET (jakarta.ws.rs.GET)1 Path (jakarta.ws.rs.Path)1 Produces (jakarta.ws.rs.Produces)1