From f9aecb503638cdb249eb94a9136bf1e952c6d0b2 Mon Sep 17 00:00:00 2001 From: Gravit Date: Fri, 5 Oct 2018 15:25:12 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D1=80=D0=B8=D0=B2=D0=B8=D0=BB=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=B2=20JsonAuthProvider.java=20MySQLAuthProvid?= =?UTF-8?q?er.java=20RequestAuthProvider.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/gravit/launchserver/auth/ClientPermissions.java | 4 ++++ .../launchserver/auth/provider/JsonAuthProvider.java | 6 +++++- .../launchserver/auth/provider/MySQLAuthProvider.java | 6 +++++- .../launchserver/auth/provider/RequestAuthProvider.java | 7 +++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/ClientPermissions.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/ClientPermissions.java index 974daf9b..b1dde197 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/ClientPermissions.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/ClientPermissions.java @@ -9,4 +9,8 @@ public ClientPermissions() { canAdmin = false; canServer = false; } + public ClientPermissions(long data) { + canAdmin = (data & (1 << 0)) != 0; + canServer = (data & (1 << 1)) != 0; + } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java index 3deb29c2..1368ade3 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/JsonAuthProvider.java @@ -8,6 +8,7 @@ import com.eclipsesource.json.JsonValue; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.auth.ClientPermissions; import ru.gravit.utils.HTTPRequest; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.SecurityHelper; @@ -21,6 +22,7 @@ public final class JsonAuthProvider extends AuthProvider { private final String passKeyName; private final String ipKeyName; private final String responseUserKeyName; + private final String responsePermissionKeyName; private final String responseErrorKeyName; JsonAuthProvider(BlockConfigEntry block, LaunchServer server) { @@ -36,6 +38,8 @@ public final class JsonAuthProvider extends AuthProvider { VerifyHelper.NOT_EMPTY, "Response username key can't be empty"); responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class), VerifyHelper.NOT_EMPTY, "Response error key can't be empty"); + responsePermissionKeyName = VerifyHelper.verify(block.getEntryValue("responsePermissionKeyName", StringConfigEntry.class), + VerifyHelper.NOT_EMPTY, "Response error key can't be empty"); url = IOHelper.convertToURL(configUrl); } @@ -50,7 +54,7 @@ public AuthProviderResult auth(String login, String password, String ip) throws String value; if ((value = response.getString(responseUserKeyName, null)) != null) - return new AuthProviderResult(value, SecurityHelper.randomStringToken()); + return new AuthProviderResult(value, SecurityHelper.randomStringToken(),new ClientPermissions(response.getLong(responsePermissionKeyName,0))); else if ((value = response.getString(responseErrorKeyName, null)) != null) return authError(value); else diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java index e28b8cf5..4d33e2a5 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java @@ -5,7 +5,9 @@ import java.sql.ResultSet; import java.sql.SQLException; +import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.auth.ClientPermissions; import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.VerifyHelper; @@ -19,6 +21,7 @@ public final class MySQLAuthProvider extends AuthProvider { private final MySQLSourceConfig mySQLHolder; private final String query; private final String[] queryParams; + private final boolean usePermission; public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) { super(block,server); @@ -27,6 +30,7 @@ public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) { // Read query query = VerifyHelper.verify(block.getEntryValue("query", StringConfigEntry.class), VerifyHelper.NOT_EMPTY, "MySQL query can't be empty"); + usePermission = block.hasEntry("usePermission") ? block.getEntryValue("usePermission", BooleanConfigEntry.class) : false; queryParams = block.getEntry("queryParams", ListConfigEntry.class). stream(StringConfigEntry.class).toArray(String[]::new); } @@ -42,7 +46,7 @@ public AuthProviderResult auth(String login, String password, String ip) throws // Execute SQL query s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); try (ResultSet set = s.executeQuery()) { - return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken()) : authError("Incorrect username or password"); + return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), usePermission ? new ClientPermissions(set.getLong(2)) : new ClientPermissions()) : authError("Incorrect username or password"); } } diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RequestAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RequestAuthProvider.java index 3737ab39..cbc4aae1 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RequestAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/RequestAuthProvider.java @@ -5,7 +5,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry; import ru.gravit.launchserver.LaunchServer; +import ru.gravit.launchserver.auth.ClientPermissions; import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.SecurityHelper; @@ -15,12 +17,13 @@ public final class RequestAuthProvider extends AuthProvider { private final String url; private final Pattern response; + private final boolean usePermission; public RequestAuthProvider(BlockConfigEntry block, LaunchServer server) { super(block,server); url = block.getEntryValue("url", StringConfigEntry.class); response = Pattern.compile(block.getEntryValue("response", StringConfigEntry.class)); - + usePermission = block.hasEntry("usePermission") ? block.getEntryValue("usePermission", BooleanConfigEntry.class) : false; // Verify is valid URL IOHelper.verifyURL(getFormattedURL("urlAuthLogin", "urlAuthPassword", "127.0.0.1")); } @@ -32,7 +35,7 @@ public AuthProviderResult auth(String login, String password, String ip) throws // Match username Matcher matcher = response.matcher(currentResponse); return matcher.matches() && matcher.groupCount() >= 1 ? - new AuthProviderResult(matcher.group("username"), SecurityHelper.randomStringToken()) : + new AuthProviderResult(matcher.group("username"), SecurityHelper.randomStringToken(), usePermission ? new ClientPermissions(Long.getLong(matcher.group("permission"))) : new ClientPermissions()) : authError(currentResponse); }