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()));
}
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());
}
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());
}
Aggregations