Мульти AuthHandler's

This commit is contained in:
Gravit 2018-09-23 19:07:07 +07:00
parent 363a3ed462
commit 23a1c9a7e1
19 changed files with 69 additions and 41 deletions

View file

@ -73,7 +73,7 @@ public static final class Config extends ConfigObject {
// Handlers & Providers // Handlers & Providers
@LauncherAPI @LauncherAPI
public final AuthHandler authHandler; public final AuthHandler[] authHandler;
@LauncherAPI @LauncherAPI
public final AuthProvider[] authProvider; public final AuthProvider[] authProvider;
@LauncherAPI @LauncherAPI
@ -105,7 +105,7 @@ public static final class Config extends ConfigObject {
private final StringConfigEntry address; private final StringConfigEntry address;
private final String bindAddress; private final String bindAddress;
private Config(BlockConfigEntry block, Path coredir) { private Config(BlockConfigEntry block, Path coredir,LaunchServer server) {
super(block); super(block);
address = block.getEntry("address", StringConfigEntry.class); address = block.getEntry("address", StringConfigEntry.class);
port = VerifyHelper.verifyInt(block.getEntryValue("port", IntegerConfigEntry.class), port = VerifyHelper.verifyInt(block.getEntryValue("port", IntegerConfigEntry.class),
@ -123,11 +123,12 @@ private Config(BlockConfigEntry block, Path coredir) {
// Set handlers & providers // Set handlers & providers
authHandler = AuthHandler.newHandler(block.getEntryValue("authHandler", StringConfigEntry.class), authHandler = new AuthHandler[1];
authHandler[0] = AuthHandler.newHandler(block.getEntryValue("authHandler", StringConfigEntry.class),
block.getEntry("authHandlerConfig", BlockConfigEntry.class)); block.getEntry("authHandlerConfig", BlockConfigEntry.class));
authProvider = new AuthProvider[1]; authProvider = new AuthProvider[1];
authProvider[0] = AuthProvider.newProvider(block.getEntryValue("authProvider", StringConfigEntry.class), authProvider[0] = AuthProvider.newProvider(block.getEntryValue("authProvider", StringConfigEntry.class),
block.getEntry("authProviderConfig", BlockConfigEntry.class)); block.getEntry("authProviderConfig", BlockConfigEntry.class),server);
textureProvider = TextureProvider.newProvider(block.getEntryValue("textureProvider", StringConfigEntry.class), textureProvider = TextureProvider.newProvider(block.getEntryValue("textureProvider", StringConfigEntry.class),
block.getEntry("textureProviderConfig", BlockConfigEntry.class)); block.getEntry("textureProviderConfig", BlockConfigEntry.class));
hwidHandler = HWIDHandler.newHandler(block.getEntryValue("hwidHandler", StringConfigEntry.class), hwidHandler = HWIDHandler.newHandler(block.getEntryValue("hwidHandler", StringConfigEntry.class),
@ -400,7 +401,7 @@ public LaunchServer(Path dir, boolean portable) throws IOException, InvalidKeySp
generateConfigIfNotExists(); generateConfigIfNotExists();
LogHelper.info("Reading LaunchServer config file"); LogHelper.info("Reading LaunchServer config file");
try (BufferedReader reader = IOHelper.newReader(configFile)) { try (BufferedReader reader = IOHelper.newReader(configFile)) {
config = new Config(TextConfigReader.read(reader, true), dir); config = new Config(TextConfigReader.read(reader, true), dir,this);
} }
config.verify(); config.verify();
@ -455,7 +456,7 @@ public void close() {
// Close handlers & providers // Close handlers & providers
try { try {
config.authHandler.close(); for(AuthHandler h : config.authHandler) h.close();
} catch (IOException e) { } catch (IOException e) {
LogHelper.error(e); LogHelper.error(e);
} }
@ -483,7 +484,7 @@ private void generateConfigIfNotExists() throws IOException {
LogHelper.info("Creating LaunchServer config"); LogHelper.info("Creating LaunchServer config");
Config newConfig; Config newConfig;
try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launchserver/defaults/config.cfg"))) { try (BufferedReader reader = IOHelper.newReader(IOHelper.getResourceURL("ru/gravit/launchserver/defaults/config.cfg"))) {
newConfig = new Config(TextConfigReader.read(reader, false), dir); newConfig = new Config(TextConfigReader.read(reader, false), dir,this);
} }
// Set server address // Set server address

View file

@ -1,11 +1,12 @@
package ru.gravit.launchserver.auth.provider; package ru.gravit.launchserver.auth.provider;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
public final class AcceptAuthProvider extends AuthProvider { public final class AcceptAuthProvider extends AuthProvider {
public AcceptAuthProvider(BlockConfigEntry block) { public AcceptAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
} }
@Override @Override

View file

@ -6,14 +6,17 @@
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launcher.serialize.config.ConfigObject; import ru.gravit.launcher.serialize.config.ConfigObject;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
public abstract class AuthProvider extends ConfigObject implements AutoCloseable { public abstract class AuthProvider extends ConfigObject implements AutoCloseable {
private static final Map<String, Adapter<AuthProvider>> AUTH_PROVIDERS = new ConcurrentHashMap<>(8); private static final Map<String, ServerAdapter<AuthProvider>> AUTH_PROVIDERS = new ConcurrentHashMap<>(8);
private static boolean registredProv = false; private static boolean registredProv = false;
private LaunchServer server;
@LauncherAPI @LauncherAPI
public static AuthProviderResult authError(String message) throws AuthException { public static AuthProviderResult authError(String message) throws AuthException {
@ -21,15 +24,15 @@ public static AuthProviderResult authError(String message) throws AuthException
} }
@LauncherAPI @LauncherAPI
public static AuthProvider newProvider(String name, BlockConfigEntry block) { public static AuthProvider newProvider(String name, BlockConfigEntry block,LaunchServer server) {
VerifyHelper.verifyIDName(name); VerifyHelper.verifyIDName(name);
Adapter<AuthProvider> authHandlerAdapter = VerifyHelper.getMapValue(AUTH_PROVIDERS, name, ServerAdapter<AuthProvider> authHandlerAdapter = VerifyHelper.getMapValue(AUTH_PROVIDERS, name,
String.format("Unknown auth provider: '%s'", name)); String.format("Unknown auth provider: '%s'", name));
return authHandlerAdapter.convert(block); return authHandlerAdapter.convert(block,server);
} }
@LauncherAPI @LauncherAPI
public static void registerProvider(String name, Adapter<AuthProvider> adapter) { public static void registerProvider(String name, ServerAdapter<AuthProvider> adapter) {
VerifyHelper.putIfAbsent(AUTH_PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"), VerifyHelper.putIfAbsent(AUTH_PROVIDERS, name, Objects.requireNonNull(adapter, "adapter"),
String.format("Auth provider has been already registered: '%s'", name)); String.format("Auth provider has been already registered: '%s'", name));
} }
@ -49,10 +52,15 @@ public static void registerProviders() {
registredProv = true; registredProv = true;
} }
} }
public AuthHandler getAccociateHandler(int this_position)
{
return server.config.authHandler[this_position];
}
@LauncherAPI @LauncherAPI
protected AuthProvider(BlockConfigEntry block) { protected AuthProvider(BlockConfigEntry block, LaunchServer launchServer) {
super(block); super(block);
server = launchServer;
} }
@LauncherAPI @LauncherAPI
@ -60,4 +68,9 @@ protected AuthProvider(BlockConfigEntry block) {
@Override @Override
public abstract void close() throws IOException; public abstract void close() throws IOException;
@FunctionalInterface
public interface ServerAdapter<O extends ConfigObject> {
@LauncherAPI
O convert(BlockConfigEntry entry,LaunchServer server);
}
} }

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.auth.provider; package ru.gravit.launchserver.auth.provider;
import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.SecurityHelper; import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.SecurityHelper.DigestAlgorithm; import ru.gravit.utils.helper.SecurityHelper.DigestAlgorithm;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
@ -11,8 +12,8 @@ public abstract class DigestAuthProvider extends AuthProvider {
private final DigestAlgorithm digest; private final DigestAlgorithm digest;
@LauncherAPI @LauncherAPI
protected DigestAuthProvider(BlockConfigEntry block) { protected DigestAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
digest = DigestAlgorithm.byName(block.getEntryValue("digest", StringConfigEntry.class)); digest = DigestAlgorithm.byName(block.getEntryValue("digest", StringConfigEntry.class));
} }

View file

@ -8,6 +8,7 @@
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import ru.gravit.launchserver.LaunchServer;
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.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -43,8 +44,8 @@ private Entry(BlockConfigEntry block) {
private FileTime cacheLastModified; private FileTime cacheLastModified;
public FileAuthProvider(BlockConfigEntry block) { public FileAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
file = IOHelper.toPath(block.getEntryValue("file", StringConfigEntry.class)); file = IOHelper.toPath(block.getEntryValue("file", StringConfigEntry.class));
// Try to update cache // Try to update cache

View file

@ -7,6 +7,7 @@
import com.eclipsesource.json.JsonObject; import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue; import com.eclipsesource.json.JsonValue;
import ru.gravit.launchserver.LaunchServer;
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;
@ -25,8 +26,8 @@ public final class JsonAuthProvider extends AuthProvider {
private final String responseUserKeyName; private final String responseUserKeyName;
private final String responseErrorKeyName; private final String responseErrorKeyName;
JsonAuthProvider(BlockConfigEntry block) { JsonAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
String configUrl = block.getEntryValue("url", StringConfigEntry.class); String configUrl = block.getEntryValue("url", StringConfigEntry.class);
userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class), userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class),
VerifyHelper.NOT_EMPTY, "Username key name can't be empty"); VerifyHelper.NOT_EMPTY, "Username key name can't be empty");

View file

@ -16,6 +16,7 @@
import com.eclipsesource.json.JsonValue; import com.eclipsesource.json.JsonValue;
import com.eclipsesource.json.WriterConfig; import com.eclipsesource.json.WriterConfig;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
@ -53,8 +54,8 @@ public static JsonObject makeJSONRequest(URL url, JsonObject request) throws IOE
} }
} }
public MojangAuthProvider(BlockConfigEntry block) { public MojangAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
} }
@Override @Override

View file

@ -5,6 +5,8 @@
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import clojure.lang.IFn;
import ru.gravit.launchserver.LaunchServer;
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,8 +21,8 @@ public final class MySQLAuthProvider extends AuthProvider {
private final String query; private final String query;
private final String[] queryParams; private final String[] queryParams;
public MySQLAuthProvider(BlockConfigEntry block) { public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
mySQLHolder = new MySQLSourceConfig("authProviderPool", block); mySQLHolder = new MySQLSourceConfig("authProviderPool", block);
// Read query // Read query

View file

@ -4,14 +4,15 @@
import java.util.Objects; import java.util.Objects;
import ru.gravit.launcher.LauncherAPI; import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
public final class NullAuthProvider extends AuthProvider { public final class NullAuthProvider extends AuthProvider {
private volatile AuthProvider provider; private volatile AuthProvider provider;
public NullAuthProvider(BlockConfigEntry block) { public NullAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
} }
@Override @Override

View file

@ -1,5 +1,6 @@
package ru.gravit.launchserver.auth.provider; package ru.gravit.launchserver.auth.provider;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry; import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
import ru.gravit.launcher.serialize.config.entry.StringConfigEntry; import ru.gravit.launcher.serialize.config.entry.StringConfigEntry;
@ -8,8 +9,8 @@
public final class RejectAuthProvider extends AuthProvider { public final class RejectAuthProvider extends AuthProvider {
private final String message; private final String message;
public RejectAuthProvider(BlockConfigEntry block) { public RejectAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); super(block,server);
message = VerifyHelper.verify(block.getEntryValue("message", StringConfigEntry.class), VerifyHelper.NOT_EMPTY, message = VerifyHelper.verify(block.getEntryValue("message", StringConfigEntry.class), VerifyHelper.NOT_EMPTY,
"Auth error message can't be empty"); "Auth error message can't be empty");
} }

View file

@ -5,6 +5,7 @@
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import ru.gravit.launchserver.LaunchServer;
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,8 +16,8 @@ public final class RequestAuthProvider extends AuthProvider {
private final String url; private final String url;
private final Pattern response; private final Pattern response;
public RequestAuthProvider(BlockConfigEntry block) { public RequestAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block); 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));

View file

@ -2,6 +2,7 @@
import java.util.UUID; import java.util.UUID;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
@ -27,10 +28,13 @@ public void invoke(String... args) throws Exception {
verifyArgs(args, 2); verifyArgs(args, 2);
String login = args[0]; String login = args[0];
String password = args[1]; String password = args[1];
int auth_id = 0;
if(args.length >= 3) auth_id = Integer.valueOf(args[3]);
// Authenticate // Authenticate
AuthProviderResult result = server.config.authProvider.auth(login, password, "127.0.0.1"); AuthProvider provider = server.config.authProvider[auth_id];
UUID uuid = server.config.authHandler.auth(result); AuthProviderResult result = provider.auth(login, password, "127.0.0.1");
UUID uuid = provider.getAccociateHandler(auth_id).auth(result);
// Print auth successful message // Print auth successful message
LogHelper.subInfo("UUID: %s, Username: '%s', Access Token: '%s'", uuid, result.username, result.accessToken); LogHelper.subInfo("UUID: %s, Username: '%s', Access Token: '%s'", uuid, result.username, result.accessToken);

View file

@ -29,7 +29,7 @@ public void invoke(String... args) throws CommandException, IOException {
UUID uuid = parseUUID(args[0]); UUID uuid = parseUUID(args[0]);
// Get UUID by username // Get UUID by username
String username = server.config.authHandler.uuidToUsername(uuid); String username = server.config.authHandler[0].uuidToUsername(uuid);
if (username == null) if (username == null)
throw new CommandException("Unknown UUID: " + uuid); throw new CommandException("Unknown UUID: " + uuid);

View file

@ -29,7 +29,7 @@ public void invoke(String... args) throws CommandException, IOException {
String username = parseUsername(args[0]); String username = parseUsername(args[0]);
// Get UUID by username // Get UUID by username
UUID uuid = server.config.authHandler.usernameToUUID(username); UUID uuid = server.config.authHandler[0].usernameToUUID(username);
if (uuid == null) if (uuid == null)
throw new CommandException(String.format("Unknown username: '%s'", username)); throw new CommandException(String.format("Unknown username: '%s'", username));

View file

@ -59,12 +59,12 @@ public void reply() throws Exception {
// Authenticate // Authenticate
debug("Login: '%s', Password: '%s'", login, echo(password.length())); debug("Login: '%s', Password: '%s'", login, echo(password.length()));
AuthProviderResult result; AuthProviderResult result;
AuthProvider provider = server.config.authProvider[auth_id];
try { try {
if (server.limiter.isLimit(ip)) { if (server.limiter.isLimit(ip)) {
AuthProvider.authError(server.config.authRejectString); AuthProvider.authError(server.config.authRejectString);
return; return;
} }
AuthProvider provider = server.config.authProvider[auth_id];
result = provider.auth(login, password, ip); result = provider.auth(login, password, ip);
if (!VerifyHelper.isValidUsername(result.username)) { if (!VerifyHelper.isValidUsername(result.username)) {
AuthProvider.authError(String.format("Illegal result: '%s'", result.username)); AuthProvider.authError(String.format("Illegal result: '%s'", result.username));
@ -88,7 +88,7 @@ public void reply() throws Exception {
// Authenticate on server (and get UUID) // Authenticate on server (and get UUID)
UUID uuid; UUID uuid;
try { try {
uuid = server.config.authHandler.auth(result); uuid = provider.getAccociateHandler(auth_id).auth(result);
} catch (AuthException e) { } catch (AuthException e) {
requestError(e.getMessage()); requestError(e.getMessage());
return; return;

View file

@ -29,7 +29,7 @@ public void reply() throws IOException {
// Try check server with auth handler // Try check server with auth handler
UUID uuid; UUID uuid;
try { try {
uuid = server.config.authHandler.checkServer(username, serverID); uuid = server.config.authHandler[0].checkServer(username, serverID);
} catch (AuthException e) { } catch (AuthException e) {
requestError(e.getMessage()); requestError(e.getMessage());
return; return;

View file

@ -28,7 +28,7 @@ public void reply() throws IOException {
debug("Username: '%s', Access token: %s, Server ID: %s", username, accessToken, serverID); debug("Username: '%s', Access token: %s, Server ID: %s", username, accessToken, serverID);
boolean success; boolean success;
try { try {
success = server.config.authHandler.joinServer(username, accessToken, serverID); success = server.config.authHandler[0].joinServer(username, accessToken, serverID);
} catch (AuthException e) { } catch (AuthException e) {
requestError(e.getMessage()); requestError(e.getMessage());
return; return;

View file

@ -47,7 +47,7 @@ public void reply() throws IOException {
debug("UUID: " + uuid); debug("UUID: " + uuid);
String client = input.readString(SerializeLimits.MAX_CLIENT); String client = input.readString(SerializeLimits.MAX_CLIENT);
// Verify has such profile // Verify has such profile
String username = server.config.authHandler.uuidToUsername(uuid); String username = server.config.authHandler[0].uuidToUsername(uuid);
if (username == null) { if (username == null) {
output.writeBoolean(false); output.writeBoolean(false);
return; return;

View file

@ -13,7 +13,7 @@
public final class ProfileByUsernameResponse extends Response { public final class ProfileByUsernameResponse extends Response {
public static void writeProfile(LaunchServer server, HOutput output, String username, String client) throws IOException { public static void writeProfile(LaunchServer server, HOutput output, String username, String client) throws IOException {
UUID uuid = server.config.authHandler.usernameToUUID(username); UUID uuid = server.config.authHandler[0].usernameToUUID(username);
if (uuid == null) { if (uuid == null) {
output.writeBoolean(false); output.writeBoolean(false);
return; return;