Adapted authkey check to new website return values and prepared for future username display

This commit is contained in:
CrushedPixel
2015-05-15 02:38:06 +02:00
parent bf03e2e05b
commit 38b900a33b
4 changed files with 30 additions and 13 deletions

View File

@@ -39,14 +39,14 @@ public class ApiClient {
return auth; return auth;
} }
public boolean checkAuthkey(String auth) { public AuthConfirmation checkAuthkey(String auth) {
try { try {
QueryBuilder builder = new QueryBuilder(ApiMethods.check_authkey); QueryBuilder builder = new QueryBuilder(ApiMethods.check_authkey);
builder.put("auth", auth); builder.put("auth", auth);
Success succ = invokeAndReturn(builder, Success.class); AuthConfirmation conf = invokeAndReturn(builder, AuthConfirmation.class);
return succ.isSuccess(); return conf;
} catch(Exception e) { } catch(Exception e) {
return false; return null;
} }
} }

View File

@@ -0,0 +1,10 @@
package eu.crushedpixel.replaymod.api.client.holders;
public class AuthConfirmation {
private String username;
private int id;
public String getUsername() {
return username;
}
}

View File

@@ -3,6 +3,7 @@ package eu.crushedpixel.replaymod.online.authentication;
import eu.crushedpixel.replaymod.ReplayMod; import eu.crushedpixel.replaymod.ReplayMod;
import eu.crushedpixel.replaymod.api.client.ApiClient; import eu.crushedpixel.replaymod.api.client.ApiClient;
import eu.crushedpixel.replaymod.api.client.ApiException; import eu.crushedpixel.replaymod.api.client.ApiException;
import eu.crushedpixel.replaymod.api.client.holders.AuthConfirmation;
import eu.crushedpixel.replaymod.api.client.holders.AuthKey; import eu.crushedpixel.replaymod.api.client.holders.AuthKey;
import net.minecraftforge.common.config.Property; import net.minecraftforge.common.config.Property;
@@ -17,6 +18,7 @@ public class AuthenticationHandler {
private static final ApiClient apiClient = new ApiClient(); private static final ApiClient apiClient = new ApiClient();
private static String authkey = null; private static String authkey = null;
private static String username = null;
public static boolean isAuthenticated() { public static boolean isAuthenticated() {
return authkey != null; return authkey != null;
@@ -25,18 +27,20 @@ public class AuthenticationHandler {
public static String getKey() { public static String getKey() {
return authkey; return authkey;
} }
public static String getUsername() { return username; }
public static boolean hasDonated(String uuid) throws IOException, ApiException { public static boolean hasDonated(String uuid) throws IOException, ApiException {
return apiClient.hasDonated(uuid); return apiClient.hasDonated(uuid);
} }
public static void register(String username, String mail, String password) throws IOException, ApiException { public static void register(String usrname, String mail, String password) throws IOException, ApiException {
AuthKey auth = apiClient.register(username, mail, password); AuthKey auth = apiClient.register(usrname, mail, password);
username = usrname;
authkey = auth.getAuthkey(); authkey = auth.getAuthkey();
saveAuthkey(authkey); saveAuthkey(authkey);
} }
public static boolean loadAuthkeyFromConfig() { public static void loadAuthkeyFromConfig() {
Property p = ReplayMod.instance.config.get("authkey", "authkey", "null"); Property p = ReplayMod.instance.config.get("authkey", "authkey", "null");
String key = null; String key = null;
@@ -45,20 +49,21 @@ public class AuthenticationHandler {
} }
if(key != null) { if(key != null) {
boolean succ = apiClient.checkAuthkey(key); AuthConfirmation conf = apiClient.checkAuthkey(key);
if(succ) { if(conf != null) {
authkey = key; authkey = key;
username = conf.getUsername();
} else { } else {
saveAuthkey("null"); saveAuthkey("null");
username = null;
} }
return succ;
} }
return false;
} }
public static int authenticate(String username, String password) { public static int authenticate(String usrname, String password) {
try { try {
authkey = ReplayMod.apiClient.getLogin(username, password).getAuthkey(); authkey = ReplayMod.apiClient.getLogin(usrname, password).getAuthkey();
username = usrname;
saveAuthkey(authkey); saveAuthkey(authkey);
return SUCCESS; return SUCCESS;
} catch(ApiException e) { } catch(ApiException e) {
@@ -72,6 +77,7 @@ public class AuthenticationHandler {
try { try {
ReplayMod.apiClient.logout(authkey); ReplayMod.apiClient.logout(authkey);
authkey = null; authkey = null;
username = null;
saveAuthkey("null"); saveAuthkey("null");
return SUCCESS; return SUCCESS;
} catch(ApiException e) { } catch(ApiException e) {

View File

@@ -22,6 +22,7 @@ replaymod.api.zippingerror=An error occured while zipping the language files
replaymod.api.usernameexists=This username is already taken replaymod.api.usernameexists=This username is already taken
replaymod.api.mailexists=This Email address is already linked to an account replaymod.api.mailexists=This Email address is already linked to an account
replaymod.api.invalidmail=Invalid Email Address replaymod.api.invalidmail=Invalid Email Address
replaymod.api.nopermissions=You don't have permission to do this
#All of the chat messages #All of the chat messages