Bug fix: Refresh token fails

This commit is contained in:
khanhduytran0 2020-12-08 18:55:11 +07:00
parent 5126193b7f
commit ec1153e9cc
2 changed files with 24 additions and 15 deletions

View File

@ -35,16 +35,20 @@ public class RefreshTokenTask extends AsyncTask<String, Void, Throwable> {
public Throwable doInBackground(String... args) { public Throwable doInBackground(String... args) {
try { try {
this.profilePath = MCProfile.load(args[0]); this.profilePath = MCProfile.load(args[0]);
// https://wiki.vg/Authentication int responseCode = 400;
// Returns an empty payload (204 No Content) if successful, an error JSON with status 403 Forbidden otherwise. responseCode = this.authenticator.validate(profilePath.getAccessToken()).statusCode;
if (204 != this.authenticator.validate(profilePath.getAccessToken()).statusCode) { if (400 <= responseCode) {
RefreshResponse response = this.authenticator.refresh(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID())); RefreshResponse response = this.authenticator.refresh(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID()));
// if (response == null) {
// throw new NullPointerException("Response is null?");
// }
if (response == null) { if (response == null) {
throw new NullPointerException("Response is null?"); // Refresh when offline?
} return null;
if (response.selectedProfile == null) { } else if (response.selectedProfile == null) {
throw new IllegalArgumentException("Can't refresh a demo account!"); throw new IllegalArgumentException("Can't refresh a demo account!");
} }
profilePath.setClientID(response.clientToken.toString()); profilePath.setClientID(response.clientToken.toString());
profilePath.setAccessToken(response.accessToken); profilePath.setAccessToken(response.accessToken);
profilePath.setUsername(response.selectedProfile.name); profilePath.setUsername(response.selectedProfile.name);

View File

@ -13,7 +13,7 @@ public class YggdrasilAuthenticator {
private String clientName = "Minecraft"; private String clientName = "Minecraft";
private int clientVersion = 1; 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; Throwable th;
InputStream is = null; InputStream is = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream();
@ -61,12 +61,14 @@ public class YggdrasilAuthenticator {
} else { } else {
Log.i("Result", "Task " + endpoint + " failure"); Log.i("Result", "Task " + endpoint + " failure");
} }
if (responseClass == null) { return new NetworkResponse(statusCode, outString);
return new NetworkResponse(statusCode, outString);
}
} catch (UnknownHostException e) { } 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) { } catch (Throwable th2) {
th = th2; th = th2;
if (is != null) { if (is != null) {
@ -86,7 +88,6 @@ public class YggdrasilAuthenticator {
} }
throw th; throw th;
} }
return null;
} }
public AuthenticateResponse authenticate(String username, String password, UUID clientId) throws IOException, Throwable { 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 { public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable {
NetworkResponse obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class); NetworkResponse obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class);
obj.throwExceptionIfNeed(); // "Invalid username or password, status code: " + obj.statusCode); if (obj == null) {
return (RefreshResponse) obj.response; return null;
} else {
obj.throwExceptionIfNeed(); // "Invalid username or password, status code: " + obj.statusCode);
return (RefreshResponse) obj.response;
}
} }
public NetworkResponse validate(String authToken) throws Throwable { public NetworkResponse validate(String authToken) throws Throwable {