Поддержка привилегий в JsonAuthProvider.java MySQLAuthProvider.java RequestAuthProvider.java

This commit is contained in:
Gravit 2018-10-05 15:25:12 +07:00
parent bbe3a5b111
commit f9aecb5036
4 changed files with 19 additions and 4 deletions

View file

@ -9,4 +9,8 @@ public ClientPermissions() {
canAdmin = false; canAdmin = false;
canServer = false; canServer = false;
} }
public ClientPermissions(long data) {
canAdmin = (data & (1 << 0)) != 0;
canServer = (data & (1 << 1)) != 0;
}
} }

View file

@ -8,6 +8,7 @@
import com.eclipsesource.json.JsonValue; import com.eclipsesource.json.JsonValue;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.ClientPermissions;
import ru.gravit.utils.HTTPRequest; import ru.gravit.utils.HTTPRequest;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
@ -21,6 +22,7 @@ public final class JsonAuthProvider extends AuthProvider {
private final String passKeyName; private final String passKeyName;
private final String ipKeyName; private final String ipKeyName;
private final String responseUserKeyName; private final String responseUserKeyName;
private final String responsePermissionKeyName;
private final String responseErrorKeyName; private final String responseErrorKeyName;
JsonAuthProvider(BlockConfigEntry block, LaunchServer server) { 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"); VerifyHelper.NOT_EMPTY, "Response username key can't be empty");
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class), responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Response error key can't be empty"); 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); url = IOHelper.convertToURL(configUrl);
} }
@ -50,7 +54,7 @@ public AuthProviderResult auth(String login, String password, String ip) throws
String value; String value;
if ((value = response.getString(responseUserKeyName, null)) != null) 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) else if ((value = response.getString(responseErrorKeyName, null)) != null)
return authError(value); return authError(value);
else else

View file

@ -5,7 +5,9 @@
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.ClientPermissions;
import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
@ -19,6 +21,7 @@ public final class MySQLAuthProvider extends AuthProvider {
private final MySQLSourceConfig mySQLHolder; private final MySQLSourceConfig mySQLHolder;
private final String query; private final String query;
private final String[] queryParams; private final String[] queryParams;
private final boolean usePermission;
public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) { public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server); super(block,server);
@ -27,6 +30,7 @@ public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) {
// Read query // Read query
query = VerifyHelper.verify(block.getEntryValue("query", StringConfigEntry.class), query = VerifyHelper.verify(block.getEntryValue("query", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "MySQL query can't be empty"); 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). queryParams = block.getEntry("queryParams", ListConfigEntry.class).
stream(StringConfigEntry.class).toArray(String[]::new); stream(StringConfigEntry.class).toArray(String[]::new);
} }
@ -42,7 +46,7 @@ public AuthProviderResult auth(String login, String password, String ip) throws
// Execute SQL query // Execute SQL query
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
try (ResultSet set = s.executeQuery()) { 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");
} }
} }

View file

@ -5,7 +5,9 @@
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import ru.gravit.launcher.serialize.config.entry.BooleanConfigEntry;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.ClientPermissions;
import ru.gravit.utils.helper.CommonHelper; import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
@ -15,12 +17,13 @@
public final class RequestAuthProvider extends AuthProvider { public final class RequestAuthProvider extends AuthProvider {
private final String url; private final String url;
private final Pattern response; private final Pattern response;
private final boolean usePermission;
public RequestAuthProvider(BlockConfigEntry block, LaunchServer server) { public RequestAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server); super(block,server);
url = block.getEntryValue("url", StringConfigEntry.class); url = block.getEntryValue("url", StringConfigEntry.class);
response = Pattern.compile(block.getEntryValue("response", StringConfigEntry.class)); response = Pattern.compile(block.getEntryValue("response", StringConfigEntry.class));
usePermission = block.hasEntry("usePermission") ? block.getEntryValue("usePermission", BooleanConfigEntry.class) : false;
// Verify is valid URL // Verify is valid URL
IOHelper.verifyURL(getFormattedURL("urlAuthLogin", "urlAuthPassword", "127.0.0.1")); 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 // Match username
Matcher matcher = response.matcher(currentResponse); Matcher matcher = response.matcher(currentResponse);
return matcher.matches() && matcher.groupCount() >= 1 ? 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); authError(currentResponse);
} }