[Important change!] Do not refresh access token when it stills usable

This commit is contained in:
khanhduytran0 2020-12-07 15:56:06 +07:00
parent 598692ea93
commit 2af418a99a
2 changed files with 72 additions and 54 deletions

View File

@ -35,6 +35,9 @@ 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
// Returns an empty payload (204 No Content) if successful, an error JSON with status 403 Forbidden otherwise.
if (204 != this.authenticator.validate(profilePath.getAccessToken())) {
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) { if (response == null) {
throw new NullPointerException("Response is null?"); throw new NullPointerException("Response is null?");
@ -47,6 +50,7 @@ public class RefreshTokenTask extends AsyncTask<String, Void, Throwable> {
profilePath.setUsername(response.selectedProfile.name); profilePath.setUsername(response.selectedProfile.name);
profilePath.setProfileID(response.selectedProfile.id); profilePath.setProfileID(response.selectedProfile.id);
MCProfile.build(profilePath); MCProfile.build(profilePath);
}
return null; return null;
} catch (Throwable e) { } catch (Throwable e) {
return e; return e;

View File

@ -49,12 +49,18 @@ public class YggdrasilAuthenticator {
} }
} }
String outString = new String(bos.toByteArray(), Charset.forName("UTF-8")); String outString = new String(bos.toByteArray(), Charset.forName("UTF-8"));
if (statusCode == 200){ if (statusCode == 200 || statusCode == 204){
Log.i("Result", "Task " + endpoint + " successful"); Log.i("Result", "Task " + endpoint + " successful");
if (responseClass == null) {
return (T) Integer.valueOf(statusCode);
} else {
return Tools.GLOBAL_GSON.fromJson(outString, responseClass); return Tools.GLOBAL_GSON.fromJson(outString, responseClass);
} }
throw new RuntimeException("Invalid username or password, status code: " + statusCode); } else {
Log.i("Result", "Task " + endpoint + " failure");
return (T) Integer.valueOf(statusCode);
}
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
throw new RuntimeException("Can't connect to the server", e); throw new RuntimeException("Can't connect to the server", e);
} catch (Throwable th2) { } catch (Throwable th2) {
@ -79,11 +85,19 @@ public class YggdrasilAuthenticator {
} }
public AuthenticateResponse authenticate(String username, String password, UUID clientId) throws IOException, Throwable { public AuthenticateResponse authenticate(String username, String password, UUID clientId) throws IOException, Throwable {
return (AuthenticateResponse) makeRequest("authenticate", new AuthenticateRequest(username, password, clientId, this.clientName, this.clientVersion), AuthenticateResponse.class); Object obj = makeRequest("authenticate", new AuthenticateRequest(username, password, clientId, this.clientName, this.clientVersion), AuthenticateResponse.class);
if (obj instanceof Integer) {
throw new RuntimeException("Invalid username or password, status code: " + (Integer) obj);
}
return (AuthenticateResponse) obj;
} }
public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable { public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable {
return (RefreshResponse) makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class); Object obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class);
if (obj instanceof Integer) {
throw new RuntimeException("Invalid username or password, status code: " + (Integer) obj);
}
return (RefreshResponse) obj;
} }
public int validate(String authToken) throws Throwable { public int validate(String authToken) throws Throwable {