Authkey is now stored in .config file to allow automatic logging in

This commit is contained in:
CrushedPixel
2015-05-05 22:56:25 +02:00
parent f49a180b3e
commit ccaa93116d
7 changed files with 56 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import eu.crushedpixel.replaymod.api.client.ApiClient;
import eu.crushedpixel.replaymod.chat.ChatMessageHandler;
import eu.crushedpixel.replaymod.events.*;
import eu.crushedpixel.replaymod.localization.LocalizedResourcePack;
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
import eu.crushedpixel.replaymod.recording.ConnectionEventHandler;
import eu.crushedpixel.replaymod.reflection.MCPNames;
import eu.crushedpixel.replaymod.registry.KeybindRegistry;
@@ -83,6 +84,7 @@ public class ReplayMod {
public void preInit(FMLPreInitializationEvent event) {
config = new Configuration(event.getSuggestedConfigurationFile());
config.load();
AuthenticationHandler.loadAuthkeyFromConfig();
uploadedFileHandler = new UploadedFileHandler(event.getModConfigurationDirectory());

View File

@@ -25,6 +25,7 @@ public class ApiClient {
QueryBuilder builder = new QueryBuilder(ApiMethods.login);
builder.put("user", username);
builder.put("pw", password);
builder.put("mod", true);
AuthKey auth = invokeAndReturn(builder, AuthKey.class);
return auth;
}
@@ -38,9 +39,21 @@ public class ApiClient {
return auth;
}
public boolean checkAuthkey(String auth) {
try {
QueryBuilder builder = new QueryBuilder(ApiMethods.check_authkey);
builder.put("auth", auth);
Success succ = invokeAndReturn(builder, Success.class);
return succ.isSuccess();
} catch(Exception e) {
return false;
}
}
public boolean logout(String auth) throws IOException, ApiException {
QueryBuilder builder = new QueryBuilder(ApiMethods.logout);
builder.put("auth", auth);
builder.put("mod", true);
Success succ = invokeAndReturn(builder, Success.class);
return succ.isSuccess();
}

View File

@@ -3,6 +3,7 @@ package eu.crushedpixel.replaymod.api.client;
public class ApiMethods {
public static final String register = "register";
public static final String check_authkey = "check_authkey";
public static final String login = "login";
public static final String logout = "logout";
public static final String replay_files = "replay_files";

View File

@@ -31,6 +31,6 @@ public class ApiError {
}
public String getTranslatedDesc() {
return I18n.format(key, objects);
return I18n.format(key, (Object[])objects);
}
}

View File

@@ -65,8 +65,10 @@ public class GuiEventHandler {
if(event.gui instanceof GuiMainMenu) {
if(ReplayMod.firstMainMenu) {
ReplayMod.firstMainMenu = false;
event.gui = new GuiLoginPrompt(event.gui, event.gui);
return;
if(!AuthenticationHandler.isAuthenticated()) {
event.gui = new GuiLoginPrompt(event.gui, event.gui);
return;
}
} else {
try {
MCTimerHandler.setTimerSpeed(1);

View File

@@ -4,6 +4,7 @@ import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.client.ApiClient;
import eu.crushedpixel.replaymod.api.client.ApiException;
import eu.crushedpixel.replaymod.api.client.holders.AuthKey;
import net.minecraftforge.common.config.Property;
import java.io.IOException;
@@ -32,12 +33,33 @@ public class AuthenticationHandler {
public static void register(String username, String mail, String password) throws IOException, ApiException {
AuthKey auth = apiClient.register(username, mail, password);
authkey = auth.getAuthkey();
saveAuthkey(authkey);
}
public static boolean loadAuthkeyFromConfig() {
Property p = ReplayMod.instance.config.get("authkey", "authkey", "null");
String key = null;
if(!(p.getString().equals("null"))) {
key = p.getString();
}
if(key != null) {
boolean succ = apiClient.checkAuthkey(key);
if(succ) {
authkey = key;
} else {
saveAuthkey("null");
}
return succ;
}
return false;
}
public static int authenticate(String username, String password) {
try {
authkey = ReplayMod.apiClient.getLogin(username, password).getAuthkey();
saveAuthkey(authkey);
return SUCCESS;
} catch(ApiException e) {
return INVALID;
@@ -50,6 +72,7 @@ public class AuthenticationHandler {
try {
ReplayMod.apiClient.logout(authkey);
authkey = null;
saveAuthkey("null");
return SUCCESS;
} catch(ApiException e) {
return INVALID;
@@ -57,4 +80,10 @@ public class AuthenticationHandler {
return NO_CONNECTION;
}
}
private static void saveAuthkey(String authkey) {
ReplayMod.instance.config.removeCategory(ReplayMod.config.getCategory("authkey"));
ReplayMod.instance.config.get("authkey", "authkey", authkey);
ReplayMod.instance.config.save();
}
}

View File

@@ -18,8 +18,12 @@ public class ReplaySettings {
return enums;
}
private static final String[] CATEGORIES = new String[]{"recording", "replay", "render", "advanced"};
public void readValues() {
Configuration config = ReplayMod.config;
Configuration config = ReplayMod.instance.config;
config.load();
for(RecordingOptions o : RecordingOptions.values()) {
Property p = getConfigSetting(config, o.name(), o.getValue(), "recording", false);
@@ -146,7 +150,7 @@ public class ReplaySettings {
public void rewriteSettings() {
ReplayMod.instance.config.load();
for(String cat : ReplayMod.instance.config.getCategoryNames()) {
for(String cat : CATEGORIES) {
ReplayMod.instance.config.removeCategory(ReplayMod.instance.config.getCategory(cat));
}