Мульти 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
@LauncherAPI
public final AuthHandler authHandler;
public final AuthHandler[] authHandler;
@LauncherAPI
public final AuthProvider[] authProvider;
@LauncherAPI
@ -105,7 +105,7 @@ public static final class Config extends ConfigObject {
private final StringConfigEntry address;
private final String bindAddress;
private Config(BlockConfigEntry block, Path coredir) {
private Config(BlockConfigEntry block, Path coredir,LaunchServer server) {
super(block);
address = block.getEntry("address", StringConfigEntry.class);
port = VerifyHelper.verifyInt(block.getEntryValue("port", IntegerConfigEntry.class),
@ -123,11 +123,12 @@ private Config(BlockConfigEntry block, Path coredir) {
// 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));
authProvider = new AuthProvider[1];
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),
block.getEntry("textureProviderConfig", BlockConfigEntry.class));
hwidHandler = HWIDHandler.newHandler(block.getEntryValue("hwidHandler", StringConfigEntry.class),
@ -400,7 +401,7 @@ public LaunchServer(Path dir, boolean portable) throws IOException, InvalidKeySp
generateConfigIfNotExists();
LogHelper.info("Reading LaunchServer config file");
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();
@ -455,7 +456,7 @@ public void close() {
// Close handlers & providers
try {
config.authHandler.close();
for(AuthHandler h : config.authHandler) h.close();
} catch (IOException e) {
LogHelper.error(e);
}
@ -483,7 +484,7 @@ private void generateConfigIfNotExists() throws IOException {
LogHelper.info("Creating LaunchServer config");
Config newConfig;
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

View file

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

View file

@ -6,14 +6,17 @@
import java.util.concurrent.ConcurrentHashMap;
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.launcher.serialize.config.ConfigObject;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
import ru.gravit.launchserver.auth.AuthException;
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 LaunchServer server;
@LauncherAPI
public static AuthProviderResult authError(String message) throws AuthException {
@ -21,15 +24,15 @@ public static AuthProviderResult authError(String message) throws AuthException
}
@LauncherAPI
public static AuthProvider newProvider(String name, BlockConfigEntry block) {
public static AuthProvider newProvider(String name, BlockConfigEntry block,LaunchServer server) {
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));
return authHandlerAdapter.convert(block);
return authHandlerAdapter.convert(block,server);
}
@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"),
String.format("Auth provider has been already registered: '%s'", name));
}
@ -49,10 +52,15 @@ public static void registerProviders() {
registredProv = true;
}
}
public AuthHandler getAccociateHandler(int this_position)
{
return server.config.authHandler[this_position];
}
@LauncherAPI
protected AuthProvider(BlockConfigEntry block) {
protected AuthProvider(BlockConfigEntry block, LaunchServer launchServer) {
super(block);
server = launchServer;
}
@LauncherAPI
@ -60,4 +68,9 @@ protected AuthProvider(BlockConfigEntry block) {
@Override
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;
import ru.gravit.launcher.LauncherAPI;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.SecurityHelper.DigestAlgorithm;
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
@ -11,8 +12,8 @@ public abstract class DigestAuthProvider extends AuthProvider {
private final DigestAlgorithm digest;
@LauncherAPI
protected DigestAuthProvider(BlockConfigEntry block) {
super(block);
protected DigestAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server);
digest = DigestAlgorithm.byName(block.getEntryValue("digest", StringConfigEntry.class));
}

View file

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

View file

@ -7,6 +7,7 @@
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.HTTPRequest;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.SecurityHelper;
@ -25,8 +26,8 @@ public final class JsonAuthProvider extends AuthProvider {
private final String responseUserKeyName;
private final String responseErrorKeyName;
JsonAuthProvider(BlockConfigEntry block) {
super(block);
JsonAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server);
String configUrl = block.getEntryValue("url", StringConfigEntry.class);
userKeyName = VerifyHelper.verify(block.getEntryValue("userKeyName", StringConfigEntry.class),
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.WriterConfig;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.IOHelper;
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) {
super(block);
public MojangAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server);
}
@Override

View file

@ -5,6 +5,8 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import clojure.lang.IFn;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.SecurityHelper;
import ru.gravit.utils.helper.VerifyHelper;
@ -19,8 +21,8 @@ public final class MySQLAuthProvider extends AuthProvider {
private final String query;
private final String[] queryParams;
public MySQLAuthProvider(BlockConfigEntry block) {
super(block);
public MySQLAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server);
mySQLHolder = new MySQLSourceConfig("authProviderPool", block);
// Read query

View file

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

View file

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

View file

@ -5,6 +5,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.utils.helper.CommonHelper;
import ru.gravit.utils.helper.IOHelper;
import ru.gravit.utils.helper.SecurityHelper;
@ -15,8 +16,8 @@ public final class RequestAuthProvider extends AuthProvider {
private final String url;
private final Pattern response;
public RequestAuthProvider(BlockConfigEntry block) {
super(block);
public RequestAuthProvider(BlockConfigEntry block, LaunchServer server) {
super(block,server);
url = block.getEntryValue("url", StringConfigEntry.class);
response = Pattern.compile(block.getEntryValue("response", StringConfigEntry.class));

View file

@ -2,6 +2,7 @@
import java.util.UUID;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.utils.helper.LogHelper;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
@ -27,10 +28,13 @@ public void invoke(String... args) throws Exception {
verifyArgs(args, 2);
String login = args[0];
String password = args[1];
int auth_id = 0;
if(args.length >= 3) auth_id = Integer.valueOf(args[3]);
// Authenticate
AuthProviderResult result = server.config.authProvider.auth(login, password, "127.0.0.1");
UUID uuid = server.config.authHandler.auth(result);
AuthProvider provider = server.config.authProvider[auth_id];
AuthProviderResult result = provider.auth(login, password, "127.0.0.1");
UUID uuid = provider.getAccociateHandler(auth_id).auth(result);
// Print auth successful message
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]);
// Get UUID by username
String username = server.config.authHandler.uuidToUsername(uuid);
String username = server.config.authHandler[0].uuidToUsername(uuid);
if (username == null)
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]);
// Get UUID by username
UUID uuid = server.config.authHandler.usernameToUUID(username);
UUID uuid = server.config.authHandler[0].usernameToUUID(username);
if (uuid == null)
throw new CommandException(String.format("Unknown username: '%s'", username));

View file

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

View file

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

View file

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

View file

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

View file

@ -13,7 +13,7 @@
public final class ProfileByUsernameResponse extends Response {
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) {
output.writeBoolean(false);
return;