From aa417be5a695ccb0a4d3093e247df99466dfa0c3 Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Wed, 9 Dec 2020 18:39:19 +0700 Subject: [PATCH] [MSA] Bug fix: Cancel button gives "Login success" --- .../com/kdt/mojangauth/RefreshTokenTask.java | 4 +- .../mojangauth/yggdrasil/NetworkResponse.java | 2 +- .../kdt/pojavlaunch/PojavLoginActivity.java | 6 +- .../microsoft/MicrosoftAuthenticator.java | 73 +++++++++++++++++++ 4 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/net/kdt/pojavlaunch/authenticator/microsoft/MicrosoftAuthenticator.java diff --git a/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java b/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java index 73dde18ee..37bee4d06 100644 --- a/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java +++ b/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java @@ -25,7 +25,7 @@ public class RefreshTokenTask extends AsyncTask { @Override public void onPreExecute() { build = new ProgressDialog(ctx); - build.setMessage("Refreshing"); + build.setMessage(ctx.getString(R.string.global_waiting)); build.setProgressStyle(ProgressDialog.STYLE_SPINNER); build.setCancelable(false); build.show(); @@ -37,7 +37,7 @@ public class RefreshTokenTask extends AsyncTask { this.profilePath = MCProfile.load(args[0]); int responseCode = 400; responseCode = this.authenticator.validate(profilePath.getAccessToken()).statusCode; - if (400 <= responseCode) { + if (responseCode >= 200 && responseCode < 300) { RefreshResponse response = this.authenticator.refresh(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID())); // if (response == null) { // throw new NullPointerException("Response is null?"); diff --git a/app/src/main/java/com/kdt/mojangauth/yggdrasil/NetworkResponse.java b/app/src/main/java/com/kdt/mojangauth/yggdrasil/NetworkResponse.java index 9edecaec4..c60881139 100644 --- a/app/src/main/java/com/kdt/mojangauth/yggdrasil/NetworkResponse.java +++ b/app/src/main/java/com/kdt/mojangauth/yggdrasil/NetworkResponse.java @@ -12,7 +12,7 @@ public class NetworkResponse } public void throwExceptionIfNeed(String msg) { - if (statusCode >= 400) { + if (statusCode < 200 || statusCode >= 300) { throw new RuntimeException(msg); } } diff --git a/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java b/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java index ecf7fec89..3ccc483da 100644 --- a/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java +++ b/app/src/main/java/net/kdt/pojavlaunch/PojavLoginActivity.java @@ -314,9 +314,11 @@ public class PojavLoginActivity extends BaseActivity if (data != null && data.getScheme().equals("ms-xal-00000000402b5328") && data.getHost().equals("auth")) { String error = data.getQueryParameter("error"); String error_description = data.getQueryParameter("error_description"); - if (error != null && !error_description.startsWith("The user has denied access to the scope requested by the client application")) { + if (error != null) { // "The user has denied access to the scope requested by the client application": user pressed Cancel button, skip it - Toast.makeText(this, "Error: " + error + ": " + error_description, Toast.LENGTH_LONG).show(); + if (!error_description.startsWith("The user has denied access to the scope requested by the client application")) { + Toast.makeText(this, "Error: " + error + ": " + error_description, Toast.LENGTH_LONG).show(); + } } else { String code = data.getQueryParameter("code"); Toast.makeText(this, "Logged in to Microsoft account, but NYI", Toast.LENGTH_LONG).show(); diff --git a/app/src/main/java/net/kdt/pojavlaunch/authenticator/microsoft/MicrosoftAuthenticator.java b/app/src/main/java/net/kdt/pojavlaunch/authenticator/microsoft/MicrosoftAuthenticator.java new file mode 100644 index 000000000..117000fdf --- /dev/null +++ b/app/src/main/java/net/kdt/pojavlaunch/authenticator/microsoft/MicrosoftAuthenticator.java @@ -0,0 +1,73 @@ +package net.kdt.pojavlaunch.authenticator.microsoft; + +import android.app.*; +import android.content.*; +import android.os.*; +import com.kdt.mojangauth.*; +import com.kdt.mojangauth.yggdrasil.*; +import java.util.*; +import net.kdt.pojavlaunch.*; + +public class MicrosoftAuthenticator extends AsyncTask { + private YggdrasilAuthenticator authenticator = new YggdrasilAuthenticator(); + //private Gson gson = new Gson(); + private RefreshListener listener; + + private Context ctx; + private ProgressDialog build; + + public MicrosoftAuthenticator(Context ctx, RefreshListener listener) { + this.ctx = ctx; + this.listener = listener; + } + + @Override + public void onPreExecute() { + build = new ProgressDialog(ctx); + build.setMessage(ctx.getString(R.string.global_waiting)); + build.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + build.setCancelable(false); + build.show(); + } + + @Override + public Throwable doInBackground(String... args) { + try { + MCProfile.Builder profilePath = MCProfile.load(args[0]); + String authCode = args[1]; + + publishProgress(); + + /* + profilePath.setClientID(response.clientToken.toString()); + profilePath.setAccessToken(response.accessToken); + profilePath.setUsername(response.selectedProfile.name); + profilePath.setProfileID(response.selectedProfile.id); + MCProfile.build(profilePath); + */ + return null; + } catch (Throwable e) { + return e; + } + } + + + + @Override + protected void onProgressUpdate(Void[] p1) { + super.onProgressUpdate(p1); + build.setProgress(build.getProgress() + 1); + + } + + @Override + public void onPostExecute(Throwable result) { + build.dismiss(); + if (result == null) { + listener.onSuccess(); + } else { + listener.onFailed(result); + } + } +} +