[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

@ -9,58 +9,62 @@ import com.kdt.mojangauth.yggdrasil.*;
import android.app.*; import android.app.*;
public class RefreshTokenTask extends AsyncTask<String, Void, Throwable> { public class RefreshTokenTask extends AsyncTask<String, Void, Throwable> {
private YggdrasilAuthenticator authenticator = new YggdrasilAuthenticator(); private YggdrasilAuthenticator authenticator = new YggdrasilAuthenticator();
//private Gson gson = new Gson(); //private Gson gson = new Gson();
private RefreshListener listener; private RefreshListener listener;
private MCProfile.Builder profilePath; private MCProfile.Builder profilePath;
private Context ctx; private Context ctx;
private ProgressDialog build; private ProgressDialog build;
public RefreshTokenTask(Context ctx, RefreshListener listener) { public RefreshTokenTask(Context ctx, RefreshListener listener) {
this.ctx = ctx; this.ctx = ctx;
this.listener = listener; this.listener = listener;
} }
@Override @Override
public void onPreExecute() { public void onPreExecute() {
build = new ProgressDialog(ctx); build = new ProgressDialog(ctx);
build.setMessage("Refreshing"); build.setMessage("Refreshing");
build.setProgressStyle(ProgressDialog.STYLE_SPINNER); build.setProgressStyle(ProgressDialog.STYLE_SPINNER);
build.setCancelable(false); build.setCancelable(false);
build.show(); build.show();
} }
@Override @Override
public Throwable doInBackground(String... args) { public Throwable doInBackground(String... args) {
try { try {
this.profilePath = MCProfile.load(args[0]); this.profilePath = MCProfile.load(args[0]);
RefreshResponse response = this.authenticator.refresh(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID())); // https://wiki.vg/Authentication
if (response == null) { // Returns an empty payload (204 No Content) if successful, an error JSON with status 403 Forbidden otherwise.
throw new NullPointerException("Response is null?"); if (204 != this.authenticator.validate(profilePath.getAccessToken())) {
} RefreshResponse response = this.authenticator.refresh(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID()));
if (response.selectedProfile == null) { if (response == null) {
throw new IllegalArgumentException("Can't refresh a demo account!"); throw new NullPointerException("Response is null?");
} }
profilePath.setClientID(response.clientToken.toString()); if (response.selectedProfile == null) {
profilePath.setAccessToken(response.accessToken); throw new IllegalArgumentException("Can't refresh a demo account!");
profilePath.setUsername(response.selectedProfile.name); }
profilePath.setProfileID(response.selectedProfile.id); profilePath.setClientID(response.clientToken.toString());
MCProfile.build(profilePath); profilePath.setAccessToken(response.accessToken);
return null; profilePath.setUsername(response.selectedProfile.name);
} catch (Throwable e) { profilePath.setProfileID(response.selectedProfile.id);
return e; MCProfile.build(profilePath);
} }
} return null;
} catch (Throwable e) {
return e;
}
}
@Override @Override
public void onPostExecute(Throwable result) { public void onPostExecute(Throwable result) {
build.dismiss(); build.dismiss();
if (result == null) { if (result == null) {
listener.onSuccess(); listener.onSuccess();
} else { } else {
listener.onFailed(result); listener.onFailed(result);
} }
} }
} }

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");
return Tools.GLOBAL_GSON.fromJson(outString, responseClass); if (responseClass == null) {
return (T) Integer.valueOf(statusCode);
} else {
return Tools.GLOBAL_GSON.fromJson(outString, responseClass);
}
} else {
Log.i("Result", "Task " + endpoint + " failure");
return (T) Integer.valueOf(statusCode);
} }
throw new RuntimeException("Invalid username or password, status code: " + 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 {