Rewrite authentication handling
Fix logout not working until restart
This commit is contained in:
@@ -10,11 +10,14 @@ import eu.crushedpixel.replaymod.gui.online.GuiReplayDownloading;
|
||||
import eu.crushedpixel.replaymod.gui.overlay.GuiReplayOverlay;
|
||||
import eu.crushedpixel.replaymod.holders.KeyframeSet;
|
||||
import eu.crushedpixel.replaymod.localization.LocalizedResourcePack;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import eu.crushedpixel.replaymod.online.authentication.ConfigurationAuthData;
|
||||
import eu.crushedpixel.replaymod.online.urischeme.UriScheme;
|
||||
import eu.crushedpixel.replaymod.recording.ConnectionEventHandler;
|
||||
import eu.crushedpixel.replaymod.registry.*;
|
||||
import eu.crushedpixel.replaymod.renderer.*;
|
||||
import eu.crushedpixel.replaymod.renderer.CustomObjectRenderer;
|
||||
import eu.crushedpixel.replaymod.renderer.InvisibilityRender;
|
||||
import eu.crushedpixel.replaymod.renderer.PathPreviewRenderer;
|
||||
import eu.crushedpixel.replaymod.renderer.SpectatorRenderer;
|
||||
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
||||
import eu.crushedpixel.replaymod.replay.ReplayProcess;
|
||||
import eu.crushedpixel.replaymod.replay.ReplaySender;
|
||||
@@ -83,7 +86,6 @@ public class ReplayMod {
|
||||
}
|
||||
|
||||
public static final String MODID = "replaymod";
|
||||
public static final ApiClient apiClient = new ApiClient();
|
||||
private static final Minecraft mc = Minecraft.getMinecraft();
|
||||
public static GuiEventHandler guiEventHandler;
|
||||
public static GuiReplayOverlay overlay;
|
||||
@@ -107,6 +109,7 @@ public class ReplayMod {
|
||||
public static CustomObjectRenderer customObjectRenderer;
|
||||
public static SoundHandler soundHandler = new SoundHandler();
|
||||
public static CrosshairRenderHandler crosshairRenderHandler;
|
||||
public static ApiClient apiClient;
|
||||
|
||||
@Getter
|
||||
private static boolean latestModVersion = true;
|
||||
@@ -134,7 +137,9 @@ public class ReplayMod {
|
||||
|
||||
config = new Configuration(event.getSuggestedConfigurationFile());
|
||||
config.load();
|
||||
AuthenticationHandler.loadAuthkeyFromConfig();
|
||||
ConfigurationAuthData authData = new ConfigurationAuthData(config);
|
||||
apiClient = new ApiClient(authData);
|
||||
authData.load(apiClient);
|
||||
|
||||
uploadedFileHandler = new UploadedFileHandler(event.getModConfigurationDirectory());
|
||||
|
||||
|
||||
@@ -27,28 +27,66 @@ public class ApiClient {
|
||||
private static final Gson gson = new Gson();
|
||||
private static final JsonParser jsonParser = new JsonParser();
|
||||
|
||||
public AuthKey getLogin(String username, String password) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.login);
|
||||
builder.put("user", username);
|
||||
builder.put("pw", password);
|
||||
builder.put("mod", true);
|
||||
return invokeAndReturn(builder, AuthKey.class);
|
||||
private final AuthData authData;
|
||||
|
||||
public ApiClient(AuthData authData) {
|
||||
this.authData = authData;
|
||||
}
|
||||
|
||||
public AuthKey register(String username, String mail, String password, String uuid)
|
||||
throws IOException, ApiException, AuthenticationException {
|
||||
public boolean isLoggedIn() {
|
||||
return authData.getUserName() != null && authData.getAuthKey() != null;
|
||||
}
|
||||
|
||||
public String getAuthKey() {
|
||||
return authData.getAuthKey();
|
||||
}
|
||||
|
||||
public void register(String userName, String eMail, String password) throws AuthenticationException, IOException, ApiException {
|
||||
AuthenticationHash authenticationHash = sessionserverJoin();
|
||||
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.register);
|
||||
builder.put("username", username);
|
||||
builder.put("email", mail);
|
||||
builder.put("username", userName);
|
||||
builder.put("email", eMail);
|
||||
builder.put("password", password);
|
||||
builder.put("uuid", uuid);
|
||||
builder.put("uuid", mc.getSession().getProfile().getId().toString());
|
||||
builder.put("mcusername", authenticationHash.username);
|
||||
builder.put("timelong", authenticationHash.currentTime);
|
||||
builder.put("randomlong", authenticationHash.randomLong);
|
||||
return invokeAndReturn(builder, AuthKey.class);
|
||||
AuthKey auth = invokeAndReturn(builder, AuthKey.class);
|
||||
|
||||
authData.setData(userName, auth.getAuth());
|
||||
}
|
||||
|
||||
public AuthData.AuthResult login(String userName, String password) {
|
||||
try {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.login);
|
||||
builder.put("user", userName);
|
||||
builder.put("pw", password);
|
||||
builder.put("mod", true);
|
||||
AuthKey result = invokeAndReturn(builder, AuthKey.class);
|
||||
authData.setData(userName, result.getAuth());
|
||||
return AuthData.AuthResult.SUCCESS;
|
||||
} catch(ApiException e) {
|
||||
return AuthData.AuthResult.INVALID_DATA;
|
||||
} catch(Exception e) {
|
||||
return AuthData.AuthResult.IO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
public AuthData.AuthResult logout() {
|
||||
try {
|
||||
authData.setData(null, null);
|
||||
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.logout);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
builder.put("mod", true);
|
||||
invokeAndReturn(builder, Success.class);
|
||||
return AuthData.AuthResult.SUCCESS;
|
||||
} catch(ApiException e) {
|
||||
return AuthData.AuthResult.INVALID_DATA;
|
||||
} catch(Exception e) {
|
||||
return AuthData.AuthResult.IO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
private AuthenticationHash sessionserverJoin() throws AuthenticationException {
|
||||
@@ -70,21 +108,6 @@ public class ApiClient {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean logout(String auth) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.logout);
|
||||
builder.put("auth", auth);
|
||||
builder.put("mod", true);
|
||||
Success succ = invokeAndReturn(builder, Success.class);
|
||||
return succ.isSuccess();
|
||||
}
|
||||
|
||||
public boolean hasDonated(String uuid) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.check_auth);
|
||||
builder.put("uuid", uuid);
|
||||
Donated succ = invokeAndReturn(builder, Donated.class);
|
||||
return succ.hasDonated();
|
||||
}
|
||||
|
||||
public FileInfo[] getFileInfo(List<Integer> ids) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.file_details);
|
||||
builder.put("id", buildListString(ids));
|
||||
@@ -111,11 +134,11 @@ public class ApiClient {
|
||||
|
||||
private boolean cancelDownload = false;
|
||||
|
||||
public void downloadFile(String auth, int file, File target, ProgressUpdateListener listener) throws IOException, ApiException {
|
||||
public void downloadFile(int file, File target, ProgressUpdateListener listener) throws IOException, ApiException {
|
||||
cancelDownload = false;
|
||||
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.download_file);
|
||||
builder.put("auth", auth);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
builder.put("id", file);
|
||||
String url = builder.toString();
|
||||
URL website = new URL(url);
|
||||
@@ -167,40 +190,40 @@ public class ApiClient {
|
||||
this.cancelDownload = true;
|
||||
}
|
||||
|
||||
public void rateFile(String auth, int file, Rating.RatingType rating) throws IOException, ApiException {
|
||||
public void rateFile(int file, Rating.RatingType rating) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.rate_file);
|
||||
builder.put("auth", auth);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
builder.put("id", file);
|
||||
builder.put("rating", rating.getKey());
|
||||
invokeAndReturn(builder, Success.class);
|
||||
}
|
||||
|
||||
public FileRating[] getRatedFiles(String auth) throws IOException, ApiException {
|
||||
public FileRating[] getRatedFiles() throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.get_ratings);
|
||||
builder.put("auth", auth);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
|
||||
return invokeAndReturn(builder, RatedFiles.class).getRated();
|
||||
}
|
||||
|
||||
public void favFile(String auth, int file, boolean fav) throws IOException, ApiException {
|
||||
public void favFile(int file, boolean fav) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.fav_file);
|
||||
builder.put("auth", auth);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
builder.put("id", file);
|
||||
builder.put("fav", fav);
|
||||
invokeAndReturn(builder, Success.class);
|
||||
}
|
||||
|
||||
public int[] getFavorites(String auth) throws IOException, ApiException {
|
||||
public int[] getFavorites() throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.get_favorites);
|
||||
builder.put("auth", auth);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
|
||||
return invokeAndReturn(builder, Favorites.class).getFavorited();
|
||||
}
|
||||
|
||||
@Api
|
||||
public void removeFile(String auth, int file) throws IOException, ApiException {
|
||||
public void removeFile(int file) throws IOException, ApiException {
|
||||
QueryBuilder builder = new QueryBuilder(ReplayModApiMethods.remove_file);
|
||||
builder.put("auth", auth);
|
||||
builder.put("auth", authData.getAuthKey());
|
||||
builder.put("id", file);
|
||||
invokeAndReturn(builder, Success.class);
|
||||
}
|
||||
|
||||
45
src/main/java/eu/crushedpixel/replaymod/api/AuthData.java
Normal file
45
src/main/java/eu/crushedpixel/replaymod/api/AuthData.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package eu.crushedpixel.replaymod.api;
|
||||
|
||||
/**
|
||||
* Represents a set of persistent authentication data.
|
||||
*/
|
||||
public interface AuthData {
|
||||
/**
|
||||
* Returns the user name of the authenticated user.
|
||||
* @return user name or {@code null} if not logged in
|
||||
*/
|
||||
String getUserName();
|
||||
|
||||
/**
|
||||
* Returns the authentication key of the authenticated user.
|
||||
* @return auth key or {@code null} if not logged in
|
||||
*/
|
||||
String getAuthKey();
|
||||
|
||||
/**
|
||||
* Store the authentication data after login.
|
||||
* @param userName The user name
|
||||
* @param authKey The authentication key
|
||||
*/
|
||||
void setData(String userName, String authKey);
|
||||
|
||||
/**
|
||||
* Result of authentication operations.
|
||||
*/
|
||||
enum AuthResult {
|
||||
/**
|
||||
* The operation succeeded without errors.
|
||||
*/
|
||||
SUCCESS,
|
||||
|
||||
/**
|
||||
* The data provided got rejected due to it being invalid.
|
||||
*/
|
||||
INVALID_DATA,
|
||||
|
||||
/**
|
||||
* Operation could not be performed due to connectivity problems.
|
||||
*/
|
||||
IO_ERROR,
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package eu.crushedpixel.replaymod.api.replay;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.ApiError;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.Category;
|
||||
import eu.crushedpixel.replaymod.gui.online.GuiUploadFile;
|
||||
@@ -23,7 +24,7 @@ public class FileUploader {
|
||||
|
||||
private GuiUploadFile parent;
|
||||
|
||||
public void uploadFile(GuiUploadFile gui, String auth, String filename, List<String> tags, File file, Category category, String description) {
|
||||
public void uploadFile(GuiUploadFile gui, String filename, List<String> tags, File file, Category category, String description) {
|
||||
boolean success = false;
|
||||
String info = null;
|
||||
|
||||
@@ -35,7 +36,7 @@ public class FileUploader {
|
||||
if(uploading) throw new RuntimeException("FileUploader is already uploading");
|
||||
uploading = true;
|
||||
|
||||
String postData = "?auth=" + auth + "&category=" + category.getId();
|
||||
String postData = "?auth=" + ReplayMod.apiClient.getAuthKey() + "&category=" + category.getId();
|
||||
|
||||
if(tags.size() > 0) {
|
||||
postData += "&tags=";
|
||||
|
||||
@@ -7,7 +7,6 @@ import eu.crushedpixel.replaymod.gui.online.GuiLoginPrompt;
|
||||
import eu.crushedpixel.replaymod.gui.online.GuiReplayCenter;
|
||||
import eu.crushedpixel.replaymod.gui.replayeditor.GuiReplayEditor;
|
||||
import eu.crushedpixel.replaymod.gui.replayviewer.GuiReplayViewer;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import eu.crushedpixel.replaymod.replay.ReplayHandler;
|
||||
import eu.crushedpixel.replaymod.replay.ReplayProcess;
|
||||
import eu.crushedpixel.replaymod.studio.VersionValidator;
|
||||
@@ -43,7 +42,7 @@ public class GuiEventHandler {
|
||||
if(event.gui instanceof GuiMainMenu) {
|
||||
if(ReplayMod.firstMainMenu) {
|
||||
ReplayMod.firstMainMenu = false;
|
||||
if(!AuthenticationHandler.isAuthenticated()) {
|
||||
if(!ReplayMod.apiClient.isLoggedIn()) {
|
||||
event.gui = new GuiLoginPrompt(event.gui, event.gui, false).toMinecraft();
|
||||
return;
|
||||
}
|
||||
@@ -57,7 +56,7 @@ public class GuiEventHandler {
|
||||
if(ReplayHandler.isInReplay()) ReplayHandler.setInReplay(false);
|
||||
}
|
||||
|
||||
if(!AuthenticationHandler.isAuthenticated()) return;
|
||||
if(!ReplayMod.apiClient.isLoggedIn()) return;
|
||||
|
||||
if(event.gui instanceof GuiChat || event.gui instanceof GuiInventory) {
|
||||
if(ReplayHandler.isInReplay()) {
|
||||
@@ -74,7 +73,7 @@ public class GuiEventHandler {
|
||||
public void onDraw(DrawScreenEvent e) {
|
||||
if(e.gui instanceof GuiMainMenu) {
|
||||
e.gui.drawString(mc.fontRendererObj, I18n.format("replaymod.title")+":", 5, 5, Color.WHITE.getRGB());
|
||||
if(AuthenticationHandler.isAuthenticated()) {
|
||||
if(ReplayMod.apiClient.isLoggedIn()) {
|
||||
e.gui.drawString(mc.fontRendererObj, I18n.format("replaymod.gui.loggedin").toUpperCase(), 5, 15, DARK_GREEN.getRGB());
|
||||
} else {
|
||||
e.gui.drawString(mc.fontRendererObj, I18n.format("replaymod.gui.loggedout").toUpperCase(), 5, 15, DARK_RED.getRGB());
|
||||
@@ -174,7 +173,7 @@ public class GuiEventHandler {
|
||||
if(event.button.id == GuiConstants.REPLAY_MANAGER_BUTTON_ID) {
|
||||
mc.displayGuiScreen(new GuiReplayViewer());
|
||||
} else if(event.button.id == GuiConstants.REPLAY_CENTER_BUTTON_ID) {
|
||||
if(AuthenticationHandler.isAuthenticated()) {
|
||||
if(ReplayMod.apiClient.isLoggedIn()) {
|
||||
mc.displayGuiScreen(new GuiReplayCenter());
|
||||
} else {
|
||||
mc.displayGuiScreen(new GuiLoginPrompt(event.gui, new GuiReplayCenter(), true).toMinecraft());
|
||||
|
||||
@@ -6,8 +6,8 @@ import de.johni0702.minecraft.gui.element.GuiLabel;
|
||||
import de.johni0702.minecraft.gui.element.GuiPasswordField;
|
||||
import de.johni0702.minecraft.gui.element.GuiTextField;
|
||||
import de.johni0702.minecraft.gui.layout.CustomLayout;
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.gui.GuiConstants;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
|
||||
public class GuiLoginPrompt extends AbstractGuiScreen<GuiLoginPrompt> {
|
||||
|
||||
@@ -34,8 +34,8 @@ public class GuiLoginPrompt extends AbstractGuiScreen<GuiLoginPrompt> {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
switch(AuthenticationHandler.authenticate(username.getText(), password.getText())) {
|
||||
case AuthenticationHandler.SUCCESS:
|
||||
switch(ReplayMod.apiClient.login(username.getText(), password.getText())) {
|
||||
case SUCCESS:
|
||||
statusLabel.setText("");
|
||||
getMinecraft().addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
@@ -44,10 +44,10 @@ public class GuiLoginPrompt extends AbstractGuiScreen<GuiLoginPrompt> {
|
||||
}
|
||||
});
|
||||
break;
|
||||
case AuthenticationHandler.INVALID:
|
||||
case INVALID_DATA:
|
||||
statusLabel.setI18nText("replaymod.gui.login.incorrect");
|
||||
break;
|
||||
case AuthenticationHandler.NO_CONNECTION:
|
||||
case IO_ERROR:
|
||||
statusLabel.setI18nText("replaymod.gui.login.connectionerror");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -7,9 +7,9 @@ import de.johni0702.minecraft.gui.element.*;
|
||||
import de.johni0702.minecraft.gui.layout.CustomLayout;
|
||||
import de.johni0702.minecraft.gui.layout.HorizontalLayout;
|
||||
import de.johni0702.minecraft.gui.layout.VerticalLayout;
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.api.ApiException;
|
||||
import eu.crushedpixel.replaymod.gui.GuiConstants;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import eu.crushedpixel.replaymod.utils.EmailAddressUtils;
|
||||
import eu.crushedpixel.replaymod.utils.RegexUtils;
|
||||
import net.minecraft.client.gui.FontRenderer;
|
||||
@@ -80,7 +80,7 @@ public class GuiRegister extends AbstractGuiScreen<GuiRegister> {
|
||||
String username = usernameInput.getText().trim();
|
||||
String mail = mailInput.getText().trim();
|
||||
String password = passwordInput.getText();
|
||||
AuthenticationHandler.register(username, mail, password);
|
||||
ReplayMod.apiClient.register(username, mail, password);
|
||||
|
||||
getMinecraft().addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
|
||||
@@ -14,7 +14,6 @@ import eu.crushedpixel.replaymod.gui.GuiConstants;
|
||||
import eu.crushedpixel.replaymod.gui.elements.*;
|
||||
import eu.crushedpixel.replaymod.gui.replayviewer.GuiReplayViewer;
|
||||
import eu.crushedpixel.replaymod.holders.GuiEntryListStringEntry;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import net.minecraft.client.gui.*;
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
@@ -22,8 +21,9 @@ import org.lwjgl.input.Keyboard;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
|
||||
public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
|
||||
@@ -59,7 +59,7 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
|
||||
Keyboard.enableRepeatEvents(true);
|
||||
|
||||
if(!initialized) {
|
||||
if(!AuthenticationHandler.isAuthenticated()) {
|
||||
if(!ReplayMod.apiClient.isLoggedIn()) {
|
||||
mc.displayGuiScreen(new GuiLoginPrompt(new GuiMainMenu(), this, true).toMinecraft());
|
||||
}
|
||||
|
||||
@@ -73,11 +73,11 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
|
||||
topBar.add(bestButton);
|
||||
|
||||
GuiButton downloadedReplayButton = new GuiButton(GuiConstants.CENTER_DOWNLOADED_REPLAYS_BUTTON, 20, 30, I18n.format("replaymod.gui.center.top.downloaded"));
|
||||
downloadedReplayButton.enabled = AuthenticationHandler.isAuthenticated();
|
||||
downloadedReplayButton.enabled = ReplayMod.apiClient.isLoggedIn();
|
||||
topBar.add(downloadedReplayButton);
|
||||
|
||||
GuiButton favoritedReplayButton = new GuiButton(GuiConstants.CENTER_FAVORITED_REPLAYS_BUTTON, 20, 30, I18n.format("replaymod.gui.center.top.favorited"));
|
||||
favoritedReplayButton.enabled = AuthenticationHandler.isAuthenticated();
|
||||
favoritedReplayButton.enabled = ReplayMod.apiClient.isLoggedIn();
|
||||
topBar.add(favoritedReplayButton);
|
||||
|
||||
GuiButton searchButton = new GuiButton(GuiConstants.CENTER_SEARCH_BUTTON, 20, 30, I18n.format("replaymod.gui.center.top.search"));
|
||||
@@ -382,7 +382,7 @@ public class GuiReplayCenter extends GuiScreen implements GuiYesNoCallback {
|
||||
mc.addScheduledTask(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
AuthenticationHandler.logout();
|
||||
ReplayMod.apiClient.logout();
|
||||
mc.displayGuiScreen(new GuiMainMenu());
|
||||
}
|
||||
});
|
||||
|
||||
@@ -7,7 +7,6 @@ import eu.crushedpixel.replaymod.gui.GuiConstants;
|
||||
import eu.crushedpixel.replaymod.gui.elements.*;
|
||||
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
||||
import eu.crushedpixel.replaymod.gui.replayviewer.GuiReplayViewer;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import eu.crushedpixel.replaymod.recording.ReplayMetaData;
|
||||
import eu.crushedpixel.replaymod.registry.KeybindRegistry;
|
||||
import eu.crushedpixel.replaymod.registry.ResourceHelper;
|
||||
@@ -135,7 +134,7 @@ public class GuiUploadFile extends GuiScreen implements ProgressUpdateListener {
|
||||
return;
|
||||
}
|
||||
|
||||
if(!AuthenticationHandler.isAuthenticated()) {
|
||||
if(!ReplayMod.apiClient.isLoggedIn()) {
|
||||
mc.displayGuiScreen(new GuiLoginPrompt(parent, this, true).toMinecraft());
|
||||
return;
|
||||
}
|
||||
@@ -320,12 +319,12 @@ public class GuiUploadFile extends GuiScreen implements ProgressUpdateListener {
|
||||
FileUtils.copyFile(replayFile, tmp);
|
||||
ReplayFileIO.addFilesToZip(tmp, toAdd);
|
||||
|
||||
uploader.uploadFile(GuiUploadFile.this, AuthenticationHandler.getKey(), name, tags, tmp, category, desc);
|
||||
uploader.uploadFile(GuiUploadFile.this, name, tags, tmp, category, desc);
|
||||
|
||||
FileUtils.deleteQuietly(tmpMeta);
|
||||
FileUtils.deleteQuietly(tmp);
|
||||
} else {
|
||||
uploader.uploadFile(GuiUploadFile.this, AuthenticationHandler.getKey(), name, tags, replayFile, category, desc);
|
||||
uploader.uploadFile(GuiUploadFile.this, name, tags, replayFile, category, desc);
|
||||
}
|
||||
|
||||
ReplayMod.uploadedFileHandler.markAsUploaded(replayFile);
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
package eu.crushedpixel.replaymod.online.authentication;
|
||||
|
||||
import com.mojang.authlib.exceptions.AuthenticationException;
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.api.ApiClient;
|
||||
import eu.crushedpixel.replaymod.api.ApiException;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.AuthConfirmation;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.AuthKey;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class AuthenticationHandler {
|
||||
|
||||
public static final int SUCCESS = 1;
|
||||
public static final int INVALID = 2;
|
||||
public static final int NO_CONNECTION = 3;
|
||||
|
||||
private static final ApiClient apiClient = new ApiClient();
|
||||
|
||||
private static String authkey = null;
|
||||
private static String username = null;
|
||||
|
||||
public static boolean isAuthenticated() {
|
||||
return authkey != null;
|
||||
}
|
||||
|
||||
public static String getKey() {
|
||||
return authkey;
|
||||
}
|
||||
public static String getUsername() { return username; }
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static boolean hasDonated(String uuid) throws IOException, ApiException {
|
||||
return apiClient.hasDonated(uuid);
|
||||
}
|
||||
|
||||
private static Minecraft mc = Minecraft.getMinecraft();
|
||||
|
||||
public static void register(String usrname, String mail, String password)
|
||||
throws IOException, ApiException, AuthenticationException {
|
||||
AuthKey auth = apiClient.register(usrname, mail, password,
|
||||
mc.getSession().getProfile().getId().toString());
|
||||
username = usrname;
|
||||
authkey = auth.getAuth();
|
||||
saveAuthkey(authkey);
|
||||
}
|
||||
|
||||
public static void loadAuthkeyFromConfig() {
|
||||
Property p = ReplayMod.config.get("authkey", "authkey", "null");
|
||||
|
||||
String key = null;
|
||||
if(!(p.getString().equals("null"))) {
|
||||
key = p.getString();
|
||||
}
|
||||
|
||||
if(key != null) {
|
||||
AuthConfirmation conf = apiClient.checkAuthkey(key);
|
||||
if(conf != null) {
|
||||
authkey = key;
|
||||
username = conf.getUsername();
|
||||
} else {
|
||||
saveAuthkey("null");
|
||||
username = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static int authenticate(String usrname, String password) {
|
||||
try {
|
||||
authkey = ReplayMod.apiClient.getLogin(usrname, password).getAuth();
|
||||
username = usrname;
|
||||
saveAuthkey(authkey);
|
||||
return SUCCESS;
|
||||
} catch(ApiException e) {
|
||||
return INVALID;
|
||||
} catch(Exception e) {
|
||||
return NO_CONNECTION;
|
||||
}
|
||||
}
|
||||
|
||||
public static int logout() {
|
||||
try {
|
||||
ReplayMod.apiClient.logout(authkey);
|
||||
authkey = null;
|
||||
username = null;
|
||||
saveAuthkey("null");
|
||||
return SUCCESS;
|
||||
} catch(ApiException e) {
|
||||
return INVALID;
|
||||
} catch(Exception e) {
|
||||
return NO_CONNECTION;
|
||||
}
|
||||
}
|
||||
|
||||
private static void saveAuthkey(String authkey) {
|
||||
ReplayMod.config.removeCategory(ReplayMod.config.getCategory("authkey"));
|
||||
ReplayMod.config.get("authkey", "authkey", authkey);
|
||||
ReplayMod.config.save();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package eu.crushedpixel.replaymod.online.authentication;
|
||||
|
||||
import eu.crushedpixel.replaymod.api.ApiClient;
|
||||
import eu.crushedpixel.replaymod.api.AuthData;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.AuthConfirmation;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
/**
|
||||
* Auth data stored in a {@link Configuration}.
|
||||
*/
|
||||
public class ConfigurationAuthData implements AuthData {
|
||||
|
||||
private final Configuration config;
|
||||
private String userName;
|
||||
private String authKey;
|
||||
|
||||
public ConfigurationAuthData(Configuration config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the data from the configuration and checks for its validity.
|
||||
* If the data is invalid, it is removed from the config.
|
||||
* @param apiClient Api client used for validating the auth data
|
||||
*/
|
||||
public void load(ApiClient apiClient) {
|
||||
Property property = config.get("authkey", "authkey", (String) null);
|
||||
if (property != null) {
|
||||
String authKey = property.getString();
|
||||
AuthConfirmation result = apiClient.checkAuthkey(authKey);
|
||||
if (result != null) {
|
||||
this.authKey = authKey;
|
||||
this.userName = result.getUsername();
|
||||
} else {
|
||||
setData(null, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthKey() {
|
||||
return authKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setData(String userName, String authKey) {
|
||||
this.userName = userName;
|
||||
this.authKey = authKey;
|
||||
|
||||
config.removeCategory(config.getCategory("authkey"));
|
||||
if (authKey != null) {
|
||||
// Note: .get() actually creates the entry with the default value if it doesn't exist
|
||||
config.get("authkey", "authkey", authKey);
|
||||
}
|
||||
config.save();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package eu.crushedpixel.replaymod.registry;
|
||||
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.gui.elements.listeners.ProgressUpdateListener;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
import eu.crushedpixel.replaymod.utils.ReplayFile;
|
||||
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
@@ -59,7 +58,7 @@ public class DownloadedFileHandler {
|
||||
f = generateFileForID(id);
|
||||
|
||||
try {
|
||||
ReplayMod.apiClient.downloadFile(AuthenticationHandler.getKey(), id, f, progressUpdateListener);
|
||||
ReplayMod.apiClient.downloadFile(id, f, progressUpdateListener);
|
||||
if(f.exists()) {
|
||||
addToIndex(id);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package eu.crushedpixel.replaymod.registry;
|
||||
import com.google.common.primitives.Ints;
|
||||
import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.api.ApiException;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
@@ -26,20 +25,20 @@ public class FavoritedFileHandler {
|
||||
}
|
||||
|
||||
public void addToFavorites(Integer id) throws IOException, ApiException {
|
||||
ReplayMod.apiClient.favFile(AuthenticationHandler.getKey(), id, true);
|
||||
ReplayMod.apiClient.favFile(id, true);
|
||||
favorited.remove(id);
|
||||
favorited.add(id);
|
||||
}
|
||||
|
||||
public void removeFromFavorites(Integer id) throws IOException, ApiException {
|
||||
ReplayMod.apiClient.favFile(AuthenticationHandler.getKey(), id, false);
|
||||
ReplayMod.apiClient.favFile(id, false);
|
||||
favorited.remove(id);
|
||||
}
|
||||
|
||||
public void reloadFavorites() {
|
||||
if(AuthenticationHandler.isAuthenticated()) {
|
||||
if(ReplayMod.apiClient.isLoggedIn()) {
|
||||
try {
|
||||
int[] ids = ReplayMod.apiClient.getFavorites(AuthenticationHandler.getKey());
|
||||
int[] ids = ReplayMod.apiClient.getFavorites();
|
||||
favorited = new ArrayList<Integer>(Ints.asList(ids));
|
||||
retrieved = true;
|
||||
} catch(Exception e) {
|
||||
|
||||
@@ -4,7 +4,6 @@ import eu.crushedpixel.replaymod.ReplayMod;
|
||||
import eu.crushedpixel.replaymod.api.ApiException;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.FileRating;
|
||||
import eu.crushedpixel.replaymod.api.replay.holders.Rating;
|
||||
import eu.crushedpixel.replaymod.online.authentication.AuthenticationHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
@@ -24,7 +23,7 @@ public class RatedFileHandler {
|
||||
}
|
||||
|
||||
public void rateFile(int id, Rating.RatingType type) throws IOException, ApiException {
|
||||
ReplayMod.apiClient.rateFile(AuthenticationHandler.getKey(), id, type);
|
||||
ReplayMod.apiClient.rateFile(id, type);
|
||||
if(type == Rating.RatingType.LIKE || type == Rating.RatingType.DISLIKE) {
|
||||
rated.put(id, type == Rating.RatingType.LIKE);
|
||||
} else {
|
||||
@@ -33,9 +32,9 @@ public class RatedFileHandler {
|
||||
}
|
||||
|
||||
public void reloadRatings() {
|
||||
if(AuthenticationHandler.isAuthenticated()) {
|
||||
if(ReplayMod.apiClient.isLoggedIn()) {
|
||||
try {
|
||||
FileRating[] ratings = ReplayMod.apiClient.getRatedFiles(AuthenticationHandler.getKey());
|
||||
FileRating[] ratings = ReplayMod.apiClient.getRatedFiles();
|
||||
rated = new HashMap<Integer, Boolean>();
|
||||
|
||||
for(FileRating fr : ratings) {
|
||||
|
||||
Reference in New Issue
Block a user