From 47303cced8f6e55aaab2579293da49723ba39c1d Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 4 Jan 2025 19:30:20 +0500 Subject: upd: renamed SessionClient to IdentityClient --- core/src/kz/ilotterytea/maxon/MaxonGame.java | 10 +- .../kz/ilotterytea/maxon/screens/MenuScreen.java | 6 +- .../ilotterytea/maxon/session/IdentityClient.java | 323 +++++++++++++++++++++ .../ilotterytea/maxon/session/SessionClient.java | 323 --------------------- 4 files changed, 331 insertions(+), 331 deletions(-) create mode 100644 core/src/kz/ilotterytea/maxon/session/IdentityClient.java delete mode 100644 core/src/kz/ilotterytea/maxon/session/SessionClient.java diff --git a/core/src/kz/ilotterytea/maxon/MaxonGame.java b/core/src/kz/ilotterytea/maxon/MaxonGame.java index b53c510..8986de9 100644 --- a/core/src/kz/ilotterytea/maxon/MaxonGame.java +++ b/core/src/kz/ilotterytea/maxon/MaxonGame.java @@ -8,7 +8,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch; import kz.ilotterytea.maxon.localization.LocalizationManager; import kz.ilotterytea.maxon.pets.PetManager; import kz.ilotterytea.maxon.screens.SplashScreen; -import kz.ilotterytea.maxon.session.SessionClient; +import kz.ilotterytea.maxon.session.IdentityClient; import kz.ilotterytea.maxon.utils.GameUpdater; public class MaxonGame extends Game { @@ -20,7 +20,7 @@ public class MaxonGame extends Game { private PetManager petManager; private DiscordActivityClient discordActivityClient; - private SessionClient sessionClient; + private IdentityClient identityClient; private static MaxonGame instance; @@ -39,8 +39,8 @@ public class MaxonGame extends Game { return discordActivityClient; } - public SessionClient getSessionClient() { - return sessionClient; + public IdentityClient getIdentityClient() { + return identityClient; } public LocalizationManager getLocale() { @@ -56,7 +56,7 @@ public class MaxonGame extends Game { // Check the latest version new GameUpdater().checkLatestUpdate(); - sessionClient = new SessionClient(Gdx.app.getPreferences("kz.ilotterytea.SigninSession")); + identityClient = new IdentityClient(Gdx.app.getPreferences("kz.ilotterytea.SigninIdentity")); batch = new SpriteBatch(); prefs = Gdx.app.getPreferences(MaxonConstants.GAME_APP_PACKAGE); locale = new LocalizationManager(Gdx.files.internal("i18n/" + prefs.getString("lang", "en_us") + ".json")); diff --git a/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java b/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java index ec60b33..0cf096d 100644 --- a/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java +++ b/core/src/kz/ilotterytea/maxon/screens/MenuScreen.java @@ -28,7 +28,7 @@ import kz.ilotterytea.maxon.constants.SettingsConstants; import kz.ilotterytea.maxon.localization.LineId; import kz.ilotterytea.maxon.localization.LocalizationManager; import kz.ilotterytea.maxon.player.Savegame; -import kz.ilotterytea.maxon.session.SessionClient; +import kz.ilotterytea.maxon.session.IdentityClient; import kz.ilotterytea.maxon.ui.DebugWidget; import kz.ilotterytea.maxon.ui.SavegameWidget; import kz.ilotterytea.maxon.ui.ShakingImageButton; @@ -407,7 +407,7 @@ public class MenuScreen implements Screen { stage.draw(); // Login button logic - SessionClient session = game.getSessionClient(); + IdentityClient session = game.getIdentityClient(); if (!session.isProcessing() && !session.isAuthorised() && !loginButton.getText().equals(game.getLocale().getLine(LineId.LoginButton))) { loginButton.setText(game.getLocale().getLine(LineId.LoginButton)); loginButton.clearListeners(); @@ -850,7 +850,7 @@ public class MenuScreen implements Screen { @Override public void clicked(InputEvent event, float x, float y) { super.clicked(event, x, y); - MaxonGame.getInstance().getSessionClient().authorize(usernameField.getText(), passwords[0]); + MaxonGame.getInstance().getIdentityClient().authorize(usernameField.getText(), passwords[0]); loginButton.setText(game.getLocale().getLine(LineId.LoginProcessing)); loginButton.setDisabled(true); // maybe we could somehow fire the close button instead of cv pasting diff --git a/core/src/kz/ilotterytea/maxon/session/IdentityClient.java b/core/src/kz/ilotterytea/maxon/session/IdentityClient.java new file mode 100644 index 0000000..570b16b --- /dev/null +++ b/core/src/kz/ilotterytea/maxon/session/IdentityClient.java @@ -0,0 +1,323 @@ +package kz.ilotterytea.maxon.session; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Net; +import com.badlogic.gdx.Preferences; +import com.badlogic.gdx.net.HttpRequestBuilder; +import com.badlogic.gdx.net.HttpStatus; +import com.badlogic.gdx.utils.JsonReader; +import com.badlogic.gdx.utils.JsonValue; +import com.badlogic.gdx.utils.JsonWriter; +import com.badlogic.gdx.utils.Timer; +import kz.ilotterytea.maxon.MaxonConstants; +import kz.ilotterytea.maxon.utils.RandomUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IdentityClient { + private final Logger log; + + private final Preferences sessionPreferences; + private final String clientToken; + private String accessToken; + private String userId, username, password; + private boolean isProcessing, isAuthorised; + + public IdentityClient(Preferences sessionPreferences) { + startValidationThread(); + this.log = LoggerFactory.getLogger(IdentityClient.class); + + this.clientToken = RandomUtils.generateRandomString(); + this.sessionPreferences = sessionPreferences; + this.isProcessing = false; + this.isAuthorised = false; + + if (sessionPreferences.contains("username") && sessionPreferences.contains("password")) { + this.authorize(sessionPreferences.getString("username"), sessionPreferences.getString("password")); + } + } + + public void authorize(String username, String password) { + log.info("Authorising..."); + this.isProcessing = true; + sessionPreferences.putString("username", username); + sessionPreferences.putString("password", password); + sessionPreferences.flush(); + + JsonValue agent = new JsonValue(JsonValue.ValueType.object); + agent.addChild("name", new JsonValue(String.valueOf(MaxonConstants.GAME_APP_ID.charAt(0)).toUpperCase() + MaxonConstants.GAME_APP_ID.substring(1))); + agent.addChild("version", new JsonValue(MaxonConstants.GAME_PROTOCOL)); + + JsonValue json = new JsonValue(JsonValue.ValueType.object); + json.addChild("agent", agent); + json.addChild("username", new JsonValue(username)); + json.addChild("password", new JsonValue(password)); + json.addChild("clientToken", new JsonValue(clientToken)); + + Net.HttpRequest request = + new HttpRequestBuilder() + .newRequest() + .method(Net.HttpMethods.POST) + .url(MaxonConstants.IDENTITY_AUTHENTICATION_URL) + .timeout(20000) + .header("Content-Type", "application/json") + .content(json.toJson(JsonWriter.OutputType.json)) + .build(); + + Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + IdentityClient.this.isProcessing = false; + + try { + JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); + + if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { + String type = json.get("error").getString("type"); + String error = json.get("error").getString("message"); + log.error("Failed to authorise: {} ({})", error, type); + + sessionPreferences.remove("username"); + sessionPreferences.remove("password"); + sessionPreferences.flush(); + + return; + } + + IdentityClient.this.username = username; + IdentityClient.this.password = password; + IdentityClient.this.accessToken = json.get("data").getString("accessToken"); + IdentityClient.this.userId = String.valueOf(json.get("data").get("user").getInt("id")); + IdentityClient.this.isAuthorised = true; + log.info("Successfully authorised! Welcome back, {}!", IdentityClient.this.username); + } catch (Exception e) { + log.error("An exception was thrown while authorising", e); + } + } + + @Override + public void failed(Throwable t) { + log.error("Failed to send an authorisation request", t); + } + + @Override + public void cancelled() { + log.info("Authorisation request was cancelled!"); + } + }); + } + + public void validateToken() { + if (clientToken == null || accessToken == null) { + return; + } + + log.info("Validating token..."); + + JsonValue json = new JsonValue(JsonValue.ValueType.object); + json.addChild("clientToken", new JsonValue(clientToken)); + json.addChild("accessToken", new JsonValue(accessToken)); + + Net.HttpRequest request = + new HttpRequestBuilder() + .newRequest() + .method(Net.HttpMethods.POST) + .url(MaxonConstants.IDENTITY_VALIDATE_URL) + .timeout(20000) + .header("Content-Type", "application/json") + .content(json.toJson(JsonWriter.OutputType.json)) + .build(); + + Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + try { + JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); + + if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { + String type = json.get("error").getString("type"); + String error = json.get("error").getString("message"); + log.error("Failed to validate: {} ({})", error, type); + accessToken = null; + userId = null; + isAuthorised = false; + authorize(username, password); + return; + } + + int expiresInSeconds = json.get("data").getInt("expiresInSeconds"); + + if (expiresInSeconds < 1000) { + refreshToken(); + } + + log.info("Token validated!"); + } catch (Exception e) { + log.error("An exception was thrown while validating", e); + } + } + + @Override + public void failed(Throwable t) { + log.error("Failed to send a validation request", t); + } + + @Override + public void cancelled() { + log.info("Validation request was cancelled!"); + } + }); + } + + public void invalidateToken() { + if (clientToken == null || accessToken == null) { + return; + } + + log.info("Invalidating token..."); + + JsonValue json = new JsonValue(JsonValue.ValueType.object); + json.addChild("clientToken", new JsonValue(clientToken)); + json.addChild("accessToken", new JsonValue(accessToken)); + + Net.HttpRequest request = + new HttpRequestBuilder() + .newRequest() + .method(Net.HttpMethods.POST) + .url(MaxonConstants.IDENTITY_INVALIDATE_URL) + .timeout(20000) + .header("Content-Type", "application/json") + .content(json.toJson(JsonWriter.OutputType.json)) + .build(); + + Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + try { + JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); + + if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { + String type = json.get("error").getString("type"); + String error = json.get("error").getString("message"); + log.error("Failed to invalidate: {} ({})", error, type); + return; + } + + log.info("Invalidated! Bye, {}", username); + + accessToken = null; + userId = null; + username = null; + password = null; + isAuthorised = false; + sessionPreferences.remove("username"); + sessionPreferences.remove("password"); + sessionPreferences.flush(); + } catch (Exception ignored) { + } + } + + @Override + public void failed(Throwable t) { + } + + @Override + public void cancelled() { + } + }); + } + + public void refreshToken() { + if (clientToken == null || accessToken == null) { + return; + } + + log.info("Refreshing token..."); + + JsonValue json = new JsonValue(JsonValue.ValueType.object); + json.addChild("clientToken", new JsonValue(clientToken)); + json.addChild("accessToken", new JsonValue(accessToken)); + + Net.HttpRequest request = + new HttpRequestBuilder() + .newRequest() + .method(Net.HttpMethods.POST) + .url(MaxonConstants.IDENTITY_REFRESH_URL) + .timeout(20000) + .header("Content-Type", "application/json") + .content(json.toJson(JsonWriter.OutputType.json)) + .build(); + + Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + try { + JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); + + if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { + String type = json.get("error").getString("type"); + String error = json.get("error").getString("message"); + log.error("Failed to refresh: {} ({})", error, type); + accessToken = null; + userId = null; + isAuthorised = false; + log.warn(error); + return; + } + + accessToken = json.get("data").get("accessToken").asString(); + log.info("Token has been refreshed!"); + } catch (Exception e) { + log.error("An exception was thrown while refreshing", e); + } + } + + @Override + public void failed(Throwable t) { + log.error("Failed to send a refresh request", t); + } + + @Override + public void cancelled() { + log.info("Refresh request was cancelled!"); + } + }); + } + + public boolean isAuthorised() { + return this.isAuthorised; + } + + public boolean isProcessing() { + return isProcessing; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public String getAccessToken() { + return accessToken; + } + + public String getClientToken() { + return clientToken; + } + + public String getUserId() { + return userId; + } + + private void startValidationThread() { + Timer.schedule(new Timer.Task() { + @Override + public void run() { + validateToken(); + } + }, 60000, 60000); + } +} diff --git a/core/src/kz/ilotterytea/maxon/session/SessionClient.java b/core/src/kz/ilotterytea/maxon/session/SessionClient.java deleted file mode 100644 index 31edd6e..0000000 --- a/core/src/kz/ilotterytea/maxon/session/SessionClient.java +++ /dev/null @@ -1,323 +0,0 @@ -package kz.ilotterytea.maxon.session; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Net; -import com.badlogic.gdx.Preferences; -import com.badlogic.gdx.net.HttpRequestBuilder; -import com.badlogic.gdx.net.HttpStatus; -import com.badlogic.gdx.utils.JsonReader; -import com.badlogic.gdx.utils.JsonValue; -import com.badlogic.gdx.utils.JsonWriter; -import com.badlogic.gdx.utils.Timer; -import kz.ilotterytea.maxon.MaxonConstants; -import kz.ilotterytea.maxon.utils.RandomUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class SessionClient { - private final Logger log; - - private final Preferences sessionPreferences; - private final String clientToken; - private String accessToken; - private String userId, username, password; - private boolean isProcessing, isAuthorised; - - public SessionClient(Preferences sessionPreferences) { - startValidationThread(); - this.log = LoggerFactory.getLogger(SessionClient.class); - - this.clientToken = RandomUtils.generateRandomString(); - this.sessionPreferences = sessionPreferences; - this.isProcessing = false; - this.isAuthorised = false; - - if (sessionPreferences.contains("username") && sessionPreferences.contains("password")) { - this.authorize(sessionPreferences.getString("username"), sessionPreferences.getString("password")); - } - } - - public void authorize(String username, String password) { - log.info("Authorising..."); - this.isProcessing = true; - sessionPreferences.putString("username", username); - sessionPreferences.putString("password", password); - sessionPreferences.flush(); - - JsonValue agent = new JsonValue(JsonValue.ValueType.object); - agent.addChild("name", new JsonValue(String.valueOf(MaxonConstants.GAME_APP_ID.charAt(0)).toUpperCase() + MaxonConstants.GAME_APP_ID.substring(1))); - agent.addChild("version", new JsonValue(MaxonConstants.GAME_PROTOCOL)); - - JsonValue json = new JsonValue(JsonValue.ValueType.object); - json.addChild("agent", agent); - json.addChild("username", new JsonValue(username)); - json.addChild("password", new JsonValue(password)); - json.addChild("clientToken", new JsonValue(clientToken)); - - Net.HttpRequest request = - new HttpRequestBuilder() - .newRequest() - .method(Net.HttpMethods.POST) - .url(MaxonConstants.IDENTITY_AUTHENTICATION_URL) - .timeout(20000) - .header("Content-Type", "application/json") - .content(json.toJson(JsonWriter.OutputType.json)) - .build(); - - Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { - @Override - public void handleHttpResponse(Net.HttpResponse httpResponse) { - SessionClient.this.isProcessing = false; - - try { - JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); - - if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { - String type = json.get("error").getString("type"); - String error = json.get("error").getString("message"); - log.error("Failed to authorise: {} ({})", error, type); - - sessionPreferences.remove("username"); - sessionPreferences.remove("password"); - sessionPreferences.flush(); - - return; - } - - SessionClient.this.username = username; - SessionClient.this.password = password; - SessionClient.this.accessToken = json.get("data").getString("accessToken"); - SessionClient.this.userId = String.valueOf(json.get("data").get("user").getInt("id")); - SessionClient.this.isAuthorised = true; - log.info("Successfully authorised! Welcome back, {}!", SessionClient.this.username); - } catch (Exception e) { - log.error("An exception was thrown while authorising", e); - } - } - - @Override - public void failed(Throwable t) { - log.error("Failed to send an authorisation request", t); - } - - @Override - public void cancelled() { - log.info("Authorisation request was cancelled!"); - } - }); - } - - public void validateToken() { - if (clientToken == null || accessToken == null) { - return; - } - - log.info("Validating token..."); - - JsonValue json = new JsonValue(JsonValue.ValueType.object); - json.addChild("clientToken", new JsonValue(clientToken)); - json.addChild("accessToken", new JsonValue(accessToken)); - - Net.HttpRequest request = - new HttpRequestBuilder() - .newRequest() - .method(Net.HttpMethods.POST) - .url(MaxonConstants.IDENTITY_VALIDATE_URL) - .timeout(20000) - .header("Content-Type", "application/json") - .content(json.toJson(JsonWriter.OutputType.json)) - .build(); - - Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { - @Override - public void handleHttpResponse(Net.HttpResponse httpResponse) { - try { - JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); - - if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { - String type = json.get("error").getString("type"); - String error = json.get("error").getString("message"); - log.error("Failed to validate: {} ({})", error, type); - accessToken = null; - userId = null; - isAuthorised = false; - authorize(username, password); - return; - } - - int expiresInSeconds = json.get("data").getInt("expiresInSeconds"); - - if (expiresInSeconds < 1000) { - refreshToken(); - } - - log.info("Token validated!"); - } catch (Exception e) { - log.error("An exception was thrown while validating", e); - } - } - - @Override - public void failed(Throwable t) { - log.error("Failed to send a validation request", t); - } - - @Override - public void cancelled() { - log.info("Validation request was cancelled!"); - } - }); - } - - public void invalidateToken() { - if (clientToken == null || accessToken == null) { - return; - } - - log.info("Invalidating token..."); - - JsonValue json = new JsonValue(JsonValue.ValueType.object); - json.addChild("clientToken", new JsonValue(clientToken)); - json.addChild("accessToken", new JsonValue(accessToken)); - - Net.HttpRequest request = - new HttpRequestBuilder() - .newRequest() - .method(Net.HttpMethods.POST) - .url(MaxonConstants.IDENTITY_INVALIDATE_URL) - .timeout(20000) - .header("Content-Type", "application/json") - .content(json.toJson(JsonWriter.OutputType.json)) - .build(); - - Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { - @Override - public void handleHttpResponse(Net.HttpResponse httpResponse) { - try { - JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); - - if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { - String type = json.get("error").getString("type"); - String error = json.get("error").getString("message"); - log.error("Failed to invalidate: {} ({})", error, type); - return; - } - - log.info("Invalidated! Bye, {}", username); - - accessToken = null; - userId = null; - username = null; - password = null; - isAuthorised = false; - sessionPreferences.remove("username"); - sessionPreferences.remove("password"); - sessionPreferences.flush(); - } catch (Exception ignored) { - } - } - - @Override - public void failed(Throwable t) { - } - - @Override - public void cancelled() { - } - }); - } - - public void refreshToken() { - if (clientToken == null || accessToken == null) { - return; - } - - log.info("Refreshing token..."); - - JsonValue json = new JsonValue(JsonValue.ValueType.object); - json.addChild("clientToken", new JsonValue(clientToken)); - json.addChild("accessToken", new JsonValue(accessToken)); - - Net.HttpRequest request = - new HttpRequestBuilder() - .newRequest() - .method(Net.HttpMethods.POST) - .url(MaxonConstants.IDENTITY_REFRESH_URL) - .timeout(20000) - .header("Content-Type", "application/json") - .content(json.toJson(JsonWriter.OutputType.json)) - .build(); - - Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { - @Override - public void handleHttpResponse(Net.HttpResponse httpResponse) { - try { - JsonValue json = new JsonReader().parse(httpResponse.getResultAsString()); - - if (httpResponse.getStatus().getStatusCode() != HttpStatus.SC_OK) { - String type = json.get("error").getString("type"); - String error = json.get("error").getString("message"); - log.error("Failed to refresh: {} ({})", error, type); - accessToken = null; - userId = null; - isAuthorised = false; - log.warn(error); - return; - } - - accessToken = json.get("data").get("accessToken").asString(); - log.info("Token has been refreshed!"); - } catch (Exception e) { - log.error("An exception was thrown while refreshing", e); - } - } - - @Override - public void failed(Throwable t) { - log.error("Failed to send a refresh request", t); - } - - @Override - public void cancelled() { - log.info("Refresh request was cancelled!"); - } - }); - } - - public boolean isAuthorised() { - return this.isAuthorised; - } - - public boolean isProcessing() { - return isProcessing; - } - - public String getUsername() { - return username; - } - - public String getPassword() { - return password; - } - - public String getAccessToken() { - return accessToken; - } - - public String getClientToken() { - return clientToken; - } - - public String getUserId() { - return userId; - } - - private void startValidationThread() { - Timer.schedule(new Timer.Task() { - @Override - public void run() { - validateToken(); - } - }, 60000, 60000); - } -} -- cgit v1.2.3