Skip to content

Commit 8c1ee71

Browse files
Thespicaimbajin
andauthored
feat(server): LoginAPI support token_expire field (#2754)
Co-authored-by: imbajin <jin@apache.org>
1 parent 1badd93 commit 8c1ee71

File tree

5 files changed

+188
-171
lines changed

5 files changed

+188
-171
lines changed

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/LoginAPI.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public String login(@Context GraphManager manager, @PathParam("graph") String gr
7070
checkCreatingBody(jsonLogin);
7171

7272
try {
73-
String token = manager.authManager().loginUser(jsonLogin.name, jsonLogin.password);
73+
String token = manager.authManager()
74+
.loginUser(jsonLogin.name, jsonLogin.password, jsonLogin.expire);
7475
HugeGraph g = graph(manager, graph);
7576
return manager.serializer(g).writeMap(ImmutableMap.of("token", token));
7677
} catch (AuthenticationException e) {
@@ -131,6 +132,8 @@ private static class JsonLogin implements Checkable {
131132
private String name;
132133
@JsonProperty("user_password")
133134
private String password;
135+
@JsonProperty("token_expire")
136+
private long expire;
134137

135138
@Override
136139
public void checkCreate(boolean isBatch) {

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

Lines changed: 99 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,16 @@
110110

111111
public final class HugeGraphAuthProxy implements HugeGraph {
112112

113+
private static final Logger LOG = Log.logger(HugeGraphAuthProxy.class);
114+
private static final ThreadLocal<Context> CONTEXTS = new InheritableThreadLocal<>();
115+
113116
static {
114117
HugeGraph.registerTraversalStrategies(HugeGraphAuthProxy.class);
115118
}
116119

117-
private static final Logger LOG = Log.logger(HugeGraphAuthProxy.class);
118120
private final Cache<Id, UserWithRole> usersRoleCache;
119121
private final Cache<Id, RateLimiter> auditLimiters;
120122
private final double auditLogMaxRate;
121-
122123
private final HugeGraph hugegraph;
123124
private final TaskSchedulerProxy taskScheduler;
124125
private final AuthManagerProxy authManager;
@@ -141,6 +142,40 @@ public HugeGraphAuthProxy(HugeGraph hugegraph) {
141142
LOG.info("Audit log rate limit is {}/s", this.auditLogMaxRate);
142143
}
143144

145+
static Context setContext(Context context) {
146+
Context old = CONTEXTS.get();
147+
CONTEXTS.set(context);
148+
return old;
149+
}
150+
151+
static void resetContext() {
152+
CONTEXTS.remove();
153+
}
154+
155+
private static Context getContext() {
156+
// Return task context first
157+
String taskContext = TaskManager.getContext();
158+
User user = User.fromJson(taskContext);
159+
if (user != null) {
160+
return new Context(user);
161+
}
162+
163+
return CONTEXTS.get();
164+
}
165+
166+
private static String getContextString() {
167+
Context context = getContext();
168+
if (context == null) {
169+
return null;
170+
}
171+
return context.user().toJson();
172+
}
173+
174+
static void logUser(User user, String path) {
175+
LOG.info("User '{}' login from client [{}] with path '{}'",
176+
user.username(), user.client(), path);
177+
}
178+
144179
@Override
145180
public HugeGraph hugegraph() {
146181
this.verifyAdminPermission();
@@ -1016,6 +1051,61 @@ else if (ro.type().isGrantOrUser()) {
10161051
return result;
10171052
}
10181053

1054+
static class Context {
1055+
1056+
private static final Context ADMIN = new Context(User.ADMIN);
1057+
1058+
private final User user;
1059+
1060+
public Context(User user) {
1061+
E.checkNotNull(user, "user");
1062+
this.user = user;
1063+
}
1064+
1065+
public static Context admin() {
1066+
return ADMIN;
1067+
}
1068+
1069+
public User user() {
1070+
return this.user;
1071+
}
1072+
}
1073+
1074+
static class ContextTask implements Runnable {
1075+
1076+
private final Runnable runner;
1077+
private final Context context;
1078+
1079+
public ContextTask(Runnable runner) {
1080+
this.context = getContext();
1081+
this.runner = runner;
1082+
}
1083+
1084+
@Override
1085+
public void run() {
1086+
setContext(this.context);
1087+
try {
1088+
this.runner.run();
1089+
} finally {
1090+
resetContext();
1091+
}
1092+
}
1093+
}
1094+
1095+
public static class ContextThreadPoolExecutor extends ThreadPoolExecutor {
1096+
1097+
public ContextThreadPoolExecutor(int corePoolSize, int maxPoolSize,
1098+
ThreadFactory threadFactory) {
1099+
super(corePoolSize, maxPoolSize, 0L, TimeUnit.MILLISECONDS,
1100+
new LinkedBlockingQueue<>(), threadFactory);
1101+
}
1102+
1103+
@Override
1104+
public void execute(Runnable command) {
1105+
super.execute(new ContextTask(command));
1106+
}
1107+
}
1108+
10191109
class TaskSchedulerProxy implements TaskScheduler {
10201110

10211111
private final TaskScheduler taskScheduler;
@@ -1622,8 +1712,14 @@ public void enabledWhiteIpList(boolean status) {
16221712

16231713
@Override
16241714
public String loginUser(String username, String password) {
1715+
return this.loginUser(username, password, -1L);
1716+
}
1717+
1718+
// TODO: the expire haven't been implemented yet
1719+
@Override
1720+
public String loginUser(String username, String password, long expire) {
16251721
try {
1626-
return this.authManager.loginUser(username, password);
1722+
return this.authManager.loginUser(username, password, expire);
16271723
} catch (AuthenticationException e) {
16281724
throw new NotAuthorizedException(e.getMessage(), e);
16291725
}
@@ -1841,95 +1937,4 @@ public String toString() {
18411937
return this.origin.toString();
18421938
}
18431939
}
1844-
1845-
private static final ThreadLocal<Context> CONTEXTS = new InheritableThreadLocal<>();
1846-
1847-
protected static Context setContext(Context context) {
1848-
Context old = CONTEXTS.get();
1849-
CONTEXTS.set(context);
1850-
return old;
1851-
}
1852-
1853-
protected static void resetContext() {
1854-
CONTEXTS.remove();
1855-
}
1856-
1857-
protected static Context getContext() {
1858-
// Return task context first
1859-
String taskContext = TaskManager.getContext();
1860-
User user = User.fromJson(taskContext);
1861-
if (user != null) {
1862-
return new Context(user);
1863-
}
1864-
1865-
return CONTEXTS.get();
1866-
}
1867-
1868-
protected static String getContextString() {
1869-
Context context = getContext();
1870-
if (context == null) {
1871-
return null;
1872-
}
1873-
return context.user().toJson();
1874-
}
1875-
1876-
protected static void logUser(User user, String path) {
1877-
LOG.info("User '{}' login from client [{}] with path '{}'",
1878-
user.username(), user.client(), path);
1879-
}
1880-
1881-
static class Context {
1882-
1883-
private static final Context ADMIN = new Context(User.ADMIN);
1884-
1885-
private final User user;
1886-
1887-
public Context(User user) {
1888-
E.checkNotNull(user, "user");
1889-
this.user = user;
1890-
}
1891-
1892-
public User user() {
1893-
return this.user;
1894-
}
1895-
1896-
public static Context admin() {
1897-
return ADMIN;
1898-
}
1899-
}
1900-
1901-
static class ContextTask implements Runnable {
1902-
1903-
private final Runnable runner;
1904-
private final Context context;
1905-
1906-
public ContextTask(Runnable runner) {
1907-
this.context = getContext();
1908-
this.runner = runner;
1909-
}
1910-
1911-
@Override
1912-
public void run() {
1913-
setContext(this.context);
1914-
try {
1915-
this.runner.run();
1916-
} finally {
1917-
resetContext();
1918-
}
1919-
}
1920-
}
1921-
1922-
public static class ContextThreadPoolExecutor extends ThreadPoolExecutor {
1923-
1924-
public ContextThreadPoolExecutor(int corePoolSize, int maxPoolSize,
1925-
ThreadFactory threadFactory) {
1926-
super(corePoolSize, maxPoolSize, 0L, TimeUnit.MILLISECONDS,
1927-
new LinkedBlockingQueue<>(), threadFactory);
1928-
}
1929-
1930-
@Override
1931-
public void execute(Runnable command) {
1932-
super.execute(new ContextTask(command));
1933-
}
1934-
}
19351940
}

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/auth/AuthManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ public interface AuthManager {
121121

122122
String loginUser(String username, String password) throws AuthenticationException;
123123

124+
String loginUser(String username, String password, long expire) throws AuthenticationException;
125+
124126
void logoutUser(String token);
125127

126128
UserWithRole validateUser(String username, String password);

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/auth/StandardAuthManager.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ public StandardAuthManager(HugeGraphParams graph) {
114114
this.ipWhiteListEnabled = false;
115115
}
116116

117+
/**
118+
* Maybe can define an proxy class to choose forward or call local
119+
*/
120+
public static boolean isLocal(AuthManager authManager) {
121+
return authManager instanceof StandardAuthManager;
122+
}
123+
117124
private <V> Cache<Id, V> cache(String prefix, long capacity,
118125
long expiredTime) {
119126
String name = prefix + "-" + this.graph.name();
@@ -636,6 +643,13 @@ private RolePermission rolePermission(HugeTarget target) {
636643
@Override
637644
public String loginUser(String username, String password)
638645
throws AuthenticationException {
646+
return this.loginUser(username, password, -1L);
647+
}
648+
649+
// TODO: the expire haven't been implemented yet
650+
@Override
651+
public String loginUser(String username, String password, long expire)
652+
throws AuthenticationException {
639653
HugeUser user = this.matchUser(username, password);
640654
if (user == null) {
641655
String msg = "Incorrect username or password";
@@ -717,13 +731,6 @@ public void enabledWhiteIpList(boolean status) {
717731
this.ipWhiteListEnabled = status;
718732
}
719733

720-
/**
721-
* Maybe can define an proxy class to choose forward or call local
722-
*/
723-
public static boolean isLocal(AuthManager authManager) {
724-
return authManager instanceof StandardAuthManager;
725-
}
726-
727734
public <R> R commit(Callable<R> callable) {
728735
this.groups.autoCommit(false);
729736
this.access.autoCommit(false);

0 commit comments

Comments
 (0)