From ec1153e9cca432c7fccb9871389ea2536d90b5dd Mon Sep 17 00:00:00 2001 From: khanhduytran0 Date: Tue, 8 Dec 2020 18:55:11 +0700 Subject: [PATCH] Bug fix: Refresh token fails --- .../com/kdt/mojangauth/RefreshTokenTask.java | 16 ++++++++----- .../yggdrasil/YggdrasilAuthenticator.java | 23 +++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java b/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java index ed1dbfc5b..73dde18ee 100644 --- a/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java +++ b/app/src/main/java/com/kdt/mojangauth/RefreshTokenTask.java @@ -35,16 +35,20 @@ public class RefreshTokenTask extends AsyncTask { public Throwable doInBackground(String... args) { try { this.profilePath = MCProfile.load(args[0]); - // https://wiki.vg/Authentication - // Returns an empty payload (204 No Content) if successful, an error JSON with status 403 Forbidden otherwise. - if (204 != this.authenticator.validate(profilePath.getAccessToken()).statusCode) { + int responseCode = 400; + responseCode = this.authenticator.validate(profilePath.getAccessToken()).statusCode; + if (400 <= responseCode) { RefreshResponse response = this.authenticator.refresh(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID())); + // if (response == null) { + // throw new NullPointerException("Response is null?"); + // } if (response == null) { - throw new NullPointerException("Response is null?"); - } - if (response.selectedProfile == null) { + // Refresh when offline? + return null; + } else if (response.selectedProfile == null) { throw new IllegalArgumentException("Can't refresh a demo account!"); } + profilePath.setClientID(response.clientToken.toString()); profilePath.setAccessToken(response.accessToken); profilePath.setUsername(response.selectedProfile.name); diff --git a/app/src/main/java/com/kdt/mojangauth/yggdrasil/YggdrasilAuthenticator.java b/app/src/main/java/com/kdt/mojangauth/yggdrasil/YggdrasilAuthenticator.java index 49a9138f2..b06196503 100644 --- a/app/src/main/java/com/kdt/mojangauth/yggdrasil/YggdrasilAuthenticator.java +++ b/app/src/main/java/com/kdt/mojangauth/yggdrasil/YggdrasilAuthenticator.java @@ -13,7 +13,7 @@ public class YggdrasilAuthenticator { private String clientName = "Minecraft"; private int clientVersion = 1; - private NetworkResponse makeRequest(String endpoint, Object inputObject, Class responseClass) throws IOException, Throwable { + private NetworkResponse makeRequest(String endpoint, Object inputObject, Class responseClass) throws IOException, Throwable { Throwable th; InputStream is = null; ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -61,12 +61,14 @@ public class YggdrasilAuthenticator { } else { Log.i("Result", "Task " + endpoint + " failure"); } - - if (responseClass == null) { - return new NetworkResponse(statusCode, outString); - } + + return new NetworkResponse(statusCode, outString); } catch (UnknownHostException e) { - throw new RuntimeException("Can't connect to the server", e); + if (endpoint.equals("refresh")) { + return null; + } else { + throw new RuntimeException("Can't connect to the server", e); + } } catch (Throwable th2) { th = th2; if (is != null) { @@ -86,7 +88,6 @@ public class YggdrasilAuthenticator { } throw th; } - return null; } public AuthenticateResponse authenticate(String username, String password, UUID clientId) throws IOException, Throwable { @@ -102,8 +103,12 @@ public class YggdrasilAuthenticator { public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable { NetworkResponse obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class); - obj.throwExceptionIfNeed(); // "Invalid username or password, status code: " + obj.statusCode); - return (RefreshResponse) obj.response; + if (obj == null) { + return null; + } else { + obj.throwExceptionIfNeed(); // "Invalid username or password, status code: " + obj.statusCode); + return (RefreshResponse) obj.response; + } } public NetworkResponse validate(String authToken) throws Throwable {