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) {
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);

View File

@ -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();
@ -62,11 +62,13 @@ public class YggdrasilAuthenticator {
Log.i("Result", "Task " + endpoint + " failure");
}
if (responseClass == null) {
return new NetworkResponse(statusCode, outString);
}
} catch (UnknownHostException 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,9 +103,13 @@ public class YggdrasilAuthenticator {
public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable {
NetworkResponse obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class);
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 {
return makeRequest("validate", new RefreshRequest(authToken, null), null);