mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-19 01:27:18 -04:00
[Important change!] Invalidate access token when user log out
This commit is contained in:
parent
2af418a99a
commit
538873517c
@ -0,0 +1,42 @@
|
|||||||
|
package com.kdt.mojangauth;
|
||||||
|
|
||||||
|
import android.content.*;
|
||||||
|
import android.os.*;
|
||||||
|
import com.kdt.mojangauth.yggdrasil.*;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
import net.kdt.pojavlaunch.*;
|
||||||
|
|
||||||
|
public class InvalidateTokenTask extends AsyncTask<String, Void, Throwable> {
|
||||||
|
private YggdrasilAuthenticator authenticator = new YggdrasilAuthenticator();
|
||||||
|
//private Gson gson = new Gson();
|
||||||
|
private MCProfile.Builder profilePath;
|
||||||
|
|
||||||
|
private Context ctx;
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
public InvalidateTokenTask(Context ctx) {
|
||||||
|
this.ctx = ctx;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Throwable doInBackground(String... args) {
|
||||||
|
path = args[0];
|
||||||
|
try {
|
||||||
|
this.profilePath = MCProfile.load(args[0]);
|
||||||
|
this.authenticator.invalidate(profilePath.getAccessToken(), UUID.fromString(profilePath.getClientID()));
|
||||||
|
return null;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
return e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPostExecute(Throwable result) {
|
||||||
|
if (result != null) {
|
||||||
|
Tools.showError(ctx, result);
|
||||||
|
}
|
||||||
|
new File(path).delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -37,7 +37,7 @@ public class RefreshTokenTask extends AsyncTask<String, Void, Throwable> {
|
|||||||
this.profilePath = MCProfile.load(args[0]);
|
this.profilePath = MCProfile.load(args[0]);
|
||||||
// https://wiki.vg/Authentication
|
// https://wiki.vg/Authentication
|
||||||
// Returns an empty payload (204 No Content) if successful, an error JSON with status 403 Forbidden otherwise.
|
// Returns an empty payload (204 No Content) if successful, an error JSON with status 403 Forbidden otherwise.
|
||||||
if (204 != this.authenticator.validate(profilePath.getAccessToken())) {
|
if (204 != this.authenticator.validate(profilePath.getAccessToken()).statusCode) {
|
||||||
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?");
|
||||||
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.kdt.mojangauth.yggdrasil;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class NetworkResponse
|
||||||
|
{
|
||||||
|
public final int statusCode;
|
||||||
|
public final Object response;
|
||||||
|
public NetworkResponse(int statusCode, Object response) {
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void throwExceptionIfNeed(String msg) {
|
||||||
|
if (statusCode >= 400) {
|
||||||
|
throw new RuntimeException(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void throwExceptionIfNeed() {
|
||||||
|
throwExceptionIfNeed("Respone error, code: " + statusCode + ", message: " + response);
|
||||||
|
}
|
||||||
|
}
|
@ -6,16 +6,16 @@ import java.net.*;
|
|||||||
import java.nio.charset.*;
|
import java.nio.charset.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import net.kdt.pojavlaunch.*;
|
import net.kdt.pojavlaunch.*;
|
||||||
|
import org.apache.commons.io.*;
|
||||||
|
|
||||||
public class YggdrasilAuthenticator {
|
public class YggdrasilAuthenticator {
|
||||||
private static final String API_URL = "https://authserver.mojang.com/";
|
private static final String API_URL = "https://authserver.mojang.com/";
|
||||||
private String clientName = "Minecraft";
|
private String clientName = "Minecraft";
|
||||||
private int clientVersion = 1;
|
private int clientVersion = 1;
|
||||||
|
|
||||||
private <T> T makeRequest(String endpoint, Object inputObject, Class<T> 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;
|
||||||
byte[] buf = new byte[16384];
|
|
||||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
String requestJson = Tools.GLOBAL_GSON.toJson(inputObject);
|
String requestJson = Tools.GLOBAL_GSON.toJson(inputObject);
|
||||||
try {
|
try {
|
||||||
@ -40,7 +40,7 @@ public class YggdrasilAuthenticator {
|
|||||||
} else {
|
} else {
|
||||||
is = conn.getInputStream();
|
is = conn.getInputStream();
|
||||||
}
|
}
|
||||||
pipe(is, bos, buf);
|
IOUtils.copy(is, bos);
|
||||||
if (is != null) {
|
if (is != null) {
|
||||||
try {
|
try {
|
||||||
is.close();
|
is.close();
|
||||||
@ -52,14 +52,14 @@ public class YggdrasilAuthenticator {
|
|||||||
if (statusCode == 200 || statusCode == 204){
|
if (statusCode == 200 || statusCode == 204){
|
||||||
Log.i("Result", "Task " + endpoint + " successful");
|
Log.i("Result", "Task " + endpoint + " successful");
|
||||||
|
|
||||||
if (responseClass == null) {
|
if (responseClass != null) {
|
||||||
return (T) Integer.valueOf(statusCode);
|
return new NetworkResponse(statusCode, Tools.GLOBAL_GSON.fromJson(outString, responseClass));
|
||||||
} else {
|
|
||||||
return Tools.GLOBAL_GSON.fromJson(outString, responseClass);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.i("Result", "Task " + endpoint + " failure");
|
Log.i("Result", "Task " + endpoint + " failure");
|
||||||
return (T) Integer.valueOf(statusCode);
|
}
|
||||||
|
if (responseClass == null) {
|
||||||
|
return new NetworkResponse(statusCode, outString);
|
||||||
}
|
}
|
||||||
} 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);
|
||||||
@ -82,37 +82,32 @@ 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 {
|
||||||
Object obj = makeRequest("authenticate", new AuthenticateRequest(username, password, clientId, this.clientName, this.clientVersion), AuthenticateResponse.class);
|
NetworkResponse 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);
|
if (obj.statusCode != 200) {
|
||||||
|
throw new RuntimeException("Invalid username or password, status code: " + obj.statusCode);
|
||||||
}
|
}
|
||||||
return (AuthenticateResponse) obj;
|
*/
|
||||||
|
obj.throwExceptionIfNeed();
|
||||||
|
return (AuthenticateResponse) obj.response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable {
|
public RefreshResponse refresh(String authToken, UUID clientId) throws IOException, Throwable {
|
||||||
Object obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class);
|
NetworkResponse obj = makeRequest("refresh", new RefreshRequest(authToken, clientId), RefreshResponse.class);
|
||||||
if (obj instanceof Integer) {
|
obj.throwExceptionIfNeed(); // "Invalid username or password, status code: " + obj.statusCode);
|
||||||
throw new RuntimeException("Invalid username or password, status code: " + (Integer) obj);
|
return (RefreshResponse) obj.response;
|
||||||
}
|
|
||||||
return (RefreshResponse) obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int validate(String authToken) throws Throwable {
|
public NetworkResponse validate(String authToken) throws Throwable {
|
||||||
return (Integer) makeRequest("validate", new RefreshRequest(authToken, null), null);
|
return makeRequest("validate", new RefreshRequest(authToken, null), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void pipe(InputStream is, OutputStream out, byte[] buf) throws IOException {
|
public NetworkResponse invalidate(String authToken, UUID clientId) throws Throwable {
|
||||||
while (true) {
|
return makeRequest("invalidate", new RefreshRequest(authToken, clientId), null);
|
||||||
int amt = is.read(buf);
|
|
||||||
if (amt >= 0) {
|
|
||||||
out.write(buf, 0, amt);
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -677,7 +677,7 @@ public class PojavLoginActivity extends BaseActivity
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface p1, int p2) {
|
public void onClick(DialogInterface p1, int p2) {
|
||||||
file.delete();
|
new InvalidateTokenTask(PojavLoginActivity.this).execute(file.getAbsolutePath());
|
||||||
flv.refreshPath();
|
flv.refreshPath();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user