[FEATURE] [CRITICAL] AuthProviderPair

This commit is contained in:
Gravit 2019-03-22 11:04:42 +07:00
parent a9cc2841ac
commit c9c867d8f3
25 changed files with 177 additions and 142 deletions

View file

@ -9,6 +9,7 @@
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launcher.serialize.signed.SignedObjectHolder; import ru.gravit.launcher.serialize.signed.SignedObjectHolder;
import ru.gravit.launchserver.auth.AuthLimiter; import ru.gravit.launchserver.auth.AuthLimiter;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.handler.AuthHandler; import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.auth.handler.MemoryAuthHandler; import ru.gravit.launchserver.auth.handler.MemoryAuthHandler;
import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler; import ru.gravit.launchserver.auth.hwid.AcceptHWIDHandler;
@ -46,8 +47,6 @@
import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey; import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import java.time.Duration;
import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -63,10 +62,9 @@ public void reload() throws Exception {
} }
config.verify(); config.verify();
Launcher.applyLauncherEnv(config.env); Launcher.applyLauncherEnv(config.env);
for (AuthProvider provider : config.authProvider) { for (AuthProviderPair auth : config.auth) {
provider.init(); auth.init();
} }
config.authHandler.init();
} }
public static final class Config { public static final class Config {
@ -86,9 +84,32 @@ public static final class Config {
// Handlers & Providers // Handlers & Providers
public AuthProvider[] authProvider; public AuthProviderPair[] auth;
public AuthHandler authHandler; private transient AuthProviderPair authDefault;
public AuthProviderPair getAuthProviderPair(String name)
{
for(AuthProviderPair pair : auth)
{
if(pair.name.equals(name)) return pair;
}
return null;
}
public AuthProviderPair getAuthProviderPair()
{
if(authDefault != null) return authDefault;
for(AuthProviderPair pair : auth)
{
if(pair.isDefault)
{
authDefault = pair;
return pair;
}
}
return null;
}
public PermissionsHandler permissionsHandler; public PermissionsHandler permissionsHandler;
@ -165,11 +186,21 @@ public void setAddress(String address) {
public void verify() { public void verify() {
VerifyHelper.verify(getAddress(), VerifyHelper.NOT_EMPTY, "LaunchServer address can't be empty"); VerifyHelper.verify(getAddress(), VerifyHelper.NOT_EMPTY, "LaunchServer address can't be empty");
if (authHandler == null) { if (auth == null || auth[0] == null) {
throw new NullPointerException("AuthHandler must not be null"); throw new NullPointerException("AuthHandler must not be null");
} }
if (authProvider == null || authProvider[0] == null) { boolean isOneDefault = false;
throw new NullPointerException("AuthProvider must not be null"); for(AuthProviderPair pair : auth)
{
if(pair.isDefault)
{
isOneDefault = true;
break;
}
}
if(!isOneDefault)
{
throw new IllegalStateException("No auth pairs declared by default.");
} }
if (textureProvider == null) { if (textureProvider == null) {
throw new NullPointerException("TextureProvider must not be null"); throw new NullPointerException("TextureProvider must not be null");
@ -188,12 +219,7 @@ public void verify() {
public void close() public void close()
{ {
try { try {
authHandler.close(); for (AuthProviderPair p : auth) p.close();
} catch (IOException e) {
LogHelper.error(e);
}
try {
for (AuthProvider p : authProvider) p.close();
} catch (IOException e) { } catch (IOException e) {
LogHelper.error(e); LogHelper.error(e);
} }
@ -443,10 +469,9 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE
} }
config.verify(); config.verify();
Launcher.applyLauncherEnv(config.env); Launcher.applyLauncherEnv(config.env);
for (AuthProvider provider : config.authProvider) { for (AuthProviderPair provider : config.auth) {
provider.init(); provider.init();
} }
config.authHandler.init();
// build hooks, anti-brutforce and other // build hooks, anti-brutforce and other
buildHookManager = new BuildHookManager(); buildHookManager = new BuildHookManager();
@ -463,12 +488,12 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE
reloadManager.registerReloadable("launchServer", this); reloadManager.registerReloadable("launchServer", this);
if (config.permissionsHandler instanceof Reloadable) if (config.permissionsHandler instanceof Reloadable)
reloadManager.registerReloadable("permissionsHandler", (Reloadable) config.permissionsHandler); reloadManager.registerReloadable("permissionsHandler", (Reloadable) config.permissionsHandler);
if (config.authHandler instanceof Reloadable) for (int i = 0; i < config.auth.length; ++i) {
reloadManager.registerReloadable("authHandler", (Reloadable) config.authHandler); AuthProviderPair pair = config.auth[i];
for (int i = 0; i < config.authProvider.length; ++i) { if (pair.provider instanceof Reloadable)
AuthProvider provider = config.authProvider[i]; reloadManager.registerReloadable("auth.".concat(pair.name).concat(".provider"), (Reloadable) pair.provider);
if (provider instanceof Reloadable) if (pair.handler instanceof Reloadable)
reloadManager.registerReloadable("authHandler".concat(String.valueOf(i)), (Reloadable) provider); reloadManager.registerReloadable("auth.".concat(pair.name).concat(".handler"), (Reloadable) pair.handler);
} }
if (config.textureProvider instanceof Reloadable) if (config.textureProvider instanceof Reloadable)
reloadManager.registerReloadable("textureProvider", (Reloadable) config.textureProvider); reloadManager.registerReloadable("textureProvider", (Reloadable) config.textureProvider);
@ -477,12 +502,12 @@ public LaunchServer(Path dir, String[] args) throws IOException, InvalidKeySpecE
if (config.permissionsHandler instanceof Reconfigurable) if (config.permissionsHandler instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("permissionsHandler", (Reconfigurable) config.permissionsHandler); reconfigurableManager.registerReconfigurable("permissionsHandler", (Reconfigurable) config.permissionsHandler);
if (config.authHandler instanceof Reconfigurable) for (int i = 0; i < config.auth.length; ++i) {
reconfigurableManager.registerReconfigurable("authHandler", (Reconfigurable) config.authHandler); AuthProviderPair pair = config.auth[i];
for (int i = 0; i < config.authProvider.length; ++i) { if (pair.provider instanceof Reconfigurable)
AuthProvider provider = config.authProvider[i]; reconfigurableManager.registerReconfigurable("auth.".concat(pair.name).concat(".provider"), (Reconfigurable) pair.provider);
if (provider instanceof Reconfigurable) if (pair.handler instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("authHandler".concat(String.valueOf(i)), (Reconfigurable) provider); reconfigurableManager.registerReconfigurable("auth.".concat(pair.name).concat(".handler"), (Reconfigurable) pair.handler);
} }
if (config.textureProvider instanceof Reconfigurable) if (config.textureProvider instanceof Reconfigurable)
reconfigurableManager.registerReconfigurable("textureProvider", (Reconfigurable) config.textureProvider); reconfigurableManager.registerReconfigurable("textureProvider", (Reconfigurable) config.textureProvider);
@ -591,10 +616,10 @@ private void generateConfigIfNotExists() throws IOException {
newConfig.launch4j.productVer = newConfig.launch4j.fileVer; newConfig.launch4j.productVer = newConfig.launch4j.fileVer;
newConfig.env = LauncherConfig.LauncherEnvironment.STD; newConfig.env = LauncherConfig.LauncherEnvironment.STD;
newConfig.startScript = JVMHelper.OS_TYPE.equals(JVMHelper.OS.MUSTDIE) ? "." + File.separator + "start.bat" : "." + File.separator + "start.sh"; newConfig.startScript = JVMHelper.OS_TYPE.equals(JVMHelper.OS.MUSTDIE) ? "." + File.separator + "start.bat" : "." + File.separator + "start.sh";
newConfig.authHandler = new MemoryAuthHandler();
newConfig.hwidHandler = new AcceptHWIDHandler(); newConfig.hwidHandler = new AcceptHWIDHandler();
newConfig.auth = new AuthProviderPair[]{ new AuthProviderPair() };
newConfig.authProvider = new AuthProvider[]{new RejectAuthProvider("Настройте authProvider")}; newConfig.auth[0].provider = new RejectAuthProvider("Настройте authProvider");
newConfig.auth[0].handler = new MemoryAuthHandler();
newConfig.textureProvider = new RequestTextureProvider("http://example.com/skins/%username%.png", "http://example.com/cloaks/%username%.png"); newConfig.textureProvider = new RequestTextureProvider("http://example.com/skins/%username%.png", "http://example.com/cloaks/%username%.png");
newConfig.permissionsHandler = new JsonFilePermissionsHandler(); newConfig.permissionsHandler = new JsonFilePermissionsHandler();
newConfig.port = 7240; newConfig.port = 7240;

View file

@ -0,0 +1,24 @@
package ru.gravit.launchserver.auth;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import java.io.IOException;
public class AuthProviderPair {
public AuthProvider provider;
public AuthHandler handler;
public String name;
public boolean isDefault;
public void init()
{
provider.init();
handler.init();
}
public void close() throws IOException {
provider.close();
handler.close();
}
}

View file

@ -36,10 +36,6 @@ public static void registerProviders() {
} }
} }
public AuthHandler getAccociateHandler(int this_position) {
return LaunchServer.server.config.authHandler;
}
public abstract AuthProviderResult auth(String login, String password, String ip) throws Exception; public abstract AuthProviderResult auth(String login, String password, String ip) throws Exception;

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.command.auth; package ru.gravit.launchserver.command.auth;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
import ru.gravit.launchserver.command.Command; import ru.gravit.launchserver.command.Command;
@ -15,7 +16,7 @@ public AuthCommand(LaunchServer server) {
@Override @Override
public String getArgsDescription() { public String getArgsDescription() {
return "<login> <password>"; return "<login> <password> <auth_id>";
} }
@Override @Override
@ -26,15 +27,20 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
verifyArgs(args, 2); verifyArgs(args, 2);
AuthProviderPair pair;
if(args.length > 2) pair = server.config.getAuthProviderPair(args[2]);
else pair = server.config.getAuthProviderPair();
if(pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[1]));
String login = args[0]; String login = args[0];
String password = args[1]; String password = args[1];
int auth_id = 0; int auth_id = 0;
if (args.length >= 3) auth_id = Integer.valueOf(args[3]); if (args.length >= 3) auth_id = Integer.valueOf(args[3]);
// Authenticate // Authenticate
AuthProvider provider = server.config.authProvider[auth_id]; AuthProvider provider = pair.provider;
AuthProviderResult result = provider.auth(login, password, "127.0.0.1"); AuthProviderResult result = provider.auth(login, password, "127.0.0.1");
UUID uuid = provider.getAccociateHandler(auth_id).auth(result); UUID uuid = pair.handler.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

@ -1,6 +1,7 @@
package ru.gravit.launchserver.command.auth; package ru.gravit.launchserver.command.auth;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.command.Command; import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.command.CommandException; import ru.gravit.utils.command.CommandException;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -15,7 +16,7 @@ public UUIDToUsernameCommand(LaunchServer server) {
@Override @Override
public String getArgsDescription() { public String getArgsDescription() {
return "<uuid>"; return "<uuid> <auth_id>";
} }
@Override @Override
@ -26,10 +27,15 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws CommandException, IOException { public void invoke(String... args) throws CommandException, IOException {
verifyArgs(args, 1); verifyArgs(args, 1);
AuthProviderPair pair;
if(args.length > 1) pair = server.config.getAuthProviderPair(args[1]);
else pair = server.config.getAuthProviderPair();
if(pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[1]));
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 = pair.handler.uuidToUsername(uuid);
if (username == null) if (username == null)
throw new CommandException("Unknown UUID: " + uuid); throw new CommandException("Unknown UUID: " + uuid);

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.command.auth; package ru.gravit.launchserver.command.auth;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.command.Command; import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.command.CommandException; import ru.gravit.utils.command.CommandException;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
@ -15,7 +16,7 @@ public UsernameToUUIDCommand(LaunchServer server) {
@Override @Override
public String getArgsDescription() { public String getArgsDescription() {
return "<username>"; return "<username> <auth_id>";
} }
@Override @Override
@ -26,10 +27,14 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws CommandException, IOException { public void invoke(String... args) throws CommandException, IOException {
verifyArgs(args, 1); verifyArgs(args, 1);
AuthProviderPair pair;
if(args.length > 1) pair = server.config.getAuthProviderPair(args[1]);
else pair = server.config.getAuthProviderPair();
if(pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[1]));
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 = pair.handler.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

@ -1,6 +1,7 @@
package ru.gravit.launchserver.command.dump; package ru.gravit.launchserver.command.dump;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.handler.CachedAuthHandler; import ru.gravit.launchserver.auth.handler.CachedAuthHandler;
import ru.gravit.launchserver.command.Command; import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.IOHelper; import ru.gravit.utils.helper.IOHelper;
@ -19,7 +20,7 @@ public DumpEntryCacheCommand(LaunchServer server) {
@Override @Override
public String getArgsDescription() { public String getArgsDescription() {
return "[load/unload] [filename]"; return "[load/unload] [auth_id] [filename]";
} }
@Override @Override
@ -29,12 +30,14 @@ public String getUsageDescription() {
@Override @Override
public void invoke(String... args) throws Exception { public void invoke(String... args) throws Exception {
verifyArgs(args, 2); verifyArgs(args, 3);
if (!(server.config.authHandler instanceof CachedAuthHandler)) AuthProviderPair pair = server.config.getAuthProviderPair(args[1]);
if(pair == null) throw new IllegalStateException(String.format("Auth %s not found", args[1]));
if (!(pair.handler instanceof CachedAuthHandler))
throw new UnsupportedOperationException("This command used only CachedAuthHandler"); throw new UnsupportedOperationException("This command used only CachedAuthHandler");
CachedAuthHandler authHandler = (CachedAuthHandler) server.config.authHandler; CachedAuthHandler authHandler = (CachedAuthHandler) pair.handler;
if (args[0].equals("unload")) { if (args[0].equals("unload")) {
LogHelper.info("CachedAuthHandler write to %s", args[1]); LogHelper.info("CachedAuthHandler write to %s", args[2]);
Map<UUID, CachedAuthHandler.Entry> entryCache = authHandler.getEntryCache(); Map<UUID, CachedAuthHandler.Entry> entryCache = authHandler.getEntryCache();
Map<String, UUID> usernamesCache = authHandler.getUsernamesCache(); Map<String, UUID> usernamesCache = authHandler.getUsernamesCache();
EntryAndUsername serializable = new EntryAndUsername(); EntryAndUsername serializable = new EntryAndUsername();

View file

@ -72,7 +72,6 @@ public static void registerCommands(ru.gravit.utils.command.CommandHandler handl
handler.registerCommand("config", new ConfigCommand(server)); handler.registerCommand("config", new ConfigCommand(server));
handler.registerCommand("configHelp", new ConfigHelpCommand(server)); handler.registerCommand("configHelp", new ConfigHelpCommand(server));
handler.registerCommand("configList", new ConfigListCommand(server)); handler.registerCommand("configList", new ConfigListCommand(server));
handler.registerCommand("swapAuthProvider", new SwapAuthProviderCommand(server));
handler.registerCommand("serverStatus", new ServerStatusCommand(server)); handler.registerCommand("serverStatus", new ServerStatusCommand(server));
handler.registerCommand("checkInstall", new CheckInstallCommand(server)); handler.registerCommand("checkInstall", new CheckInstallCommand(server));
handler.registerCommand("multi", new MultiCommand(server)); handler.registerCommand("multi", new MultiCommand(server));

View file

@ -1,6 +1,7 @@
package ru.gravit.launchserver.command.service; package ru.gravit.launchserver.command.service;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.handler.CachedAuthHandler; import ru.gravit.launchserver.auth.handler.CachedAuthHandler;
import ru.gravit.launchserver.command.Command; import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.JVMHelper; import ru.gravit.utils.helper.JVMHelper;
@ -33,8 +34,12 @@ public void invoke(String... args) {
LogHelper.info("Uptime: %d days %d hours %d minutes %d seconds", days, hour, min, second); LogHelper.info("Uptime: %d days %d hours %d minutes %d seconds", days, hour, min, second);
LogHelper.info("Uptime (double): %f", (double) JVMHelper.RUNTIME_MXBEAN.getUptime() / 1000); LogHelper.info("Uptime (double): %f", (double) JVMHelper.RUNTIME_MXBEAN.getUptime() / 1000);
LogHelper.info("Sessions: %d | Modules: %d | Commands: %d", server.sessionManager.getSessions().size(), server.modulesManager.modules.size(), server.commandHandler.commandsMap().size()); LogHelper.info("Sessions: %d | Modules: %d | Commands: %d", server.sessionManager.getSessions().size(), server.modulesManager.modules.size(), server.commandHandler.commandsMap().size());
if (server.config.authHandler instanceof CachedAuthHandler) { for(AuthProviderPair pair : server.config.auth)
LogHelper.info("AuthHandler: EntryCache: %d | usernameCache: %d", ((CachedAuthHandler) server.config.authHandler).getEntryCache().size(), ((CachedAuthHandler) server.config.authHandler).getUsernamesCache().size()); {
if (pair.handler instanceof CachedAuthHandler) {
LogHelper.info("AuthHandler %s: EntryCache: %d | usernameCache: %d", pair.name, ((CachedAuthHandler) pair.handler).getEntryCache().size(), ((CachedAuthHandler) pair.handler).getUsernamesCache().size());
}
} }
} }
} }

View file

@ -1,63 +0,0 @@
package ru.gravit.launchserver.command.service;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.provider.AcceptAuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.RejectAuthProvider;
import ru.gravit.launchserver.command.Command;
import ru.gravit.utils.helper.LogHelper;
public class SwapAuthProviderCommand extends Command {
public AuthProvider[] providersCache;
public SwapAuthProviderCommand(LaunchServer server) {
super(server);
}
@Override
public String getArgsDescription() {
return "[index] [accept/reject/undo] [message(for reject)]";
}
@Override
public String getUsageDescription() {
return "Change authProvider";
}
@SuppressWarnings("resource")
@Override
public void invoke(String... args) throws Exception {
verifyArgs(args, 2);
if (providersCache == null) providersCache = new AuthProvider[server.config.authProvider.length];
int index = Integer.valueOf(args[0]);
switch (args[1]) {
case "accept":
if (providersCache[index] == null) {
AcceptAuthProvider provider = new AcceptAuthProvider();
providersCache[index] = server.config.authProvider[index];
server.config.authProvider[index] = provider;
LogHelper.info("AuthProvider[%d] is AcceptAuthProvider", index);
} else LogHelper.error("Changes detected. Use undo");
break;
case "reject":
if (providersCache[index] == null) {
RejectAuthProvider rejectAuthProvider;
if (args.length < 3) rejectAuthProvider = new RejectAuthProvider();
else rejectAuthProvider = new RejectAuthProvider(args[2]);
providersCache[index] = server.config.authProvider[index];
server.config.authProvider[index] = rejectAuthProvider;
LogHelper.info("AuthProvider[%d] is RejectAuthProvider", index);
} else LogHelper.error("Changes detected. Use undo");
break;
case "undo":
if (providersCache[index] == null) LogHelper.error("Cache clean. Undo impossible");
else {
server.config.authProvider[index].close();
server.config.authProvider[index] = providersCache[index];
providersCache[index] = null;
}
break;
}
}
}

View file

@ -7,6 +7,7 @@
import ru.gravit.launcher.serialize.SerializeLimits; import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.hwid.HWIDException;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
@ -62,9 +63,8 @@ public void reply() throws Exception {
String client = null; String client = null;
if (isClient) if (isClient)
client = input.readString(SerializeLimits.MAX_CLIENT); client = input.readString(SerializeLimits.MAX_CLIENT);
int auth_id = input.readInt(); String auth_id = input.readString(SerializeLimits.MAX_QUEUE_SIZE);
String hwid_str = input.readString(SerializeLimits.MAX_HWID_STR); String hwid_str = input.readString(SerializeLimits.MAX_HWID_STR);
if (auth_id + 1 > server.config.authProvider.length || auth_id < 0) auth_id = 0;
byte[] encryptedPassword = input.readByteArray(SecurityHelper.CRYPTO_MAX_LENGTH); byte[] encryptedPassword = input.readByteArray(SecurityHelper.CRYPTO_MAX_LENGTH);
String customText = input.readString(SerializeLimits.MAX_CUSTOM_TEXT); String customText = input.readString(SerializeLimits.MAX_CUSTOM_TEXT);
// Decrypt password // Decrypt password
@ -80,7 +80,11 @@ 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]; AuthProviderPair pair;
if(auth_id.isEmpty()) pair = server.config.getAuthProviderPair();
else pair = server.config.getAuthProviderPair(auth_id);
if(pair == null) requestError("Auth type not found");
AuthProvider provider = pair.provider;
clientData.type = Client.Type.USER; clientData.type = Client.Type.USER;
AuthContext context = new AuthContext(session, login, password.length(), customText, client, hwid_str, false); AuthContext context = new AuthContext(session, login, password.length(), customText, client, hwid_str, false);
try { try {
@ -127,10 +131,12 @@ public void reply() throws Exception {
clientData.isAuth = true; clientData.isAuth = true;
clientData.permissions = result.permissions; clientData.permissions = result.permissions;
clientData.username = result.username; clientData.username = result.username;
clientData.auth_id = auth_id;
clientData.updateAuth();
// 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 = pair.handler.auth(result);
} catch (AuthException e) { } catch (AuthException e) {
requestError(e.getMessage()); requestError(e.getMessage());
return; return;

View file

@ -6,6 +6,7 @@
import ru.gravit.launcher.serialize.SerializeLimits; import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.hwid.HWIDException;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
@ -36,8 +37,7 @@ public AuthServerResponse(LaunchServer server, long session, HInput input, HOutp
public void reply() throws Exception { public void reply() throws Exception {
String login = input.readString(SerializeLimits.MAX_LOGIN); String login = input.readString(SerializeLimits.MAX_LOGIN);
String client = input.readString(SerializeLimits.MAX_CLIENT); String client = input.readString(SerializeLimits.MAX_CLIENT);
int auth_id = input.readInt(); String auth_id = input.readString(SerializeLimits.MAX_QUEUE_SIZE);
if (auth_id + 1 > server.config.authProvider.length || auth_id < 0) auth_id = 0;
byte[] encryptedPassword = input.readByteArray(SecurityHelper.CRYPTO_MAX_LENGTH); byte[] encryptedPassword = input.readByteArray(SecurityHelper.CRYPTO_MAX_LENGTH);
// Decrypt password // Decrypt password
String password; String password;
@ -51,7 +51,11 @@ public void reply() throws Exception {
// Authenticate // Authenticate
debug("ServerLogin: '%s', Password: '%s'", login, echo(password.length())); debug("ServerLogin: '%s', Password: '%s'", login, echo(password.length()));
AuthProviderResult result; AuthProviderResult result;
AuthProvider provider = server.config.authProvider[auth_id]; AuthProviderPair pair;
if(auth_id.isEmpty()) pair = server.config.getAuthProviderPair();
else pair = server.config.getAuthProviderPair(auth_id);
if(pair == null) requestError("Auth type not found");
AuthProvider provider = pair.provider;
try { try {
if (server.limiter.isLimit(ip)) { if (server.limiter.isLimit(ip)) {
AuthProvider.authError(server.config.authRejectString); AuthProvider.authError(server.config.authRejectString);
@ -87,6 +91,8 @@ public void reply() throws Exception {
} }
debug("ServerAuth: '%s' -> '%s', '%s'", login, result.username, result.accessToken); debug("ServerAuth: '%s' -> '%s', '%s'", login, result.username, result.accessToken);
clientData.isAuth = true; clientData.isAuth = true;
clientData.auth_id = auth_id;
clientData.updateAuth();
writeNoError(output); writeNoError(output);
clientData.permissions.write(output); clientData.permissions.write(output);
} }

View file

@ -34,7 +34,7 @@ public void reply() throws IOException {
UUID uuid; UUID uuid;
try { try {
server.authHookManager.checkServerHook(username, serverID); server.authHookManager.checkServerHook(username, serverID);
uuid = server.config.authHandler.checkServer(username, serverID); uuid = clientData.auth.handler.checkServer(username, serverID);
} catch (AuthException e) { } catch (AuthException e) {
requestError(e.getMessage()); requestError(e.getMessage());
return; return;

View file

@ -33,7 +33,7 @@ public void reply() throws IOException {
boolean success; boolean success;
try { try {
server.authHookManager.joinServerHook(username, accessToken, serverID); server.authHookManager.joinServerHook(username, accessToken, serverID);
success = server.config.authHandler.joinServer(username, accessToken, serverID); success = clientData.auth.handler.joinServer(username, accessToken, serverID);
} catch (AuthException e) { } catch (AuthException e) {
requestError(e.getMessage()); requestError(e.getMessage());
return; return;

View file

@ -30,6 +30,6 @@ public void reply() throws IOException {
// Respond with profiles array // Respond with profiles array
for (int i = 0; i < usernames.length; i++) for (int i = 0; i < usernames.length; i++)
ProfileByUsernameResponse.writeProfile(server, output, usernames[i], clients[i]); ProfileByUsernameResponse.writeProfile(server, output, usernames[i], clients[i], clientData.auth.handler);
} }
} }

View file

@ -48,7 +48,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 = clientData.auth.handler.uuidToUsername(uuid);
if (username == null) { if (username == null) {
output.writeBoolean(false); output.writeBoolean(false);
return; return;

View file

@ -4,6 +4,7 @@
import ru.gravit.launcher.serialize.HOutput; import ru.gravit.launcher.serialize.HOutput;
import ru.gravit.launcher.serialize.SerializeLimits; import ru.gravit.launcher.serialize.SerializeLimits;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.handler.AuthHandler;
import ru.gravit.launchserver.response.Response; import ru.gravit.launchserver.response.Response;
import ru.gravit.launchserver.socket.Client; import ru.gravit.launchserver.socket.Client;
import ru.gravit.utils.helper.VerifyHelper; import ru.gravit.utils.helper.VerifyHelper;
@ -13,8 +14,8 @@
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, AuthHandler handler) throws IOException {
UUID uuid = server.config.authHandler.usernameToUUID(username); UUID uuid = handler.usernameToUUID(username);
if (uuid == null) { if (uuid == null) {
output.writeBoolean(false); output.writeBoolean(false);
return; return;
@ -35,6 +36,6 @@ public void reply() throws IOException {
debug("Username: " + username); debug("Username: " + username);
String client = input.readString(SerializeLimits.MAX_CLIENT); String client = input.readString(SerializeLimits.MAX_CLIENT);
// Write response // Write response
writeProfile(server, output, username, client); writeProfile(server, output, username, client, clientData.auth.handler);
} }
} }

View file

@ -2,11 +2,13 @@
import ru.gravit.launcher.ClientPermissions; import ru.gravit.launcher.ClientPermissions;
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.LogHelper;
public class Client { public class Client {
public long session; public long session;
public String auth_id;
public long timestamp; public long timestamp;
public Type type; public Type type;
public ClientProfile profile; public ClientProfile profile;
@ -16,6 +18,8 @@ public class Client {
public String username; public String username;
public LogHelper.OutputEnity logOutput; public LogHelper.OutputEnity logOutput;
public transient AuthProviderPair auth;
public Client(long session) { public Client(long session) {
this.session = session; this.session = session;
timestamp = System.currentTimeMillis(); timestamp = System.currentTimeMillis();
@ -26,10 +30,16 @@ public Client(long session) {
checkSign = false; checkSign = false;
} }
//Данные ваторизации //Данные авторизации
public void up() { public void up() {
timestamp = System.currentTimeMillis(); timestamp = System.currentTimeMillis();
} }
public void updateAuth()
{
if(!isAuth) return;
if(auth_id.isEmpty()) auth = LaunchServer.server.config.getAuthProviderPair();
else auth = LaunchServer.server.config.getAuthProviderPair(auth_id);
}
public enum Type { public enum Type {
SERVER, SERVER,

View file

@ -7,6 +7,7 @@
import ru.gravit.launcher.profiles.ClientProfile; import ru.gravit.launcher.profiles.ClientProfile;
import ru.gravit.launchserver.LaunchServer; import ru.gravit.launchserver.LaunchServer;
import ru.gravit.launchserver.auth.AuthException; import ru.gravit.launchserver.auth.AuthException;
import ru.gravit.launchserver.auth.AuthProviderPair;
import ru.gravit.launchserver.auth.hwid.HWIDException; import ru.gravit.launchserver.auth.hwid.HWIDException;
import ru.gravit.launchserver.auth.provider.AuthProvider; import ru.gravit.launchserver.auth.provider.AuthProvider;
import ru.gravit.launchserver.auth.provider.AuthProviderResult; import ru.gravit.launchserver.auth.provider.AuthProviderResult;
@ -31,14 +32,14 @@ public class AuthResponse implements JsonResponseInterface {
public String password; public String password;
public byte[] encryptedPassword; public byte[] encryptedPassword;
public AuthResponse(String login, String password, int authid, OshiHWID hwid) { public AuthResponse(String login, String password, String auth_id, OshiHWID hwid) {
this.login = login; this.login = login;
this.password = password; this.password = password;
this.authid = authid; this.auth_id = auth_id;
this.hwid = hwid; this.hwid = hwid;
} }
public int authid; public String auth_id;
public ConnectTypes authType; public ConnectTypes authType;
public OshiHWID hwid; public OshiHWID hwid;
public enum ConnectTypes public enum ConnectTypes
@ -82,8 +83,11 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
{ {
AuthProvider.authError("authType: SERVER not allowed for this account"); AuthProvider.authError("authType: SERVER not allowed for this account");
} }
AuthProviderPair pair;
if(auth_id.isEmpty()) pair = LaunchServer.server.config.getAuthProviderPair();
else pair = LaunchServer.server.config.getAuthProviderPair(auth_id);
ru.gravit.launchserver.response.auth.AuthResponse.AuthContext context = new ru.gravit.launchserver.response.auth.AuthResponse.AuthContext(0, login, password.length(),customText, client, null, false); ru.gravit.launchserver.response.auth.AuthResponse.AuthContext context = new ru.gravit.launchserver.response.auth.AuthResponse.AuthContext(0, login, password.length(),customText, client, null, false);
AuthProvider provider = LaunchServer.server.config.authProvider[authid]; AuthProvider provider = pair.provider;
LaunchServer.server.authHookManager.preHook(context, clientData); LaunchServer.server.authHookManager.preHook(context, clientData);
provider.preAuth(login,password,customText,ip); provider.preAuth(login,password,customText,ip);
AuthProviderResult aresult = provider.auth(login, password, ip); AuthProviderResult aresult = provider.auth(login, password, ip);
@ -103,12 +107,14 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
//if (clientData.profile == null) { //if (clientData.profile == null) {
// throw new AuthException("You profile not found"); // throw new AuthException("You profile not found");
//} //}
UUID uuid = LaunchServer.server.config.authHandler.auth(aresult); UUID uuid = pair.handler.auth(aresult);
if(authType == ConnectTypes.CLIENT) if(authType == ConnectTypes.CLIENT)
LaunchServer.server.config.hwidHandler.check(hwid, aresult.username); LaunchServer.server.config.hwidHandler.check(hwid, aresult.username);
LaunchServer.server.authHookManager.postHook(context, clientData); LaunchServer.server.authHookManager.postHook(context, clientData);
clientData.isAuth = true; clientData.isAuth = true;
clientData.permissions = aresult.permissions; clientData.permissions = aresult.permissions;
clientData.auth_id = auth_id;
clientData.updateAuth();
result.accessToken = aresult.accessToken; result.accessToken = aresult.accessToken;
result.permissions = clientData.permissions; result.permissions = clientData.permissions;
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,aresult.username,client); result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,aresult.username,client);

View file

@ -25,7 +25,7 @@ public String getType() {
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client pClient) { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client pClient) {
CheckServerRequestEvent result = new CheckServerRequestEvent(); CheckServerRequestEvent result = new CheckServerRequestEvent();
try { try {
result.uuid = LaunchServer.server.config.authHandler.checkServer(username, serverID); result.uuid = pClient.auth.handler.checkServer(username, serverID);
if(result.uuid != null) if(result.uuid != null)
result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,result.uuid,username,client); result.playerProfile = ProfileByUUIDResponse.getProfile(LaunchServer.server,result.uuid,username,client);
} catch (AuthException e) { } catch (AuthException e) {

View file

@ -24,7 +24,7 @@ public String getType() {
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) {
boolean success; boolean success;
try { try {
success = LaunchServer.server.config.authHandler.joinServer(username, accessToken, serverID); success = client.auth.handler.joinServer(username, accessToken, serverID);
} catch (AuthException e) { } catch (AuthException e) {
service.sendObject(ctx, new ErrorRequestEvent(e.getMessage())); service.sendObject(ctx, new ErrorRequestEvent(e.getMessage()));
return; return;

View file

@ -28,7 +28,7 @@ public void execute(WebSocketService service, ChannelHandlerContext ctx, Client
result.playerProfiles = new PlayerProfile[list.length]; result.playerProfiles = new PlayerProfile[list.length];
for(int i=0;i<list.length;++i) for(int i=0;i<list.length;++i)
{ {
UUID uuid = LaunchServer.server.config.authHandler.usernameToUUID(list[i].username); UUID uuid = client.auth.handler.usernameToUUID(list[i].username);
result.playerProfiles[i] = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,list[i].username,list[i].client); result.playerProfiles[i] = ProfileByUUIDResponse.getProfile(LaunchServer.server,uuid,list[i].username,list[i].client);
} }
service.sendObject(ctx, result); service.sendObject(ctx, result);

View file

@ -46,7 +46,7 @@ public String getType() {
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
String username = LaunchServer.server.config.authHandler.uuidToUsername(uuid); String username = client.auth.handler.uuidToUsername(uuid);
service.sendObject(ctx, new ProfileByUUIDRequestEvent(getProfile(LaunchServer.server,uuid,username,this.client))); service.sendObject(ctx, new ProfileByUUIDRequestEvent(getProfile(LaunchServer.server,uuid,username,this.client)));
} }
} }

View file

@ -21,7 +21,7 @@ public String getType() {
@Override @Override
public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception { public void execute(WebSocketService service, ChannelHandlerContext ctx, Client client) throws Exception {
UUID uuid = LaunchServer.server.config.authHandler.usernameToUUID(username); UUID uuid = client.auth.handler.usernameToUUID(username);
service.sendObject(ctx, new ProfileByUsernameRequestEvent(getProfile(LaunchServer.server,uuid,username,this.client))); service.sendObject(ctx, new ProfileByUsernameRequestEvent(getProfile(LaunchServer.server,uuid,username,this.client)));
} }
} }

@ -1 +1 @@
Subproject commit b7fbe50c08a3e274aadb93292fed6d05918647b3 Subproject commit cbcabd887ec4e671730a3eda6cba777683d6de78