From 56e1920a8d643c0e3638891ec636c95e2ceed779 Mon Sep 17 00:00:00 2001 From: Gravit Date: Tue, 31 Mar 2020 10:37:06 +0700 Subject: [PATCH] [FEATURE][EXPERIMENTAL] Flags in AuthProvider --- .../auth/provider/JsonAuthProvider.java | 3 ++- .../auth/provider/MySQLAuthProvider.java | 4 +++- .../auth/provider/PostgreSQLAuthProvider.java | 4 +++- .../auth/provider/RejectAuthProvider.java | 13 +++++++++++-- .../auth/provider/RequestAuthProvider.java | 4 +++- .../launchserver/dao/impl/UserHibernateImpl.java | 6 ++++-- .../dao/provider/HibernateDaoProvider.java | 2 ++ 7 files changed, 28 insertions(+), 8 deletions(-) diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java index 2bdc5dd0..7716ca42 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -21,6 +21,7 @@ public static class authResult { String username; String error; long permissions; + long flags; } public static class authRequest { @@ -54,7 +55,7 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p authResult result = gson.fromJson(content, authResult.class); if (result.username != null) - return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions)); + return new AuthProviderResult(result.username, SecurityHelper.randomStringToken(), new ClientPermissions(result.permissions, result.flags)); else if (result.error != null) return authError(result.error); else diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/MySQLAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/MySQLAuthProvider.java index aae966bf..1fe7558d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/MySQLAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/MySQLAuthProvider.java @@ -20,6 +20,7 @@ public final class MySQLAuthProvider extends AuthProvider { private String query; private String message; private String[] queryParams; + private boolean flagsEnabled; @Override public void init(LaunchServer srv) { @@ -41,7 +42,8 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p // Execute SQL query s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); try (ResultSet set = s.executeQuery()) { - return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions(set.getLong(2))) : authError(message); + return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions( + set.getLong(2), flagsEnabled ? set.getLong(3) : 0)) : authError(message); } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/PostgreSQLAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/PostgreSQLAuthProvider.java index 3599f249..32581d59 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/PostgreSQLAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/PostgreSQLAuthProvider.java @@ -19,6 +19,7 @@ public final class PostgreSQLAuthProvider extends AuthProvider { private String query; private String message; private String[] queryParams; + private boolean flagsEnabled; @Override public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws SQLException, AuthException { @@ -32,7 +33,8 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p // Execute SQL query s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT); try (ResultSet set = s.executeQuery()) { - return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions(set.getLong(2))) : authError(message); + return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), new ClientPermissions( + set.getLong(2), flagsEnabled ? set.getLong(3) : 0)) : authError(message); } } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java index aaf60d3b..ae04a567 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RejectAuthProvider.java @@ -22,7 +22,7 @@ public RejectAuthProvider(String message) { } private String message; - private ArrayList whitelist; + private ArrayList whitelist = new ArrayList<>(); @Override public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws AuthException { @@ -46,11 +46,20 @@ public Map getCommands() { Map commands = new HashMap<>(); commands.put("message", new SubCommand() { @Override - public void invoke(String... args) { + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); message = args[0]; LogHelper.info("New reject message: %s", message); } }); + commands.put("whirelist.add", new SubCommand() { + @Override + public void invoke(String... args) throws Exception { + verifyArgs(args, 1); + whitelist.add(args[0]); + LogHelper.info("%s added to whitelist", args[0]); + } + }); return commands; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RequestAuthProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RequestAuthProvider.java index e0f489bb..28f3a22e 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RequestAuthProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/provider/RequestAuthProvider.java @@ -19,6 +19,7 @@ public final class RequestAuthProvider extends AuthProvider { private String url; private transient Pattern pattern; private String response; + private boolean flagsEnabled; @Override public void init(LaunchServer srv) { @@ -36,7 +37,8 @@ public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface p // Match username Matcher matcher = pattern.matcher(currentResponse); return matcher.matches() && matcher.groupCount() >= 1 ? - new AuthProviderResult(matcher.group("username"), SecurityHelper.randomStringToken(), new ClientPermissions(Long.parseLong(matcher.group("permission")))) : + new AuthProviderResult(matcher.group("username"), SecurityHelper.randomStringToken(), new ClientPermissions( + Long.parseLong(matcher.group("permissions")), flagsEnabled ? Long.parseLong(matcher.group("flags")) : 0)) : authError(currentResponse); } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java index bfee7899..3b3d4ef7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/impl/UserHibernateImpl.java @@ -29,6 +29,7 @@ public class UserHibernateImpl implements User { public String serverID; private String password_salt; public long permissions; + public long flags; public void setPassword(String password) { password_salt = SecurityHelper.randomStringAESKey(); @@ -55,11 +56,12 @@ public boolean verifyPassword(String password) { } public ClientPermissions getPermissions() { - return new ClientPermissions(permissions); + return new ClientPermissions(permissions, flags); } public void setPermissions(ClientPermissions permissions) { - this.permissions = permissions.toLong(); + this.permissions = permissions.permissions; + this.flags = permissions.flags; } public String getAccessToken() { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java index 4d83a57d..ae3520c3 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/dao/provider/HibernateDaoProvider.java @@ -90,6 +90,7 @@ public void invoke(String... args) throws Exception { long perm = Long.parseLong(args[1]); boolean value = Boolean.parseBoolean(args[2]); permissions.setPermission(perm, value); + user.setPermissions(permissions); userDAO.update(user); } }); @@ -106,6 +107,7 @@ public void invoke(String... args) throws Exception { long perm = Long.parseLong(args[1]); boolean value = Boolean.parseBoolean(args[2]); permissions.setFlag(perm, value); + user.setPermissions(permissions); userDAO.update(user); } });